回答編集履歴

2

見直しキャンペーン中

2023/08/13 12:06

投稿

TN8001
TN8001

スコア9862

test CHANGED
@@ -1,263 +1,132 @@
1
1
  直接的な原因は`getGraphics()`したものは、`Foreground`の色が設定されます。
2
-
3
2
  そのため`setColor()`したとしても`getGraphics()`するたびに、黒に戻ってしまいます。
4
-
5
3
  方針としては`Color`をメンバ変数にもち、`g.drawLine()`の前で`setColor()`することです。
6
4
 
7
-
8
-
9
5
  しかし現在の実装ですと再描画される(ウィンドウサイズを変更する等)と、すべて消えてしまうのでよろしくありません。
10
-
11
6
  通常は`BufferedImage`に描き、消えないように保持しておきます。
12
7
 
13
-
14
-
15
- AWTよりSwingのほうがよいと思いますので、「java swing お絵かき」等で検索してみてください。
8
+ AWTよりSwingのほうがよいと思いますので、「[java swing お絵かき](https://www.google.co.jp/search?q=java+swing+%E3%81%8A%E7%B5%B5%E3%81%8B%E3%81%8D)」等で検索してみてください。
16
-
17
-
18
9
 
19
10
  ---
20
11
 
21
-
22
-
23
12
  追記
24
-
25
13
  上記内容を分かったうえで、とりあえず動かすだけでよいならこんな感じ?
26
14
 
27
-
28
-
29
15
  ```Java
30
-
31
16
  import java.awt.Button;
32
-
33
17
  import java.awt.Color;
34
-
35
18
  import java.awt.Frame;
36
-
37
19
  import java.awt.Graphics;
38
-
39
20
  import java.awt.GridLayout;
40
-
41
21
  import java.awt.Panel;
42
-
43
22
  import java.awt.event.ActionEvent;
44
-
45
23
  import java.awt.event.ActionListener;
46
-
47
24
  import java.awt.event.MouseAdapter;
48
-
49
25
  import java.awt.event.MouseEvent;
50
-
51
26
  import java.awt.event.WindowAdapter;
52
-
53
27
  import java.awt.event.WindowEvent;
54
28
 
55
29
 
56
-
57
-
58
-
59
30
  class Draw extends Frame {
60
-
61
31
  int x, y, cx, cy;
62
-
63
32
  Color currentColor = Color.black;
64
33
 
65
-
66
-
67
34
  public Draw() {
68
-
69
35
  super("ペイント");
70
-
71
36
  addWindowListener(new SampleWindowListener());
72
-
73
37
  addMouseListener(new SampleMouseAdapter());
74
-
75
38
  addMouseMotionListener(new SampleMouseAdapter());
76
-
77
39
  }
78
40
 
79
-
80
-
81
41
  public void setColor(Color newColor) {
82
-
83
42
  currentColor = newColor;
84
-
85
43
  }
86
44
 
45
+ class SampleWindowListener extends WindowAdapter {
46
+ public void windowClosing(WindowEvent e) {
47
+ System.exit(0);
48
+ }
49
+ }
87
50
 
88
-
89
- class SampleWindowListener extends WindowAdapter {
51
+ class SampleMouseAdapter extends MouseAdapter {
90
-
91
- public void windowClosing(WindowEvent e) {
52
+ public void mousePressed(MouseEvent e) {
92
-
93
- System.exit(0);
53
+ x = e.getX();
94
-
54
+ y = e.getY();
95
55
  }
96
56
 
57
+ public void mouseDragged(MouseEvent e) {
58
+ Graphics g = getGraphics();
59
+ cx = e.getX();
60
+ cy = e.getY();
61
+ g.setColor(currentColor);
62
+ g.drawLine(x, y, cx, cy);
63
+ x = cx;
64
+ y = cy;
65
+ }
97
66
  }
98
-
99
-
100
-
101
- class SampleMouseAdapter extends MouseAdapter {
102
-
103
- public void mousePressed(MouseEvent e) {
104
-
105
- x = e.getX();
106
-
107
- y = e.getY();
108
-
109
- }
110
-
111
-
112
-
113
- public void mouseDragged(MouseEvent e) {
114
-
115
- Graphics g = getGraphics();
116
-
117
- cx = e.getX();
118
-
119
- cy = e.getY();
120
-
121
- g.setColor(currentColor);
122
-
123
- g.drawLine(x, y, cx, cy);
124
-
125
- x = cx;
126
-
127
- y = cy;
128
-
129
- }
130
-
131
- }
132
-
133
67
  }
134
68
 
135
69
 
136
-
137
-
138
-
139
70
  class Draw2 extends Draw {
140
-
141
71
  private Button Whitebt, Blackbt, Redbt, Bluebt, Greenbt, Clearbt;
142
-
143
72
  // Draw drawarea; // Drawを継承しているのだから自分自身
144
73
 
145
-
146
-
147
74
  public static void main(String[] args) {
148
-
149
75
  Draw2 ft = new Draw2();
150
-
151
76
  }
152
77
 
153
-
154
-
155
78
  Draw2() {
156
-
157
79
  setSize(500, 500);
158
-
159
-
160
80
 
161
81
  // drawarea = new Draw();
162
82
 
163
-
164
-
165
83
  Whitebt = new Button("White");
166
-
167
84
  // add(Whitebt); // pan.add(Whitebt); しているので2重登録
168
-
169
85
  Whitebt.addActionListener(new SampleActionListener());
170
-
171
86
  Blackbt = new Button("Black");
172
-
173
87
  Blackbt.addActionListener(new SampleActionListener());
174
-
175
88
  Redbt = new Button("Red");
176
-
177
89
  Redbt.addActionListener(new SampleActionListener());
178
-
179
90
  Bluebt = new Button("Blue");
180
-
181
91
  Bluebt.addActionListener(new SampleActionListener());
182
-
183
92
  Greenbt = new Button("Green");
184
-
185
93
  Greenbt.addActionListener(new SampleActionListener());
186
-
187
94
  Clearbt = new Button("Clear");
188
-
189
95
  Clearbt.addActionListener(new SampleActionListener());
190
96
 
191
-
192
-
193
97
  Panel pan = new Panel();
194
-
195
98
  pan.setLayout(new GridLayout(1, 6));
196
-
197
99
  pan.add(Whitebt);
198
-
199
100
  pan.add(Blackbt);
200
-
201
101
  pan.add(Redbt);
202
-
203
102
  pan.add(Bluebt);
204
-
205
103
  pan.add(Greenbt);
206
-
207
104
  pan.add(Clearbt);
208
105
 
209
-
210
-
211
106
  add("North", pan);
212
-
213
107
  setVisible(true);
214
-
215
108
  }
216
109
 
217
-
218
-
219
110
  class SampleActionListener implements ActionListener {
220
-
221
111
  public void actionPerformed(ActionEvent e) {
222
-
223
112
  if (e.getSource() == Whitebt) {
224
-
225
113
  setColor(Color.white);
226
-
227
114
  } else if (e.getSource() == Blackbt) {
228
-
229
115
  setColor(Color.black);
230
-
231
116
  } else if (e.getSource() == Redbt) {
232
-
233
117
  setColor(Color.red);
234
-
235
118
  } else if (e.getSource() == Bluebt) {
236
-
237
119
  setColor(Color.blue);
238
-
239
120
  } else if (e.getSource() == Greenbt) {
240
-
241
121
  setColor(Color.green);
242
-
243
122
  } else if (e.getSource() == Clearbt) {
244
-
245
123
  Graphics g = getGraphics();
246
-
247
124
  setColor(Color.white);
248
-
249
125
  g.fillRect(0, 0, getSize().width, getSize().height);
250
-
251
126
  setColor(Color.black);
252
-
253
127
  repaint();
254
-
255
128
  }
256
-
257
129
  }
258
-
259
130
  }
260
-
261
131
  }
262
-
263
132
  ```

1

追記 コード

2021/01/24 09:33

投稿

TN8001
TN8001

スコア9862

test CHANGED
@@ -13,3 +13,251 @@
13
13
 
14
14
 
15
15
  AWTよりSwingのほうがよいと思いますので、「java swing お絵かき」等で検索してみてください。
16
+
17
+
18
+
19
+ ---
20
+
21
+
22
+
23
+ 追記
24
+
25
+ 上記内容を分かったうえで、とりあえず動かすだけでよいならこんな感じ?
26
+
27
+
28
+
29
+ ```Java
30
+
31
+ import java.awt.Button;
32
+
33
+ import java.awt.Color;
34
+
35
+ import java.awt.Frame;
36
+
37
+ import java.awt.Graphics;
38
+
39
+ import java.awt.GridLayout;
40
+
41
+ import java.awt.Panel;
42
+
43
+ import java.awt.event.ActionEvent;
44
+
45
+ import java.awt.event.ActionListener;
46
+
47
+ import java.awt.event.MouseAdapter;
48
+
49
+ import java.awt.event.MouseEvent;
50
+
51
+ import java.awt.event.WindowAdapter;
52
+
53
+ import java.awt.event.WindowEvent;
54
+
55
+
56
+
57
+
58
+
59
+ class Draw extends Frame {
60
+
61
+ int x, y, cx, cy;
62
+
63
+ Color currentColor = Color.black;
64
+
65
+
66
+
67
+ public Draw() {
68
+
69
+ super("ペイント");
70
+
71
+ addWindowListener(new SampleWindowListener());
72
+
73
+ addMouseListener(new SampleMouseAdapter());
74
+
75
+ addMouseMotionListener(new SampleMouseAdapter());
76
+
77
+ }
78
+
79
+
80
+
81
+ public void setColor(Color newColor) {
82
+
83
+ currentColor = newColor;
84
+
85
+ }
86
+
87
+
88
+
89
+ class SampleWindowListener extends WindowAdapter {
90
+
91
+ public void windowClosing(WindowEvent e) {
92
+
93
+ System.exit(0);
94
+
95
+ }
96
+
97
+ }
98
+
99
+
100
+
101
+ class SampleMouseAdapter extends MouseAdapter {
102
+
103
+ public void mousePressed(MouseEvent e) {
104
+
105
+ x = e.getX();
106
+
107
+ y = e.getY();
108
+
109
+ }
110
+
111
+
112
+
113
+ public void mouseDragged(MouseEvent e) {
114
+
115
+ Graphics g = getGraphics();
116
+
117
+ cx = e.getX();
118
+
119
+ cy = e.getY();
120
+
121
+ g.setColor(currentColor);
122
+
123
+ g.drawLine(x, y, cx, cy);
124
+
125
+ x = cx;
126
+
127
+ y = cy;
128
+
129
+ }
130
+
131
+ }
132
+
133
+ }
134
+
135
+
136
+
137
+
138
+
139
+ class Draw2 extends Draw {
140
+
141
+ private Button Whitebt, Blackbt, Redbt, Bluebt, Greenbt, Clearbt;
142
+
143
+ // Draw drawarea; // Drawを継承しているのだから自分自身
144
+
145
+
146
+
147
+ public static void main(String[] args) {
148
+
149
+ Draw2 ft = new Draw2();
150
+
151
+ }
152
+
153
+
154
+
155
+ Draw2() {
156
+
157
+ setSize(500, 500);
158
+
159
+
160
+
161
+ // drawarea = new Draw();
162
+
163
+
164
+
165
+ Whitebt = new Button("White");
166
+
167
+ // add(Whitebt); // pan.add(Whitebt); しているので2重登録
168
+
169
+ Whitebt.addActionListener(new SampleActionListener());
170
+
171
+ Blackbt = new Button("Black");
172
+
173
+ Blackbt.addActionListener(new SampleActionListener());
174
+
175
+ Redbt = new Button("Red");
176
+
177
+ Redbt.addActionListener(new SampleActionListener());
178
+
179
+ Bluebt = new Button("Blue");
180
+
181
+ Bluebt.addActionListener(new SampleActionListener());
182
+
183
+ Greenbt = new Button("Green");
184
+
185
+ Greenbt.addActionListener(new SampleActionListener());
186
+
187
+ Clearbt = new Button("Clear");
188
+
189
+ Clearbt.addActionListener(new SampleActionListener());
190
+
191
+
192
+
193
+ Panel pan = new Panel();
194
+
195
+ pan.setLayout(new GridLayout(1, 6));
196
+
197
+ pan.add(Whitebt);
198
+
199
+ pan.add(Blackbt);
200
+
201
+ pan.add(Redbt);
202
+
203
+ pan.add(Bluebt);
204
+
205
+ pan.add(Greenbt);
206
+
207
+ pan.add(Clearbt);
208
+
209
+
210
+
211
+ add("North", pan);
212
+
213
+ setVisible(true);
214
+
215
+ }
216
+
217
+
218
+
219
+ class SampleActionListener implements ActionListener {
220
+
221
+ public void actionPerformed(ActionEvent e) {
222
+
223
+ if (e.getSource() == Whitebt) {
224
+
225
+ setColor(Color.white);
226
+
227
+ } else if (e.getSource() == Blackbt) {
228
+
229
+ setColor(Color.black);
230
+
231
+ } else if (e.getSource() == Redbt) {
232
+
233
+ setColor(Color.red);
234
+
235
+ } else if (e.getSource() == Bluebt) {
236
+
237
+ setColor(Color.blue);
238
+
239
+ } else if (e.getSource() == Greenbt) {
240
+
241
+ setColor(Color.green);
242
+
243
+ } else if (e.getSource() == Clearbt) {
244
+
245
+ Graphics g = getGraphics();
246
+
247
+ setColor(Color.white);
248
+
249
+ g.fillRect(0, 0, getSize().width, getSize().height);
250
+
251
+ setColor(Color.black);
252
+
253
+ repaint();
254
+
255
+ }
256
+
257
+ }
258
+
259
+ }
260
+
261
+ }
262
+
263
+ ```