質問編集履歴
2
コードを本文に記載
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,41 +6,264 @@
|
|
6
6
|
|
7
7
|
ドローエディタを作成したい.
|
8
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
|
-
|
9
|
+
|
10
|
+
### 該当のソースコード
|
11
|
+
```java
|
12
|
+
import javax.swing.*;
|
13
|
+
import java.awt.*;
|
14
|
+
import java.awt.event.*;
|
15
|
+
import java.util.*;
|
16
|
+
// 描画した図形を記録する Figure クラス (継承して利用する)
|
17
|
+
class Figure {
|
18
|
+
protected int x, y, width, height;
|
19
|
+
protected Color color;
|
20
|
+
public Figure(int x, int y, int w, int h, Color c) {
|
21
|
+
this.x = x;
|
22
|
+
this.y = y; // this.x, this.y はインスタンス変数.
|
23
|
+
width = w;
|
24
|
+
height = h; // ローカル変数で同名の変数がある場合は,this
|
25
|
+
color = c; // を付けると,インスタンス変数を指す.
|
26
|
+
}
|
27
|
+
public void setSize(int w, int h) {
|
28
|
+
width = w;
|
29
|
+
height = h;
|
30
|
+
}
|
31
|
+
public void setLocation(int x, int y) {
|
32
|
+
this.x = x;
|
33
|
+
this.y = y;
|
34
|
+
}
|
35
|
+
public void reshape(int x1, int y1, int x2, int y2) {
|
36
|
+
}
|
37
|
+
public void draw(Graphics g) {
|
38
|
+
}
|
39
|
+
}
|
40
|
+
class CircleFigure extends Figure {
|
41
|
+
public CircleFigure(int x, int y, int w, int h, Color c) {
|
42
|
+
super(x, y, w, h, c);
|
43
|
+
}
|
44
|
+
public void reshape(int x1, int y1, int x2, int y2) {
|
45
|
+
int newx = Math.min(x1, x2);
|
46
|
+
int newy = Math.min(y1, y2);
|
47
|
+
int neww = Math.abs(x1 - x2);
|
48
|
+
int newh = Math.abs(y1 - y2);
|
49
|
+
setLocation(newx, newy);
|
50
|
+
setSize(neww, newh);
|
51
|
+
}
|
52
|
+
public void draw(Graphics g) {
|
53
|
+
g.setColor(color);
|
54
|
+
g.drawOval(x, y, width, height);
|
55
|
+
}
|
56
|
+
}
|
57
|
+
class LineFigure extends Figure {
|
58
|
+
public LineFigure(int x, int y, int w, int h, Color c) {
|
59
|
+
super(x, y, w, h, c);
|
60
|
+
}
|
61
|
+
public void reshape(int x1, int y1, int x2, int y2) {
|
62
|
+
setLocation(x1, y1);
|
63
|
+
setSize(x2, y2);
|
64
|
+
}
|
65
|
+
public void draw(Graphics g) {
|
66
|
+
g.setColor(color);
|
67
|
+
g.drawLine(x, y, width, height);
|
68
|
+
}
|
69
|
+
}
|
70
|
+
class RectangleFigure extends Figure {
|
71
|
+
public RectangleFigure(int x, int y, int w, int h, Color c) {
|
72
|
+
super(x, y, w, h, c);
|
73
|
+
// 引数付きのコンストラクタは継承されないので,コンストラクタを定義.
|
74
|
+
// superで親のコンストラクタを呼び出すだけ.
|
75
|
+
}
|
76
|
+
public void reshape(int x1, int y1, int x2, int y2) {
|
77
|
+
int newx = Math.min(x1, x2);
|
78
|
+
int newy = Math.min(y1, y2);
|
79
|
+
int neww = Math.abs(x1 - x2);
|
80
|
+
int newh = Math.abs(y1 - y2);
|
81
|
+
setLocation(newx, newy);
|
82
|
+
setSize(neww, newh);
|
83
|
+
}
|
84
|
+
public void draw(Graphics g) {
|
85
|
+
g.setColor(color);
|
86
|
+
g.drawRect(x, y, width, height);
|
87
|
+
}
|
88
|
+
}
|
89
|
+
////////////////////////////////////////////////
|
90
|
+
// Model (M)
|
91
|
+
// modelは java.util.Observableを継承する.Viewに監視される.
|
92
|
+
class DrawModel extends Observable {
|
93
|
+
protected ArrayList<Figure> fig;
|
94
|
+
protected String figurelabel;
|
95
|
+
protected Figure drawingFigure;
|
96
|
+
protected Color currentColor;
|
97
|
+
protected ViewPanel viewPanel;
|
98
|
+
public DrawModel() {
|
99
|
+
fig = new ArrayList<Figure>();
|
100
|
+
drawingFigure = null;
|
101
|
+
currentColor = Color.red;
|
102
|
+
}
|
103
|
+
public void setViewPanel(ViewPanel c) {
|
104
|
+
viewPanel = c;
|
105
|
+
}
|
106
|
+
public ArrayList<Figure> getFigures() {
|
107
|
+
return fig;
|
108
|
+
}
|
109
|
+
public Figure getFigure(int idx) {
|
110
|
+
return fig.get(idx);
|
111
|
+
}
|
112
|
+
public void createFigure(int x, int y) {
|
113
|
+
Figure f = null;
|
114
|
+
;
|
115
|
+
if (figurelabel == "rect")
|
116
|
+
f = new RectangleFigure(x, y, 0, 0, currentColor);
|
117
|
+
else if (figurelabel == "circ")
|
118
|
+
f = new CircleFigure(x, y, 0, 0, currentColor);
|
119
|
+
else if (figurelabel == "line")
|
120
|
+
f = new LineFigure(x, y, x, y, currentColor);
|
121
|
+
fig.add(f);
|
122
|
+
drawingFigure = f;
|
123
|
+
viewPanel.repaint();
|
124
|
+
setChanged();
|
125
|
+
notifyObservers();
|
126
|
+
}
|
127
|
+
public void reshapeFigure(int x1, int y1, int x2, int y2) {
|
128
|
+
if (drawingFigure != null) {
|
129
|
+
drawingFigure.reshape(x1, y1, x2, y2);
|
130
|
+
viewPanel.repaint();
|
131
|
+
setChanged();
|
132
|
+
notifyObservers();
|
133
|
+
}
|
134
|
+
}
|
135
|
+
public void changecolor(Color col) {
|
136
|
+
currentColor = col;
|
137
|
+
}
|
138
|
+
public void changefigure(String fi) {
|
139
|
+
figurelabel = fi;
|
140
|
+
}
|
141
|
+
}
|
142
|
+
////////////////////////////////////////////////
|
143
|
+
// View (V)
|
144
|
+
// Viewは,Observerをimplementsする.Modelを監視して,
|
145
|
+
// モデルが更新されたupdateする.実際には,Modelから
|
146
|
+
// update が呼び出される.
|
147
|
+
class ViewPanel extends JPanel implements Observer {
|
148
|
+
protected DrawModel model;
|
149
|
+
public ViewPanel(DrawModel m) {
|
150
|
+
this.setBackground(Color.white);
|
151
|
+
model = m;
|
152
|
+
model.addObserver(this);
|
153
|
+
}
|
154
|
+
public void paintComponent(Graphics g) {
|
155
|
+
super.paintComponent(g);
|
156
|
+
ArrayList<Figure> fig = model.getFigures();
|
157
|
+
for (int i = 0; i < fig.size(); i++) {
|
158
|
+
Figure f = fig.get(i);
|
159
|
+
f.draw(g);
|
160
|
+
}
|
161
|
+
}
|
162
|
+
public void update(Observable o, Object arg) {
|
163
|
+
repaint();
|
164
|
+
}
|
165
|
+
}
|
166
|
+
class Select implements ActionListener {
|
167
|
+
DrawModel a;
|
168
|
+
Select(DrawModel ap) {
|
169
|
+
a = ap;
|
170
|
+
}
|
171
|
+
public void actionPerformed(ActionEvent e) { // 設定の変更
|
172
|
+
String es = e.getActionCommand();
|
173
|
+
if (es.equals("red"))
|
174
|
+
a.changecolor(Color.red);
|
175
|
+
if (es.equals("green"))
|
176
|
+
a.changecolor(Color.green);
|
177
|
+
if (es.equals("blue"))
|
178
|
+
a.changecolor(Color.blue);
|
179
|
+
if (es.equals("rect"))
|
180
|
+
a.changefigure("rect");
|
181
|
+
if (es.equals("circ"))
|
182
|
+
a.changefigure("circ");
|
183
|
+
if (es.equals("line"))
|
184
|
+
a.changefigure("line");
|
185
|
+
}
|
186
|
+
}
|
187
|
+
//////////////////////////////////////////////////
|
188
|
+
// Main class
|
189
|
+
// (GUIを組み立てているので,view の一部と考えてもよい)
|
190
|
+
class DrawFrame extends JFrame {
|
191
|
+
DrawModel model;
|
192
|
+
ViewPanel view;
|
193
|
+
DrawController cont;
|
194
|
+
public static void main(String[] args) {
|
195
|
+
JFrame f = new JFrame("Draw");
|
196
|
+
JPanel pc = new JPanel();
|
197
|
+
JPanel pf = new JPanel();
|
198
|
+
pc.setLayout(new GridLayout(1, 3));
|
199
|
+
pf.setLayout(new GridLayout(1, 3));
|
200
|
+
JButton r = new JButton("red");
|
201
|
+
JButton g = new JButton("green");
|
202
|
+
JButton b = new JButton("blue");
|
203
|
+
JButton rect = new JButton("rect");
|
204
|
+
JButton circ = new JButton("circ");
|
205
|
+
JButton line = new JButton("line");
|
206
|
+
r.setActionCommand("red");
|
207
|
+
g.setActionCommand("green");
|
208
|
+
b.setActionCommand("blue");
|
209
|
+
rect.setActionCommand("rect");
|
210
|
+
circ.setActionCommand("circ");
|
211
|
+
line.setActionCommand("line");
|
212
|
+
DrawModel a = new DrawModel();
|
213
|
+
ViewPanel dp = new ViewPanel(a);
|
214
|
+
a.setViewPanel(dp);
|
215
|
+
DrawController ml = new DrawController(a);
|
216
|
+
dp.addMouseListener(ml);
|
217
|
+
dp.addMouseMotionListener(ml);
|
218
|
+
pc.add(r);
|
219
|
+
pc.add(g);
|
220
|
+
pc.add(b);
|
221
|
+
pf.add(rect);
|
222
|
+
pf.add(circ);
|
223
|
+
pf.add(line);
|
224
|
+
b.addActionListener(new Select(a));
|
225
|
+
g.addActionListener(new Select(a));
|
226
|
+
r.addActionListener(new Select(a));
|
227
|
+
circ.addActionListener(new Select(a));
|
228
|
+
rect.addActionListener(new Select(a));
|
229
|
+
line.addActionListener(new Select(a));
|
230
|
+
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
231
|
+
f.getContentPane().add(dp, BorderLayout.CENTER);
|
232
|
+
f.getContentPane().add(pc, BorderLayout.SOUTH);
|
233
|
+
f.getContentPane().add(pf, BorderLayout.NORTH);
|
234
|
+
f.setSize(400, 300);
|
235
|
+
f.setVisible(true);
|
236
|
+
}
|
237
|
+
}
|
238
|
+
////////////////////////////////////////////////
|
239
|
+
// Controller (C)
|
240
|
+
class DrawController implements MouseListener, MouseMotionListener {
|
241
|
+
protected DrawModel model;
|
242
|
+
protected int dragStartX, dragStartY;
|
243
|
+
public DrawController(DrawModel a) {
|
244
|
+
model = a;
|
245
|
+
}
|
246
|
+
public void mouseClicked(MouseEvent e) {
|
247
|
+
}
|
248
|
+
public void mousePressed(MouseEvent e) {
|
249
|
+
dragStartX = e.getX();
|
250
|
+
dragStartY = e.getY();
|
251
|
+
model.createFigure(dragStartX, dragStartY);
|
252
|
+
}
|
253
|
+
public void mouseDragged(MouseEvent e) {
|
254
|
+
model.reshapeFigure(dragStartX, dragStartY, e.getX(), e.getY());
|
255
|
+
}
|
256
|
+
public void mouseReleased(MouseEvent e) {
|
257
|
+
model.reshapeFigure(dragStartX, dragStartY, e.getX(), e.getY());
|
258
|
+
}
|
259
|
+
public void mouseEntered(MouseEvent e) {
|
260
|
+
}
|
261
|
+
public void mouseExited(MouseEvent e) {
|
262
|
+
}
|
263
|
+
public void mouseMoved(MouseEvent e) {
|
264
|
+
}
|
265
|
+
}
|
266
|
+
```
|
267
|
+
|
268
|
+
|
269
|
+
|
1
コードを本文に記載
test
CHANGED
File without changes
|
test
CHANGED
@@ -41,5 +41,6 @@
|
|
41
41
|
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
|
42
42
|
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
|
43
43
|
```
|
44
|
+
### 該当のソースコード ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; // 描画した図形を記録する Figure クラス (継承して利用する) class Figure { protected int x, y, width, height; protected Color color; public Figure(int x, int y, int w, int h, Color c) { this.x = x; this.y = y; // this.x, this.y はインスタンス変数. width = w; height = h; // ローカル変数で同名の変数がある場合は,this color = c; // を付けると,インスタンス変数を指す. } public void setSize(int w, int h) { width = w; height = h; } public void setLocation(int x, int y) { this.x = x; this.y = y; } public void reshape(int x1, int y1, int x2, int y2) { } public void draw(Graphics g) { } } class CircleFigure extends Figure { public CircleFigure(int x, int y, int w, int h, Color c) { super(x, y, w, h, c); } public void reshape(int x1, int y1, int x2, int y2) { int newx = Math.min(x1, x2); int newy = Math.min(y1, y2); int neww = Math.abs(x1 - x2); int newh = Math.abs(y1 - y2); setLocation(newx, newy); setSize(neww, newh); } public void draw(Graphics g) { g.setColor(color); g.drawOval(x, y, width, height); } } class LineFigure extends Figure { public LineFigure(int x, int y, int w, int h, Color c) { super(x, y, w, h, c); } public void reshape(int x1, int y1, int x2, int y2) { setLocation(x1, y1); setSize(x2, y2); } public void draw(Graphics g) { g.setColor(color); g.drawLine(x, y, width, height); } } class RectangleFigure extends Figure { public RectangleFigure(int x, int y, int w, int h, Color c) { super(x, y, w, h, c); // 引数付きのコンストラクタは継承されないので,コンストラクタを定義. // superで親のコンストラクタを呼び出すだけ. } public void reshape(int x1, int y1, int x2, int y2) { int newx = Math.min(x1, x2); int newy = Math.min(y1, y2); int neww = Math.abs(x1 - x2); int newh = Math.abs(y1 - y2); setLocation(newx, newy); setSize(neww, newh); } public void draw(Graphics g) { g.setColor(color); g.drawRect(x, y, width, height); } } //////////////////////////////////////////////// // Model (M) // modelは java.util.Observableを継承する.Viewに監視される. class DrawModel extends Observable { protected ArrayList<Figure> fig; protected String figurelabel; protected Figure drawingFigure; protected Color currentColor; protected ViewPanel viewPanel; public DrawModel() { fig = new ArrayList<Figure>(); drawingFigure = null; currentColor = Color.red; } public void setViewPanel(ViewPanel c) { viewPanel = c; } public ArrayList<Figure> getFigures() { return fig; } public Figure getFigure(int idx) { return fig.get(idx); } public void createFigure(int x, int y) { Figure f = null; ; if (figurelabel == "rect") f = new RectangleFigure(x, y, 0, 0, currentColor); else if (figurelabel == "circ") f = new CircleFigure(x, y, 0, 0, currentColor); else if (figurelabel == "line") f = new LineFigure(x, y, x, y, currentColor); fig.add(f); drawingFigure = f; viewPanel.repaint(); setChanged(); notifyObservers(); } public void reshapeFigure(int x1, int y1, int x2, int y2) { if (drawingFigure != null) { drawingFigure.reshape(x1, y1, x2, y2); viewPanel.repaint(); setChanged(); notifyObservers(); } } public void changecolor(Color col) { currentColor = col; } public void changefigure(String fi) { figurelabel = fi; } } //////////////////////////////////////////////// // View (V) // Viewは,Observerをimplementsする.Modelを監視して, // モデルが更新されたupdateする.実際には,Modelから // update が呼び出される. class ViewPanel extends JPanel implements Observer { protected DrawModel model; public ViewPanel(DrawModel m) { this.setBackground(Color.white); model = m; model.addObserver(this); } public void paintComponent(Graphics g) { super.paintComponent(g); ArrayList<Figure> fig = model.getFigures(); for (int i = 0; i < fig.size(); i++) { Figure f = fig.get(i); f.draw(g); } } public void update(Observable o, Object arg) { repaint(); } } class Select implements ActionListener { DrawModel a; Select(DrawModel ap) { a = ap; } public void actionPerformed(ActionEvent e) { // 設定の変更 String es = e.getActionCommand(); if (es.equals("red")) a.changecolor(Color.red); if (es.equals("green")) a.changecolor(Color.green); if (es.equals("blue")) a.changecolor(Color.blue); if (es.equals("rect")) a.changefigure("rect"); if (es.equals("circ")) a.changefigure("circ"); if (es.equals("line")) a.changefigure("line"); } } ////////////////////////////////////////////////// // Main class // (GUIを組み立てているので,view の一部と考えてもよい) class DrawFrame extends JFrame { DrawModel model; ViewPanel view; DrawController cont; public static void main(String[] args) { JFrame f = new JFrame("Draw"); JPanel pc = new JPanel(); JPanel pf = new JPanel(); pc.setLayout(new GridLayout(1, 3)); pf.setLayout(new GridLayout(1, 3)); JButton r = new JButton("red"); JButton g = new JButton("green"); JButton b = new JButton("blue"); JButton rect = new JButton("rect"); JButton circ = new JButton("circ"); JButton line = new JButton("line"); r.setActionCommand("red"); g.setActionCommand("green"); b.setActionCommand("blue"); rect.setActionCommand("rect"); circ.setActionCommand("circ"); line.setActionCommand("line"); DrawModel a = new DrawModel(); ViewPanel dp = new ViewPanel(a); a.setViewPanel(dp); DrawController ml = new DrawController(a); dp.addMouseListener(ml); dp.addMouseMotionListener(ml); pc.add(r); pc.add(g); pc.add(b); pf.add(rect); pf.add(circ); pf.add(line); b.addActionListener(new Select(a)); g.addActionListener(new Select(a)); r.addActionListener(new Select(a)); circ.addActionListener(new Select(a)); rect.addActionListener(new Select(a)); line.addActionListener(new Select(a)); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(dp, BorderLayout.CENTER); f.getContentPane().add(pc, BorderLayout.SOUTH); f.getContentPane().add(pf, BorderLayout.NORTH); f.setSize(400, 300); f.setVisible(true); } } //////////////////////////////////////////////// // Controller (C) class DrawController implements MouseListener, MouseMotionListener { protected DrawModel model; protected int dragStartX, dragStartY; public DrawController(DrawModel a) { model = a; } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { dragStartX = e.getX(); dragStartY = e.getY(); model.createFigure(dragStartX, dragStartY); } public void mouseDragged(MouseEvent e) { model.reshapeFigure(dragStartX, dragStartY, e.getX(), e.getY()); } public void mouseReleased(MouseEvent e) { model.reshapeFigure(dragStartX, dragStartY, e.getX(), e.getY()); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseMoved(MouseEvent e) { } } ``` ### 試したこと コンパイルを行った. ``` 「DrawFrame.java:98: 警告:[deprecation] java.utilのObservableは推奨されません class DrawModel extends Observable { ^ DrawFrame.java:163: 警告:[deprecation] java.utilのObserverは推奨されません class ViewPanel extends JPanel implements Observer { ^ DrawFrame.java:181: 警告:[deprecation] java.utilのObservableは推奨されません public void update(Observable o, Object arg) { ^ 警告3個」 ``` といった警告を確認した.
|
44
45
|
|
45
46
|
|