質問編集履歴
1
アドバイスに基づく修正ソース
title
CHANGED
File without changes
|
body
CHANGED
@@ -87,4 +87,86 @@
|
|
87
87
|
}
|
88
88
|
|
89
89
|
|
90
|
-
###補足情報(言語/FW/ツール等のバージョンなど)
|
90
|
+
###補足情報(言語/FW/ツール等のバージョンなど)
|
91
|
+
ご回答ありがとうございます。うまくいきました。
|
92
|
+
Circle.java::
|
93
|
+
import java.awt.Color;
|
94
|
+
import java.awt.Graphics;
|
95
|
+
|
96
|
+
public class Circle
|
97
|
+
{
|
98
|
+
private int x;
|
99
|
+
private int y;
|
100
|
+
private int radius;
|
101
|
+
|
102
|
+
public Circle(int x, int y, int radius)
|
103
|
+
{
|
104
|
+
this.x = x;
|
105
|
+
this.y = y;
|
106
|
+
this.radius = radius;
|
107
|
+
}
|
108
|
+
public void draw(Graphics g)
|
109
|
+
{
|
110
|
+
g.setColor(Color.red);
|
111
|
+
g.drawOval(x - radius / 2, y - radius / 2, radius, radius);
|
112
|
+
}
|
113
|
+
}
|
114
|
+
|
115
|
+
Sample4.java::
|
116
|
+
import java.awt.BorderLayout;
|
117
|
+
import java.awt.Graphics;
|
118
|
+
import java.awt.event.MouseAdapter;
|
119
|
+
import java.awt.event.MouseEvent;
|
120
|
+
import java.util.ArrayList;
|
121
|
+
import java.util.Iterator;
|
122
|
+
|
123
|
+
import javax.swing.JFrame;
|
124
|
+
import javax.swing.JPanel;
|
125
|
+
|
126
|
+
public class Sample4 extends JFrame
|
127
|
+
{
|
128
|
+
private SamplePanel sp;
|
129
|
+
|
130
|
+
public static void main(String[] args)
|
131
|
+
{
|
132
|
+
Sample4 sm = new Sample4();
|
133
|
+
sm.setVisible(true);
|
134
|
+
}
|
135
|
+
public Sample4()
|
136
|
+
{
|
137
|
+
super("円を描画");
|
138
|
+
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
139
|
+
setSize(500, 500);
|
140
|
+
sp = new SamplePanel();
|
141
|
+
add(sp, BorderLayout.CENTER);
|
142
|
+
}
|
143
|
+
public class SamplePanel extends JPanel
|
144
|
+
{
|
145
|
+
private int radius = 0;
|
146
|
+
|
147
|
+
private ArrayList<Circle> circlelist = new ArrayList<Circle>();
|
148
|
+
|
149
|
+
public SamplePanel()
|
150
|
+
{
|
151
|
+
addMouseListener(new SampleMouseListener());
|
152
|
+
}
|
153
|
+
public void paint(Graphics g)
|
154
|
+
{
|
155
|
+
super.paint(g);;
|
156
|
+
Iterator<Circle> it = circlelist.iterator();
|
157
|
+
while(it.hasNext()) {
|
158
|
+
Circle c = it.next();
|
159
|
+
c.draw(g);
|
160
|
+
}
|
161
|
+
}
|
162
|
+
public class SampleMouseListener extends MouseAdapter
|
163
|
+
{
|
164
|
+
|
165
|
+
public void mousePressed(MouseEvent e)
|
166
|
+
{
|
167
|
+
circlelist.add(new Circle(e.getX(), e.getY(), radius++));
|
168
|
+
repaint();
|
169
|
+
}
|
170
|
+
}
|
171
|
+
}
|
172
|
+
}
|