回答編集履歴
2
見直しキャンペーン中
answer
CHANGED
@@ -1,132 +1,132 @@
|
|
1
|
-
直接的な原因は`getGraphics()`したものは、`Foreground`の色が設定されます。
|
2
|
-
そのため`setColor()`したとしても`getGraphics()`するたびに、黒に戻ってしまいます。
|
3
|
-
方針としては`Color`をメンバ変数にもち、`g.drawLine()`の前で`setColor()`することです。
|
4
|
-
|
5
|
-
しかし現在の実装ですと再描画される(ウィンドウサイズを変更する等)と、すべて消えてしまうのでよろしくありません。
|
6
|
-
通常は`BufferedImage`に描き、消えないように保持しておきます。
|
7
|
-
|
8
|
-
AWTよりSwingのほうがよいと思いますので、「java swing お絵かき」等で検索してみてください。
|
9
|
-
|
10
|
-
---
|
11
|
-
|
12
|
-
追記
|
13
|
-
上記内容を分かったうえで、とりあえず動かすだけでよいならこんな感じ?
|
14
|
-
|
15
|
-
```Java
|
16
|
-
import java.awt.Button;
|
17
|
-
import java.awt.Color;
|
18
|
-
import java.awt.Frame;
|
19
|
-
import java.awt.Graphics;
|
20
|
-
import java.awt.GridLayout;
|
21
|
-
import java.awt.Panel;
|
22
|
-
import java.awt.event.ActionEvent;
|
23
|
-
import java.awt.event.ActionListener;
|
24
|
-
import java.awt.event.MouseAdapter;
|
25
|
-
import java.awt.event.MouseEvent;
|
26
|
-
import java.awt.event.WindowAdapter;
|
27
|
-
import java.awt.event.WindowEvent;
|
28
|
-
|
29
|
-
|
30
|
-
class Draw extends Frame {
|
31
|
-
int x, y, cx, cy;
|
32
|
-
Color currentColor = Color.black;
|
33
|
-
|
34
|
-
public Draw() {
|
35
|
-
super("ペイント");
|
36
|
-
addWindowListener(new SampleWindowListener());
|
37
|
-
addMouseListener(new SampleMouseAdapter());
|
38
|
-
addMouseMotionListener(new SampleMouseAdapter());
|
39
|
-
}
|
40
|
-
|
41
|
-
public void setColor(Color newColor) {
|
42
|
-
currentColor = newColor;
|
43
|
-
}
|
44
|
-
|
45
|
-
class SampleWindowListener extends WindowAdapter {
|
46
|
-
public void windowClosing(WindowEvent e) {
|
47
|
-
System.exit(0);
|
48
|
-
}
|
49
|
-
}
|
50
|
-
|
51
|
-
class SampleMouseAdapter extends MouseAdapter {
|
52
|
-
public void mousePressed(MouseEvent e) {
|
53
|
-
x = e.getX();
|
54
|
-
y = e.getY();
|
55
|
-
}
|
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
|
-
}
|
66
|
-
}
|
67
|
-
}
|
68
|
-
|
69
|
-
|
70
|
-
class Draw2 extends Draw {
|
71
|
-
private Button Whitebt, Blackbt, Redbt, Bluebt, Greenbt, Clearbt;
|
72
|
-
// Draw drawarea; // Drawを継承しているのだから自分自身
|
73
|
-
|
74
|
-
public static void main(String[] args) {
|
75
|
-
Draw2 ft = new Draw2();
|
76
|
-
}
|
77
|
-
|
78
|
-
Draw2() {
|
79
|
-
setSize(500, 500);
|
80
|
-
|
81
|
-
// drawarea = new Draw();
|
82
|
-
|
83
|
-
Whitebt = new Button("White");
|
84
|
-
// add(Whitebt); // pan.add(Whitebt); しているので2重登録
|
85
|
-
Whitebt.addActionListener(new SampleActionListener());
|
86
|
-
Blackbt = new Button("Black");
|
87
|
-
Blackbt.addActionListener(new SampleActionListener());
|
88
|
-
Redbt = new Button("Red");
|
89
|
-
Redbt.addActionListener(new SampleActionListener());
|
90
|
-
Bluebt = new Button("Blue");
|
91
|
-
Bluebt.addActionListener(new SampleActionListener());
|
92
|
-
Greenbt = new Button("Green");
|
93
|
-
Greenbt.addActionListener(new SampleActionListener());
|
94
|
-
Clearbt = new Button("Clear");
|
95
|
-
Clearbt.addActionListener(new SampleActionListener());
|
96
|
-
|
97
|
-
Panel pan = new Panel();
|
98
|
-
pan.setLayout(new GridLayout(1, 6));
|
99
|
-
pan.add(Whitebt);
|
100
|
-
pan.add(Blackbt);
|
101
|
-
pan.add(Redbt);
|
102
|
-
pan.add(Bluebt);
|
103
|
-
pan.add(Greenbt);
|
104
|
-
pan.add(Clearbt);
|
105
|
-
|
106
|
-
add("North", pan);
|
107
|
-
setVisible(true);
|
108
|
-
}
|
109
|
-
|
110
|
-
class SampleActionListener implements ActionListener {
|
111
|
-
public void actionPerformed(ActionEvent e) {
|
112
|
-
if (e.getSource() == Whitebt) {
|
113
|
-
setColor(Color.white);
|
114
|
-
} else if (e.getSource() == Blackbt) {
|
115
|
-
setColor(Color.black);
|
116
|
-
} else if (e.getSource() == Redbt) {
|
117
|
-
setColor(Color.red);
|
118
|
-
} else if (e.getSource() == Bluebt) {
|
119
|
-
setColor(Color.blue);
|
120
|
-
} else if (e.getSource() == Greenbt) {
|
121
|
-
setColor(Color.green);
|
122
|
-
} else if (e.getSource() == Clearbt) {
|
123
|
-
Graphics g = getGraphics();
|
124
|
-
setColor(Color.white);
|
125
|
-
g.fillRect(0, 0, getSize().width, getSize().height);
|
126
|
-
setColor(Color.black);
|
127
|
-
repaint();
|
128
|
-
}
|
129
|
-
}
|
130
|
-
}
|
131
|
-
}
|
1
|
+
直接的な原因は`getGraphics()`したものは、`Foreground`の色が設定されます。
|
2
|
+
そのため`setColor()`したとしても`getGraphics()`するたびに、黒に戻ってしまいます。
|
3
|
+
方針としては`Color`をメンバ変数にもち、`g.drawLine()`の前で`setColor()`することです。
|
4
|
+
|
5
|
+
しかし現在の実装ですと再描画される(ウィンドウサイズを変更する等)と、すべて消えてしまうのでよろしくありません。
|
6
|
+
通常は`BufferedImage`に描き、消えないように保持しておきます。
|
7
|
+
|
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)」等で検索してみてください。
|
9
|
+
|
10
|
+
---
|
11
|
+
|
12
|
+
追記
|
13
|
+
上記内容を分かったうえで、とりあえず動かすだけでよいならこんな感じ?
|
14
|
+
|
15
|
+
```Java
|
16
|
+
import java.awt.Button;
|
17
|
+
import java.awt.Color;
|
18
|
+
import java.awt.Frame;
|
19
|
+
import java.awt.Graphics;
|
20
|
+
import java.awt.GridLayout;
|
21
|
+
import java.awt.Panel;
|
22
|
+
import java.awt.event.ActionEvent;
|
23
|
+
import java.awt.event.ActionListener;
|
24
|
+
import java.awt.event.MouseAdapter;
|
25
|
+
import java.awt.event.MouseEvent;
|
26
|
+
import java.awt.event.WindowAdapter;
|
27
|
+
import java.awt.event.WindowEvent;
|
28
|
+
|
29
|
+
|
30
|
+
class Draw extends Frame {
|
31
|
+
int x, y, cx, cy;
|
32
|
+
Color currentColor = Color.black;
|
33
|
+
|
34
|
+
public Draw() {
|
35
|
+
super("ペイント");
|
36
|
+
addWindowListener(new SampleWindowListener());
|
37
|
+
addMouseListener(new SampleMouseAdapter());
|
38
|
+
addMouseMotionListener(new SampleMouseAdapter());
|
39
|
+
}
|
40
|
+
|
41
|
+
public void setColor(Color newColor) {
|
42
|
+
currentColor = newColor;
|
43
|
+
}
|
44
|
+
|
45
|
+
class SampleWindowListener extends WindowAdapter {
|
46
|
+
public void windowClosing(WindowEvent e) {
|
47
|
+
System.exit(0);
|
48
|
+
}
|
49
|
+
}
|
50
|
+
|
51
|
+
class SampleMouseAdapter extends MouseAdapter {
|
52
|
+
public void mousePressed(MouseEvent e) {
|
53
|
+
x = e.getX();
|
54
|
+
y = e.getY();
|
55
|
+
}
|
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
|
+
}
|
66
|
+
}
|
67
|
+
}
|
68
|
+
|
69
|
+
|
70
|
+
class Draw2 extends Draw {
|
71
|
+
private Button Whitebt, Blackbt, Redbt, Bluebt, Greenbt, Clearbt;
|
72
|
+
// Draw drawarea; // Drawを継承しているのだから自分自身
|
73
|
+
|
74
|
+
public static void main(String[] args) {
|
75
|
+
Draw2 ft = new Draw2();
|
76
|
+
}
|
77
|
+
|
78
|
+
Draw2() {
|
79
|
+
setSize(500, 500);
|
80
|
+
|
81
|
+
// drawarea = new Draw();
|
82
|
+
|
83
|
+
Whitebt = new Button("White");
|
84
|
+
// add(Whitebt); // pan.add(Whitebt); しているので2重登録
|
85
|
+
Whitebt.addActionListener(new SampleActionListener());
|
86
|
+
Blackbt = new Button("Black");
|
87
|
+
Blackbt.addActionListener(new SampleActionListener());
|
88
|
+
Redbt = new Button("Red");
|
89
|
+
Redbt.addActionListener(new SampleActionListener());
|
90
|
+
Bluebt = new Button("Blue");
|
91
|
+
Bluebt.addActionListener(new SampleActionListener());
|
92
|
+
Greenbt = new Button("Green");
|
93
|
+
Greenbt.addActionListener(new SampleActionListener());
|
94
|
+
Clearbt = new Button("Clear");
|
95
|
+
Clearbt.addActionListener(new SampleActionListener());
|
96
|
+
|
97
|
+
Panel pan = new Panel();
|
98
|
+
pan.setLayout(new GridLayout(1, 6));
|
99
|
+
pan.add(Whitebt);
|
100
|
+
pan.add(Blackbt);
|
101
|
+
pan.add(Redbt);
|
102
|
+
pan.add(Bluebt);
|
103
|
+
pan.add(Greenbt);
|
104
|
+
pan.add(Clearbt);
|
105
|
+
|
106
|
+
add("North", pan);
|
107
|
+
setVisible(true);
|
108
|
+
}
|
109
|
+
|
110
|
+
class SampleActionListener implements ActionListener {
|
111
|
+
public void actionPerformed(ActionEvent e) {
|
112
|
+
if (e.getSource() == Whitebt) {
|
113
|
+
setColor(Color.white);
|
114
|
+
} else if (e.getSource() == Blackbt) {
|
115
|
+
setColor(Color.black);
|
116
|
+
} else if (e.getSource() == Redbt) {
|
117
|
+
setColor(Color.red);
|
118
|
+
} else if (e.getSource() == Bluebt) {
|
119
|
+
setColor(Color.blue);
|
120
|
+
} else if (e.getSource() == Greenbt) {
|
121
|
+
setColor(Color.green);
|
122
|
+
} else if (e.getSource() == Clearbt) {
|
123
|
+
Graphics g = getGraphics();
|
124
|
+
setColor(Color.white);
|
125
|
+
g.fillRect(0, 0, getSize().width, getSize().height);
|
126
|
+
setColor(Color.black);
|
127
|
+
repaint();
|
128
|
+
}
|
129
|
+
}
|
130
|
+
}
|
131
|
+
}
|
132
132
|
```
|
1
追記 コード
answer
CHANGED
@@ -5,4 +5,128 @@
|
|
5
5
|
しかし現在の実装ですと再描画される(ウィンドウサイズを変更する等)と、すべて消えてしまうのでよろしくありません。
|
6
6
|
通常は`BufferedImage`に描き、消えないように保持しておきます。
|
7
7
|
|
8
|
-
AWTよりSwingのほうがよいと思いますので、「java swing お絵かき」等で検索してみてください。
|
8
|
+
AWTよりSwingのほうがよいと思いますので、「java swing お絵かき」等で検索してみてください。
|
9
|
+
|
10
|
+
---
|
11
|
+
|
12
|
+
追記
|
13
|
+
上記内容を分かったうえで、とりあえず動かすだけでよいならこんな感じ?
|
14
|
+
|
15
|
+
```Java
|
16
|
+
import java.awt.Button;
|
17
|
+
import java.awt.Color;
|
18
|
+
import java.awt.Frame;
|
19
|
+
import java.awt.Graphics;
|
20
|
+
import java.awt.GridLayout;
|
21
|
+
import java.awt.Panel;
|
22
|
+
import java.awt.event.ActionEvent;
|
23
|
+
import java.awt.event.ActionListener;
|
24
|
+
import java.awt.event.MouseAdapter;
|
25
|
+
import java.awt.event.MouseEvent;
|
26
|
+
import java.awt.event.WindowAdapter;
|
27
|
+
import java.awt.event.WindowEvent;
|
28
|
+
|
29
|
+
|
30
|
+
class Draw extends Frame {
|
31
|
+
int x, y, cx, cy;
|
32
|
+
Color currentColor = Color.black;
|
33
|
+
|
34
|
+
public Draw() {
|
35
|
+
super("ペイント");
|
36
|
+
addWindowListener(new SampleWindowListener());
|
37
|
+
addMouseListener(new SampleMouseAdapter());
|
38
|
+
addMouseMotionListener(new SampleMouseAdapter());
|
39
|
+
}
|
40
|
+
|
41
|
+
public void setColor(Color newColor) {
|
42
|
+
currentColor = newColor;
|
43
|
+
}
|
44
|
+
|
45
|
+
class SampleWindowListener extends WindowAdapter {
|
46
|
+
public void windowClosing(WindowEvent e) {
|
47
|
+
System.exit(0);
|
48
|
+
}
|
49
|
+
}
|
50
|
+
|
51
|
+
class SampleMouseAdapter extends MouseAdapter {
|
52
|
+
public void mousePressed(MouseEvent e) {
|
53
|
+
x = e.getX();
|
54
|
+
y = e.getY();
|
55
|
+
}
|
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
|
+
}
|
66
|
+
}
|
67
|
+
}
|
68
|
+
|
69
|
+
|
70
|
+
class Draw2 extends Draw {
|
71
|
+
private Button Whitebt, Blackbt, Redbt, Bluebt, Greenbt, Clearbt;
|
72
|
+
// Draw drawarea; // Drawを継承しているのだから自分自身
|
73
|
+
|
74
|
+
public static void main(String[] args) {
|
75
|
+
Draw2 ft = new Draw2();
|
76
|
+
}
|
77
|
+
|
78
|
+
Draw2() {
|
79
|
+
setSize(500, 500);
|
80
|
+
|
81
|
+
// drawarea = new Draw();
|
82
|
+
|
83
|
+
Whitebt = new Button("White");
|
84
|
+
// add(Whitebt); // pan.add(Whitebt); しているので2重登録
|
85
|
+
Whitebt.addActionListener(new SampleActionListener());
|
86
|
+
Blackbt = new Button("Black");
|
87
|
+
Blackbt.addActionListener(new SampleActionListener());
|
88
|
+
Redbt = new Button("Red");
|
89
|
+
Redbt.addActionListener(new SampleActionListener());
|
90
|
+
Bluebt = new Button("Blue");
|
91
|
+
Bluebt.addActionListener(new SampleActionListener());
|
92
|
+
Greenbt = new Button("Green");
|
93
|
+
Greenbt.addActionListener(new SampleActionListener());
|
94
|
+
Clearbt = new Button("Clear");
|
95
|
+
Clearbt.addActionListener(new SampleActionListener());
|
96
|
+
|
97
|
+
Panel pan = new Panel();
|
98
|
+
pan.setLayout(new GridLayout(1, 6));
|
99
|
+
pan.add(Whitebt);
|
100
|
+
pan.add(Blackbt);
|
101
|
+
pan.add(Redbt);
|
102
|
+
pan.add(Bluebt);
|
103
|
+
pan.add(Greenbt);
|
104
|
+
pan.add(Clearbt);
|
105
|
+
|
106
|
+
add("North", pan);
|
107
|
+
setVisible(true);
|
108
|
+
}
|
109
|
+
|
110
|
+
class SampleActionListener implements ActionListener {
|
111
|
+
public void actionPerformed(ActionEvent e) {
|
112
|
+
if (e.getSource() == Whitebt) {
|
113
|
+
setColor(Color.white);
|
114
|
+
} else if (e.getSource() == Blackbt) {
|
115
|
+
setColor(Color.black);
|
116
|
+
} else if (e.getSource() == Redbt) {
|
117
|
+
setColor(Color.red);
|
118
|
+
} else if (e.getSource() == Bluebt) {
|
119
|
+
setColor(Color.blue);
|
120
|
+
} else if (e.getSource() == Greenbt) {
|
121
|
+
setColor(Color.green);
|
122
|
+
} else if (e.getSource() == Clearbt) {
|
123
|
+
Graphics g = getGraphics();
|
124
|
+
setColor(Color.white);
|
125
|
+
g.fillRect(0, 0, getSize().width, getSize().height);
|
126
|
+
setColor(Color.black);
|
127
|
+
repaint();
|
128
|
+
}
|
129
|
+
}
|
130
|
+
}
|
131
|
+
}
|
132
|
+
```
|