-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscribble.java
276 lines (225 loc) · 8 KB
/
scribble.java
1
2
3
4
5
6
7
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.*;
import java.io.FileInputStream;
import java.io.IOException;
public class scribble extends Component implements ActionListener, ChangeListener {
JFrame frame;
JMenuBar mb;
JMenu menu;
JButton colorButton, erasebutt, setBack;
JToolBar toolBar, toolBar2;
ScribblePane2 scribblePane;
Container contentPane;
JSlider slider;
int stoke = 10;
public scribble() throws IOException {
frame = new JFrame("SCRIBBLE");
frame.setBounds(250, 110, 1000, 670);
frame.setVisible(true);
frame.setLayout(null);
frame.setDefaultCloseOperation(frame.DO_NOTHING_ON_CLOSE);
contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
mb = new JMenuBar();
menu = new JMenu("Menu");
mb.add(menu);
frame.setJMenuBar(mb);
toolBar = new JToolBar();
toolBar.setBackground(new Color(221, 221, 221));
toolBar.setOrientation(SwingConstants.VERTICAL);
contentPane.add(toolBar, BorderLayout.WEST);
toolBar2 = new JToolBar();
toolBar2.setBackground(new Color(221, 221, 221));
toolBar2.setOrientation(SwingConstants.HORIZONTAL);
contentPane.add(toolBar2, BorderLayout.NORTH);
Action black = new scribble.ColorAction(Color.black);
Action red = new scribble.ColorAction(Color.red);
Action blue = new scribble.ColorAction(Color.blue);
Action yellow = new scribble.ColorAction(Color.yellow);
Action green = new scribble.ColorAction(Color.green);
Action pink = new scribble.ColorAction(Color.MAGENTA);
Action grey = new scribble.ColorAction(Color.darkGray);
Action brown = new scribble.ColorAction(new Color(138, 76, 35));
Action orange = new scribble.ColorAction(Color.orange);
Action white = new scribble.ColorAction(Color.WHITE);
Action clear = new ClearAction();
Action quit = new quitAction();
erasebutt = new JButton(" Erase ");
setBack = new JButton(" Background ");
slider = new JSlider(0, 50, 10);
slider.setMinorTickSpacing(2);
slider.setMajorTickSpacing(10);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.setMaximumSize(new Dimension(350, Short.MAX_VALUE));
toolBar.add(pink);
toolBar.add(red);
toolBar.add(orange);
toolBar.add(yellow);
toolBar.add(green);
toolBar.add(blue);
toolBar.add(brown);
toolBar.add(grey);
toolBar.add(black);
toolBar.add(white);
toolBar2.add(slider);
toolBar2.addSeparator();
toolBar2.add(erasebutt);
toolBar2.addSeparator();
toolBar2.add(setBack);
toolBar2.addSeparator();
toolBar2.add(clear);
toolBar2.addSeparator();
toolBar2.add(quit);
menu.add(clear);
menu.addSeparator();
menu.add(quit);
colorButton = new JButton();
Image img = ImageIO.read(new FileInputStream("D:\\colorButton.PNG"));
colorButton.setIcon(new ImageIcon(img));
toolBar.add(colorButton);
scribblePane = new ScribblePane2();
scribblePane.setBackground(Color.white);
scribblePane.setBounds(0, 200, 1000, 400);
contentPane.add(scribblePane, BorderLayout.CENTER);
//ActonListeners
colorButton.addActionListener(this);
erasebutt.addActionListener(this);
setBack.addActionListener(this);
slider.addChangeListener(this);
}
//main method
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
try {
new scribble();
} catch (IOException e) {
e.printStackTrace();
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == colorButton) {
Color color = JColorChooser.showDialog(scribble.this,
"Select Drawing Color", scribblePane.getColor());
if (color != null)
scribblePane.setColor(color);
}
if (e.getSource() == erasebutt) {
scribblePane.color = Color.white;
}
if (e.getSource() == setBack) {
scribblePane.setBackground(scribblePane.color);
}
}
@Override
public void stateChanged(ChangeEvent e) {
stoke = slider.getValue();
}
class ScribblePane2 extends JPanel {
public ScribblePane2() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
moveto(e.getX(), e.getY());
requestFocus();
}
});
//to draw on current x and y axis
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
lineto(e.getX(), e.getY());
}
});
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_C) {
clear();
}
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
quit();
}
}
});
}
protected int last_x, last_y;
public void moveto(int x, int y) {
last_x = x;
last_y = y;
}
public void lineto(int x, int y) {
Graphics2D g = (Graphics2D) getGraphics();
g.setColor(color);
g.setStroke(new BasicStroke(stoke));
g.drawLine(last_x, last_y, x, y);
moveto(x, y);
}
public void clear() {
scribblePane.setBackground(Color.white);
repaint();
}
public void quit() {
int response = JOptionPane.showConfirmDialog(scribble.this,
"Really Quit?");
if (response == JOptionPane.YES_NO_OPTION)
System.exit(0);
}
//to change color
Color color = Color.black;
public void setColor(Color color) {
this.color = color;
}
public Color getColor() {
return color;
}
}
class ClearAction extends AbstractAction {
public ClearAction() {
super(" Clear ");
}
@Override
public void actionPerformed(ActionEvent e) {
scribblePane.clear();
}
}
class quitAction extends AbstractAction {
public quitAction() {
super(" Quit ");
}
@Override
public void actionPerformed(ActionEvent e) {
scribblePane.quit();
}
}
class ColorAction extends AbstractAction {
Color color;
public ColorAction(Color color) {
this.color = color;
putValue(Action.LARGE_ICON_KEY, new scribble.ColorIcon(color)); // specify icon
}
@Override
public void actionPerformed(ActionEvent e) {
scribblePane.setColor(color);
}
}
static class ColorIcon implements Icon {
Color color;
public ColorIcon(Color color) {
this.color = color;
}
public int getIconHeight() {
return 25;
}
public int getIconWidth() {
return 25;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(color);
g.fillRect(x, y, 25, 25);
}
}
}