-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaintingComponent.java
More file actions
538 lines (496 loc) · 15 KB
/
PaintingComponent.java
File metadata and controls
538 lines (496 loc) · 15 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
/**
* A component that allows the user to draw on the screen using various tools.
*
* @author Tom Brannan
* @date May 3, 2013
*/
public class PaintingComponent extends JPanel {
public static final byte ERASER = 0;
public static final byte PENCIL = 1;
public static final byte LINE = 2;
public static final byte BOX = 3;
public static final byte ELLIPSE = 4;
public static final byte ISOSCELES = 5;
public static final byte RIGHT_TRIANGLE = 6;
public static final byte DIAMOND = 7;
public static final byte PENTAGON = 8;
public static final byte LINE_REPEATER = 9;
private final JFileChooser jfc = new JFileChooser("C:/");
private final String saveExtension = "png";
private Color primaryColor = Color.BLACK;
private Color secondaryColor = Color.YELLOW;
private Color bgColor = Color.WHITE;
private boolean antialias = true;
private byte drawMode = PENCIL;
private Graphics2D g2;
// the (x,y) coordinates of points upon clicking and dragging
private int currentX, currentY, oldX, oldY;
private float lineThickness = 1.0f;
private boolean fill = true; // whether or not to fill shape with 2nd color
private BufferedImage image, prevImage;
/**
* Initializes the mouse listeners for the component
*/
public PaintingComponent() {
addMouseListener(new PaintingComponent.ClickListener());
addMouseMotionListener(new PaintingComponent.DragListener());
}
/**
* ClickListener is used only for mousePressed
*/
private class ClickListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
// Only continue if the graphics context exists
if (g2 != null) {
g2.setColor(primaryColor);
oldX = e.getX();
oldY = e.getY();
// The pencil and eraser do not need to preserve the image
// Every other tool requires a previousImage to be saved
// and repainted upon dragging.
if (drawMode > 1) {
prevImage = deepCopy(image);
}
// If the eraser is selected, we can erase with one click
if (drawMode == 0) {
g2.setColor(bgColor);
int ewidth = (int) (10 * lineThickness);
g2.setColor(bgColor);
g2.fill(new Rectangle(oldX - ewidth / 2, oldY - ewidth / 2, ewidth, ewidth));
repaint();
}
}
}
}
/**
* DragListener accounts for mouse motion while a button is held down. The
* selected tool determines what happens. The eraser draws a white square and
* the pencil draws a short line. The line tool draws a line and repaints the
* preserved image each time so the user can see the line being drawn. The line
* repeater tool functions just like the line tool but doesn't erase the
* previous line. The rest of the tools draw shapes which may or may not be
* filled in with a second color.
*/
private class DragListener extends MouseMotionAdapter {
@Override
public void mouseDragged(MouseEvent e) {
if (g2 != null) {
int[] xpts;
int[] ypts;
switch (drawMode) {
case ERASER:
currentX = e.getX();
currentY = e.getY();
int ewidth = (int) (10 * lineThickness);
g2.setColor(bgColor);
g2.fill(new Rectangle(currentX - ewidth / 2, currentY - ewidth / 2, ewidth, ewidth));
repaint();
break;
case PENCIL:
repaint();
currentX = e.getX();
currentY = e.getY();
g2.setStroke(new BasicStroke(lineThickness));
g2.drawLine(oldX, oldY, currentX, currentY);
oldX = currentX;
oldY = currentY;
break;
case LINE:
clear();
currentX = e.getX();
currentY = e.getY();
g2.drawImage(prevImage, 0, 0, null);
g2.drawLine(oldX, oldY, currentX, currentY);
repaint();
break;
case BOX:
clear();
currentX = e.getX();
currentY = e.getY();
g2.drawImage(prevImage, 0, 0, null);
if (currentX >= oldX && currentY >= oldY) {
if (fill) {
g2.setColor(secondaryColor);
g2.fill(new Rectangle(oldX, oldY, currentX - oldX, currentY - oldY));
g2.setColor(primaryColor);
}
g2.draw(new Rectangle(oldX, oldY, currentX - oldX, currentY - oldY));
}
if (currentX >= oldX && currentY <= oldY) {
if (fill) {
g2.setColor(secondaryColor);
g2.fill(new Rectangle(oldX, currentY, currentX - oldX, oldY - currentY));
g2.setColor(primaryColor);
}
g2.draw(new Rectangle(oldX, currentY, currentX - oldX, oldY - currentY));
}
if (currentX <= oldX && currentY >= oldY) {
if (fill) {
g2.setColor(secondaryColor);
g2.fill(new Rectangle(currentX, oldY, oldX - currentX, currentY - oldY));
g2.setColor(primaryColor);
}
g2.draw(new Rectangle(currentX, oldY, oldX - currentX, currentY - oldY));
}
if (currentY <= oldY && currentY <= oldY) {
if (fill) {
g2.setColor(secondaryColor);
g2.fill(new Rectangle(currentX, currentY, oldX - currentX, oldY - currentY));
g2.setColor(primaryColor);
}
g2.draw(new Rectangle(currentX, currentY, oldX - currentX, oldY - currentY));
}
repaint();
break;
case ELLIPSE:
clear();
currentX = e.getX();
currentY = e.getY();
g2.drawImage(prevImage, 0, 0, null);
if (currentX >= oldX && currentY >= oldY) {
if (fill) {
g2.setColor(secondaryColor);
g2.fill(new Ellipse2D.Double(oldX, oldY, currentX - oldX, currentY - oldY));
g2.setColor(primaryColor);
}
g2.draw(new Ellipse2D.Double(oldX, oldY, currentX - oldX, currentY - oldY));
}
if (currentX >= oldX && currentY <= oldY) {
if (fill) {
g2.setColor(secondaryColor);
g2.fill(new Ellipse2D.Double(oldX, currentY, currentX - oldX, oldY - currentY));
g2.setColor(primaryColor);
}
g2.draw(new Ellipse2D.Double(oldX, currentY, currentX - oldX, oldY - currentY));
}
if (currentX <= oldX && currentY >= oldY) {
if (fill) {
g2.setColor(secondaryColor);
g2.fill(new Ellipse2D.Double(currentX, oldY, oldX - currentX, currentY - oldY));
g2.setColor(primaryColor);
}
g2.draw(new Ellipse2D.Double(currentX, oldY, oldX - currentX, currentY - oldY));
}
if (currentY <= oldY && currentY <= oldY) {
if (fill) {
g2.setColor(secondaryColor);
g2.fill(new Ellipse2D.Double(currentX, currentY, oldX - currentX, oldY - currentY));
g2.setColor(primaryColor);
}
g2.draw(new Ellipse2D.Double(currentX, currentY, oldX - currentX, oldY - currentY));
}
repaint();
break;
case ISOSCELES:
clear();
currentX = e.getX();
currentY = e.getY();
g2.drawImage(prevImage, 0, 0, null);
xpts = new int[3];
ypts = new int[3];
xpts[0] = oldX;
ypts[0] = oldY;
xpts[1] = (oldX + currentX) / 2;
ypts[1] = currentY;
xpts[2] = currentX;
ypts[2] = oldY;
if (fill) {
g2.setColor(secondaryColor);
g2.fillPolygon(xpts, ypts, 3);
g2.setColor(primaryColor);
}
g2.draw(new Polygon(xpts, ypts, 3));
repaint();
break;
case RIGHT_TRIANGLE:
clear();
currentX = e.getX();
currentY = e.getY();
g2.drawImage(prevImage, 0, 0, null);
xpts = new int[3];
ypts = new int[3];
xpts[0] = oldX;
ypts[0] = oldY;
xpts[1] = (oldX);
ypts[1] = currentY;
xpts[2] = currentX;
ypts[2] = currentY;
if (fill) {
g2.setColor(secondaryColor);
g2.fillPolygon(xpts, ypts, 3);
g2.setColor(primaryColor);
}
g2.draw(new Polygon(xpts, ypts, 3));
repaint();
break;
case DIAMOND:
clear();
currentX = e.getX();
currentY = e.getY();
g2.drawImage(prevImage, 0, 0, null);
xpts = new int[4];
ypts = new int[4];
xpts[0] = (currentX + oldX) / 2;
ypts[0] = oldY;
xpts[1] = currentX;
ypts[1] = (currentY + oldY) / 2;
xpts[2] = (currentX + oldX) / 2;
ypts[2] = currentY;
xpts[3] = oldX;
ypts[3] = (currentY + oldY) / 2;
if (fill) {
g2.setColor(secondaryColor);
g2.fillPolygon(xpts, ypts, 4);
g2.setColor(primaryColor);
}
g2.draw(new Polygon(xpts, ypts, 4));
repaint();
break;
// The pentagon points were found
// geometrically on pencil and paper :)
case PENTAGON:
clear();
currentX = e.getX();
currentY = e.getY();
g2.drawImage(prevImage, 0, 0, null);
xpts = new int[5];
ypts = new int[5];
int a = (int) (Math.abs(currentX - oldX) / 2);
if (currentX >= oldX) {
xpts[0] = oldX + a;
ypts[0] = oldY;
xpts[1] = currentX;
ypts[1] = oldY + (int) ((tan(36) / 2 * (currentY - oldY)));
xpts[2] = (currentX - (int) ((2 * a - a * tan(36)) * tan(18)));
ypts[2] = currentY;
xpts[3] = (oldX + (int) ((2 * a - a * tan(36)) * tan(18)));
ypts[3] = currentY;
xpts[4] = oldX;
ypts[4] = oldY + (int) ((tan(36) / 2 * (currentY - oldY)));
} else {
xpts[0] = oldX - a;
ypts[0] = oldY;
xpts[1] = oldX;
ypts[1] = oldY + (int) ((tan(36) / 2 * (currentY - oldY)));
xpts[2] = oldX - (int) ((2 * a - a * tan(36)) * tan(18));
ypts[2] = currentY;
xpts[3] = currentX + (int) ((2 * a - a * tan(36)) * tan(18));
ypts[3] = currentY;
xpts[4] = currentX;
ypts[4] = oldY + (int) ((tan(36) / 2 * (currentY - oldY)));
}
if (fill) {
g2.setColor(secondaryColor);
g2.fillPolygon(xpts, ypts, 5);
g2.setColor(primaryColor);
}
g2.draw(new Polygon(xpts, ypts, 5));
repaint();
break;
case LINE_REPEATER:
currentX = e.getX();
currentY = e.getY();
g2.drawLine(oldX, oldY, currentX, currentY);
repaint();
break;
}
}
}
}
// helper method to calculate tangent of an angle in degrees
private double tan(int degrees) {
return Math.tan(degrees * Math.PI / 180);
}
/**
* Toggles fill (whether a shape is filled in by the secondary color)
*
* @param fill whether or not to fill
*/
public void setFill(boolean fill) {
this.fill = fill;
System.out.println("Fill: " + (fill ? "ON" : "OFF"));
}
/**
* Sets the draw mode.
*
* @param drawMode the draw mode
*/
public void setDrawMode(byte drawMode) {
System.out.print("Draw mode set to ");
String s = "";
switch (drawMode) {
case ERASER:
s = "Eraser.";
break;
case PENCIL:
s = "Pencil.";
break;
case LINE:
s = "Line.";
break;
case BOX:
s = "Box.";
break;
case ELLIPSE:
s = "Ellipse.";
break;
case ISOSCELES:
s = "Isosceles Triangle.";
break;
case RIGHT_TRIANGLE:
s = "Right Triangle.";
break;
case DIAMOND:
s = "Diamond.";
break;
case PENTAGON:
s = "Pentagon.";
break;
case LINE_REPEATER:
s = "Line Reapter.";
break;
}
System.out.println(s);
this.drawMode = drawMode;
}
/**
* Sets the anti-aliasing either on or off
*
* @param b
*/
public void setAntiAliasing(boolean b) {
antialias = b;
if (b) {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
} else {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
}
System.out.println("Anti-Aliasing: " + (b ? "ON" : "OFF"));
}
@Override
public void paintComponent(Graphics g) {
// If the image is null, create a blank image
if (image == null) {
image = (BufferedImage) createImage(this.getSize().width, this.getSize().height);
g2 = (Graphics2D) image.getGraphics();
g2.setColor(primaryColor);
setAntiAliasing(antialias);
g2.setStroke(new BasicStroke(lineThickness));
clear();
}
g2.setColor(primaryColor);
g.drawImage(image, 0, 0, null);
}
// This code was taken from:
// http://stackoverflow.com/questions/3514158/how-do-you-clone-a-bufferedimage
/**
* Copies one image into another (rather than pointing from a normal assign)
*
* @param bi the bufferedImage to be copied
* @return a copy of the image
*/
private static BufferedImage deepCopy(BufferedImage bi) {
ColorModel cm = bi.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = bi.copyData(null);
return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
/**
* Sets the line thickness
*
* @param f the line thickness
*/
public void setLineThickness(float f) {
lineThickness = f;
g2.setStroke(new BasicStroke(f));
System.out.println("Line thickness set to " + (int) f);
}
/**
* Loads an image by prompting the user
*
* @throws IOException file not found
*/
public void load() throws IOException {
BufferedImage tmpImage;
int status = jfc.showOpenDialog(this);
File file = jfc.getSelectedFile();
if (status == JFileChooser.APPROVE_OPTION) {
prevImage = deepCopy(image);
clear();
tmpImage = ImageIO.read(file);
g2.drawImage(tmpImage, 0, 0, null);
repaint();
System.out.println("Image Opened: " + file.toString());
}
if (status == JFileChooser.CANCEL_OPTION) {
System.out.println("Open canceled.");
}
}
/**
* Saves an image by prompting the user
*
* @throws IOException file not found
*/
public void save() throws IOException {
jfc.setSelectedFile(new File("untitled." + saveExtension));
int status = jfc.showSaveDialog(this);
if (status == JFileChooser.APPROVE_OPTION) {
ImageIO.write(image, saveExtension, new File(jfc.getSelectedFile().toString() + "." + saveExtension));
System.out.println("Image saved: " + jfc.getSelectedFile().toString());
}
if (status == JFileChooser.CANCEL_OPTION) {
System.out.println("Save canceled.");
}
}
/**
* Wipe the screen
*/
public void clear() {
if (g2 == null) {
repaint();
}
Color temp = g2.getColor();
g2.setColor(bgColor);
g2.fill(new Rectangle(this.getWidth(), this.getHeight()));
g2.setColor(temp);
repaint();
}
/**
* Sets the primary color
*
* @param c a color
*/
public void setPrimaryColor(Color c) {
primaryColor = c;
System.out.println("Primary color changed.");
}
/**
* Sets the secondary color
*
* @param c a color
*/
public void setSecondaryColor(Color c) {
secondaryColor = c;
System.out.println("Secondary color changed.");
}
}