J'ai écrit cette applet : http://sep.free.fr/test.

De temps en temps, lorsqu'on clique sur le bouton run, on constate le problème d'affichage suivant :


Voilà le code de l'applet :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
 
public class Test extends JApplet implements Runnable {
 
	/**
         *
         */
	private class ButtonPanel extends JPanel implements ActionListener {
 
		private JButton	newButton ;
		private JButton	runButton ;
		private JButton	pauseButton ;
		private JButton	infoButton ;
 
		private ButtonPanel() {
 
			super() ;
			setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS)) ;
			newButton = new JButton ("New") ;
			runButton = new JButton ("Run") ;
			pauseButton = new JButton ("Pause") ;
			infoButton = new JButton ("Info") ;
			pauseButton.setEnabled(false) ;
			newButton.addActionListener(this) ;
			runButton.addActionListener(this) ;
			pauseButton.addActionListener(this) ;
			infoButton.addActionListener(this) ;
			newButton.setToolTipText("New simulation") ;
			runButton.setToolTipText("Run simulation") ;
			pauseButton.setToolTipText("Pause simulation") ;
			infoButton.setToolTipText("Simulation info") ;
			add(newButton) ;
			add(runButton) ;
			add(pauseButton) ;
			add(infoButton) ;
 
		}
 
		public void actionPerformed(ActionEvent e) {
 
			if ( e.getSource() == newButton ) {
				pause = true ;
				pauseButton.setEnabled(false) ;
				runButton.setEnabled(true);
				depopulate();
				populate(blacks,whites);
				iterations=0;
			}
			else if ( e.getSource() == runButton ) {
				pauseButton.setEnabled(true);
				runButton.setEnabled(false);
				pause = false;
			}
			else if ( e.getSource() == pauseButton ) {
				pause = true;
				pauseButton.setEnabled(false);
				runButton.setEnabled(true);
			}
			else if ( e.getSource() == infoButton ) {
				boolean wasPaused = pause;
				pause = true;
				JOptionPane.showMessageDialog(this, getInfo(), "Information", JOptionPane.INFORMATION_MESSAGE) ;
				pause = wasPaused;
			}
			else
				Toolkit.getDefaultToolkit().beep();
 
		}
 
	} // End Inner Class ButtonPanel
 
	/**
         *
         */
	private class CityPanel extends JPanel {
 
		static final int xMargin = 20;   
		static final int yMargin = 15;   
		static final int pSize = 5;   
 
		public void paintComponent(Graphics g) {
			g.setColor(Color.gray);
			g.fillRect(xMargin-1, yMargin-1, cityX*pSize+1, cityY*pSize+1);
 
			for (int x=0; x<cityX; x++)
				for (int y=0; y<cityY; y++) {
					if (city[x][y]!=null) plot(x,y);
				}
		}
 
		void plot(int x, int y) {
			Graphics g = this.getGraphics();
			if (city[x][y]!=null) g.setColor(city[x][y].color);
			else g.setColor(Color.gray);
			g.fillRect(xMargin+x*pSize, yMargin+y*pSize, pSize-1, pSize-1);
		}
 
	}
 
	class Agent {
 
		final Color color;
		private int x,y;
 
		Agent(Color color) {
			this.color = color;
		}
 
		void setPosition(int x, int y) {
			this.x = x;
			this.y = y;		
		}
 
		int getX() {
			return x;	
		}
 
		int getY() {
			return y;	
		}
 
		boolean getSatisfaction() {
 
			int stranger = 0;
			int similar = 0;
			for (int dx=-1;dx<=1;dx++)
				for (int dy=-1;dy<=1;dy++) {
					int nX=x+dx;
					int nY=y+dy;
					if (nX<0) nX=cityX-1;
					if (nY<0) nY=cityY-1;
					if (nX>=cityX) nX=0;
					if (nY>=cityY) nY=0;
					Agent neighbour = city[nX][nY];
					if (neighbour != null) {
						if (neighbour.color==this.color)
							similar++;
						else stranger++;
					}
				}
			boolean satisfaction = (stranger<=similar+1);
			return satisfaction;
 
		}
 
	}
 
	private int cityY;
	private int cityX;
	private Agent [][] city;
	private CityPanel cityPanel;
	private Thread runner;
	private int iterations=0;
	private boolean pause=true;
	private int blacks;
	private int whites;
 
	public void init() {
		System.out.println("init");
 
		String widthString = getParameter("myWidth");
		String heightString = getParameter("myHeight");
		String blacksParameter = getParameter("blacks");
		String whitesParameter = getParameter("whites");
 
		cityY = Integer.parseInt(widthString);
		cityX = Integer.parseInt(heightString);
		city = new Agent [cityX][cityY];
 
		try {
			javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
				public void run() {
					createGUI();
				}
			});
		} catch (Exception e) {
			System.err.println("createGUI didn't successfully complete");
		}
 
		blacks = Integer.parseInt(blacksParameter);
		whites = Integer.parseInt(whitesParameter);
		populate(blacks,whites);
	}
 
	private String getInfo() {
 
		String nl=System.getProperty("line.separator");
		String info = 
			"Population:"+nl+
			"     Blacks:"+blacks+nl+
			"     Whites:"+whites+nl+
			"     Total:"+(blacks+whites)+nl+nl+
			"Simulation size:"+nl+
			"     Eight:"+cityX+nl+
			"     Width:"+cityY+nl+
			"     Total:"+(cityX*cityY)+nl+
			"     Vacancies:"+((cityX*cityY)-(blacks+whites))+nl+nl+
			"Iterations:"+iterations
			;
		return info;
	}
 
	public void start() {
		if (runner == null) {
			runner = new Thread(this);
			runner.start();
		}
	}
 
	public void run() {
		Thread thisThread = Thread.currentThread();
		int count = 0;
		while (runner == thisThread) {
			if (!pause) action();
			count++;if (count>10) {
				try {Thread.sleep(1);} catch (InterruptedException e) { }
				count=0;
			}
		}
	}
 
	private void action() {
		Agent agent = getRandomAgent();
		if (!agent.getSatisfaction()) {
			move(agent);
		}
		iterations++;
	}
 
	private void move(Agent agent) {
		int x=agent.getX();
		int y=agent.getY();
		this.findFreeLocation(agent);
		city[x][y]=null;
		cityPanel.plot(x, y);
	}
 
	private Agent getRandomAgent() {
		do {
			int x = getRandomX();
			int y = getRandomY();
			if (city[x][y]!=null)
				return city[x][y];
		}
		while (true);
	}
 
	private void depopulate() {
 
		for (int x=0; x<cityX; x++)
			for (int y=0; y<cityY; y++) {
				city[x][y]=null;
				cityPanel.plot(x, y);
			}
 
	}
 
 
	private void populate(int blacks, int whites) {
		int population=blacks+whites;
		for (int count = 0; count < population; count ++) {
			Agent newAgent;
			double p = (double)blacks/(blacks+whites);
			double r = Math.random();
			if (r<p) {
				newAgent = new Agent(Color.BLACK);
				blacks--;
			}
			else {
				newAgent = new Agent(Color.WHITE);
				whites--;
			}
			findFreeLocation(newAgent);
		}
	}
 
	private void findFreeLocation(Agent agent) {
		do {
			int x = getRandomX();
			int y = getRandomY();
			if (city[x][y]==null) {
				city[x][y] = agent;
				agent.setPosition(x, y);
				cityPanel.plot(x, y);
				break;
			}
		}
		while (true);
	}
 
	private int getRandomY() {
		return (int) (Math.random()*this.cityY);
	}
 
	private int getRandomX() {
		return (int) (Math.random()*this.cityX);
	}
 
	private void createGUI() {
		cityPanel= new CityPanel();
		getContentPane().add(cityPanel, BorderLayout.CENTER);
		getContentPane().add(new ButtonPanel(), BorderLayout.SOUTH);
	}
 
 
}
Votre avis ?