質問編集履歴
1
アドバイスに基づく修正ソース
test
CHANGED
File without changes
|
test
CHANGED
@@ -177,3 +177,167 @@
|
|
177
177
|
|
178
178
|
|
179
179
|
###補足情報(言語/FW/ツール等のバージョンなど)
|
180
|
+
|
181
|
+
ご回答ありがとうございます。うまくいきました。
|
182
|
+
|
183
|
+
Circle.java::
|
184
|
+
|
185
|
+
import java.awt.Color;
|
186
|
+
|
187
|
+
import java.awt.Graphics;
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
public class Circle
|
192
|
+
|
193
|
+
{
|
194
|
+
|
195
|
+
private int x;
|
196
|
+
|
197
|
+
private int y;
|
198
|
+
|
199
|
+
private int radius;
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
public Circle(int x, int y, int radius)
|
204
|
+
|
205
|
+
{
|
206
|
+
|
207
|
+
this.x = x;
|
208
|
+
|
209
|
+
this.y = y;
|
210
|
+
|
211
|
+
this.radius = radius;
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
public void draw(Graphics g)
|
216
|
+
|
217
|
+
{
|
218
|
+
|
219
|
+
g.setColor(Color.red);
|
220
|
+
|
221
|
+
g.drawOval(x - radius / 2, y - radius / 2, radius, radius);
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
Sample4.java::
|
230
|
+
|
231
|
+
import java.awt.BorderLayout;
|
232
|
+
|
233
|
+
import java.awt.Graphics;
|
234
|
+
|
235
|
+
import java.awt.event.MouseAdapter;
|
236
|
+
|
237
|
+
import java.awt.event.MouseEvent;
|
238
|
+
|
239
|
+
import java.util.ArrayList;
|
240
|
+
|
241
|
+
import java.util.Iterator;
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
import javax.swing.JFrame;
|
246
|
+
|
247
|
+
import javax.swing.JPanel;
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
public class Sample4 extends JFrame
|
252
|
+
|
253
|
+
{
|
254
|
+
|
255
|
+
private SamplePanel sp;
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
public static void main(String[] args)
|
260
|
+
|
261
|
+
{
|
262
|
+
|
263
|
+
Sample4 sm = new Sample4();
|
264
|
+
|
265
|
+
sm.setVisible(true);
|
266
|
+
|
267
|
+
}
|
268
|
+
|
269
|
+
public Sample4()
|
270
|
+
|
271
|
+
{
|
272
|
+
|
273
|
+
super("円を描画");
|
274
|
+
|
275
|
+
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
276
|
+
|
277
|
+
setSize(500, 500);
|
278
|
+
|
279
|
+
sp = new SamplePanel();
|
280
|
+
|
281
|
+
add(sp, BorderLayout.CENTER);
|
282
|
+
|
283
|
+
}
|
284
|
+
|
285
|
+
public class SamplePanel extends JPanel
|
286
|
+
|
287
|
+
{
|
288
|
+
|
289
|
+
private int radius = 0;
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
private ArrayList<Circle> circlelist = new ArrayList<Circle>();
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
public SamplePanel()
|
298
|
+
|
299
|
+
{
|
300
|
+
|
301
|
+
addMouseListener(new SampleMouseListener());
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
public void paint(Graphics g)
|
306
|
+
|
307
|
+
{
|
308
|
+
|
309
|
+
super.paint(g);;
|
310
|
+
|
311
|
+
Iterator<Circle> it = circlelist.iterator();
|
312
|
+
|
313
|
+
while(it.hasNext()) {
|
314
|
+
|
315
|
+
Circle c = it.next();
|
316
|
+
|
317
|
+
c.draw(g);
|
318
|
+
|
319
|
+
}
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
public class SampleMouseListener extends MouseAdapter
|
324
|
+
|
325
|
+
{
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
public void mousePressed(MouseEvent e)
|
330
|
+
|
331
|
+
{
|
332
|
+
|
333
|
+
circlelist.add(new Circle(e.getX(), e.getY(), radius++));
|
334
|
+
|
335
|
+
repaint();
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
}
|
340
|
+
|
341
|
+
}
|
342
|
+
|
343
|
+
}
|