Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrawingPanel.java
More file actions
352 lines (316 loc) · 11.6 KB
/
Copy pathDrawingPanel.java
File metadata and controls
352 lines (316 loc) · 11.6 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
package java_paint;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
/**
*
* DrawingPanel class is where the drawing functions take place.
* Using the mouse we can draw lines or areas. This functionality is achieved by implementing the
* MouseListener and MouseMotionListener interfaces.
*
* The paint() method overrides the javax.swing.JComponent.paint() method.
* And it is refreshing the JPanel for us.
*
* @author
*
*/
public class DrawingPanel extends JPanel implements MouseListener, MouseMotionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private MainContainer window;
private Point initialPoint;
private Point finalPoint;
private GeneralPath polygonPath;
private ArrayList<BrushStroke> drawings = new ArrayList<BrushStroke>();
private boolean isPolygonFirstVertex;
public DrawingPanel(MainContainer frameWindow) {
this.isPolygonFirstVertex = true;
this.polygonPath = new GeneralPath();
this.window = frameWindow;
this.setBackground(Color.WHITE);
addMouseListener(this);
addMouseMotionListener(this);
}
@Override
public void paint(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
String brushChoice = window.getBrushType();
drawAllPreviousFigures(g2);
if (brushChoice.equals("polygon")) {
drawPolygon(g2);
}
if (wasMouseMoved()) {
g2.setColor(window.getBrushColor());
if (brushChoice.equals("pencil")) {
// implemented in mousePressed and mouseDragged
} else if (brushChoice.equals("line")) {
drawLine(g2);
} else if (brushChoice.equals("rectangle")) {
drawRectangle(g2);
} else if (brushChoice.equals("ellipse")) {
drawEllipse(g2);
} else if (brushChoice.equals("arc")) {
drawArc(g2);
}
}
}
private void drawAllPreviousFigures(Graphics2D g2) {
for (BrushStroke drawing : drawings) {
g2.setColor(drawing.getColor());
g2.setStroke(new BasicStroke(drawing.getThickness()));
if (drawing.isFilled() && !(drawing.getShape() instanceof Line2D.Float)) {
g2.fill(drawing.getShape());
} else {
g2.draw(drawing.getShape());
}
}
}
private void drawPolygon(Graphics2D g2) {
int border = 10;
if (wasMouseMoved() &&
initialPoint.x > finalPoint.x - border &&
initialPoint.x < finalPoint.x + border &&
initialPoint.y > finalPoint.y - border &&
initialPoint.y < finalPoint.y + border) {
drawings.add(new BrushStroke(polygonPath,
window.getBrushColor(),
window.getBrushThickness(),
window.isFigureFilled()));
polygonPath = new GeneralPath();
initialPoint = null;
finalPoint = null;
isPolygonFirstVertex = true;
repaint();
} else {
g2.setColor(window.getBrushColor());
g2.setStroke(new BasicStroke(window.getBrushThickness()));
g2.draw(polygonPath);
}
}
private void drawLine(Graphics2D g2) {
BrushStroke lineStroke = createLine(initialPoint.x, initialPoint.y,
finalPoint.x, finalPoint.y,
window.getBrushColor(), window.getBrushThickness(), window.isFigureFilled());
g2.setStroke(new BasicStroke(lineStroke.getThickness()));
g2.draw(lineStroke.getShape());
}
private void drawRectangle(Graphics2D g2) {
BrushStroke rectangle = createRectangle(initialPoint.x, initialPoint.y, finalPoint.x,
finalPoint.y, window.getBrushColor(), window.getBrushThickness(),
window.isFigureFilled());
g2.setStroke(new BasicStroke(rectangle.getThickness()));
g2.draw(rectangle.getShape());
}
private void drawEllipse(Graphics2D g2) {
BrushStroke ellipse = createEllipse(initialPoint.x, initialPoint.y, finalPoint.x,
finalPoint.y, window.getBrushColor(), window.getBrushThickness(),
window.isFigureFilled());
g2.setStroke(new BasicStroke(ellipse.getThickness()));
g2.draw(ellipse.getShape());
}
private void drawArc(Graphics2D g2) {
BrushStroke arc = createArc(initialPoint.x, initialPoint.y, finalPoint.x, finalPoint.y,
window.getBrushColor(), window.getBrushThickness(), window.isFigureFilled());
g2.setStroke(new BasicStroke(arc.getThickness()));
g2.draw(arc.getShape());
}
private boolean wasMouseMoved() {
return initialPoint != null && finalPoint != null;
}
public void mousePressed(MouseEvent e) {
String brushChoice = window.getBrushType();
if (brushChoice.equals("pencil")) {
setInitialMousePosition(e);
repaint();
} else if (brushChoice.equals("line")) {
setInitialMousePosition(e);
updateMousePosition();
repaint();
} else if (brushChoice.equals("rectangle")) {
setInitialMousePosition(e);
updateMousePosition();
repaint();
} else if (brushChoice.equals("ellipse")) {
setInitialMousePosition(e);
updateMousePosition();
repaint();
} else if (brushChoice.equals("arc")) {
setInitialMousePosition(e);
updateMousePosition();
repaint();
} else if (brushChoice.equals("polygon")) {
if (isPolygonFirstVertex) {
setInitialMousePosition(e);
polygonPath.moveTo(e.getX(), e.getY());
isPolygonFirstVertex = false;
} else {
setFinalMousePosition(e);
polygonPath.lineTo(e.getX(), e.getY());
}
repaint();
}
}
public void mouseDragged(MouseEvent e) {
String brushChoice = window.getBrushType();
if (brushChoice.equals("pencil")) {
addFreeHandLine(e);
initialPoint = new Point(finalPoint.x, finalPoint.y);
repaint();
} else if (brushChoice.equals("line")) {
setFinalMousePosition(e);
repaint();
} else if (brushChoice.equals("rectangle")) {
setFinalMousePosition(e);
repaint();
} else if (brushChoice.equals("ellipse")) {
setFinalMousePosition(e);
repaint();
} else if (brushChoice.equals("arc")) {
setFinalMousePosition(e);
repaint();
}
}
public void mouseReleased(MouseEvent e) {
String brushChoice = window.getBrushType();
if (brushChoice.equals("pencil")) {
addFreeHandLine(e);
repaint();
} else if (brushChoice.equals("line")) {
addFreeHandLine(e);
initialPoint = null;
finalPoint = null;
repaint();
} else if (brushChoice.equals("rectangle")) {
addRectangle(e);
initialPoint = null;
finalPoint = null;
repaint();
} else if (brushChoice.equals("ellipse")) {
addEllipse(e);
initialPoint = null;
finalPoint = null;
repaint();
} else if (brushChoice.equals("arc")) {
addArc(e);
initialPoint = null;
finalPoint = null;
repaint();
}
}
public void addFreeHandLine(MouseEvent e) {
setFinalMousePosition(e);
BrushStroke line = createLine(initialPoint.x, initialPoint.y, finalPoint.x,
finalPoint.y, window.getBrushColor(), window.getBrushThickness(),
window.isFigureFilled());
drawings.add(line);
}
private BrushStroke createLine(int x1, int y1, int x2, int y2, Color color, int thickness,
boolean filledOption) {
return new BrushStroke(new Line2D.Float(x1, y1, x2, y2), color, thickness, filledOption);
}
public void addRectangle(MouseEvent e) {
setFinalMousePosition(e);
BrushStroke rectangle = createRectangle(initialPoint.x, initialPoint.y, finalPoint.x,
finalPoint.y, window.getBrushColor(), window.getBrushThickness(),
window.isFigureFilled());
drawings.add(rectangle);
}
private BrushStroke createRectangle(int x1, int y1, int x2, int y2, Color color, int thickness,
boolean filledOption) {
return new BrushStroke(
new Rectangle2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
Math.abs(y1 - y2)),
color, thickness, filledOption);
}
public void addEllipse(MouseEvent e) {
BrushStroke ellipse = createEllipse(initialPoint.x, initialPoint.y, e.getX(), e.getY(),
window.getBrushColor(), window.getBrushThickness(), window.isFigureFilled());
drawings.add(ellipse);
}
private BrushStroke createEllipse(int x1, int y1, int x2, int y2, Color color, int thickness,
boolean filledOption) {
return new BrushStroke(
new Ellipse2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
Math.abs(y1 - y2)),
color, thickness, filledOption);
}
public void addArc(MouseEvent e) {
BrushStroke arc = createArc(initialPoint.x, initialPoint.y, e.getX(), e.getY(),
window.getBrushColor(), window.getBrushThickness(), window.isFigureFilled());
drawings.add(arc);
}
private BrushStroke createArc(int x1, int y1, int x2, int y2, Color color, int thickness,
boolean filledOption) {
int initialValueDegrees = 0;
int finalValueDegrees = 90;
if (x1 > x2 && y1 < y2) {
initialValueDegrees = 90;
}
if (x1 < x2 && y1 > y2) {
finalValueDegrees = -90;
}
if (x1 > x2 && y1 > y2) {
initialValueDegrees = 180;
finalValueDegrees = 90;
}
return new BrushStroke(
new Arc2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
Math.abs(y1 - y2), initialValueDegrees, finalValueDegrees, Arc2D.OPEN),
color, thickness, filledOption);
}
public void setInitialMousePosition(MouseEvent e) {
initialPoint = new Point(e.getX(), e.getY());
}
public void setFinalMousePosition(MouseEvent e) {
finalPoint = new Point(e.getX(), e.getY());
}
public void updateMousePosition() {
finalPoint = initialPoint;
}
public void clearDrawing() {
drawings.clear();
repaint();
}
public void undo() {
if (!drawings.isEmpty()) {
drawings.remove(drawings.size() - 1);
}
}
public void saveDrawingToFile(String filePath) {
BufferedImage image = new BufferedImage(getWidth(), getHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
paint(g2);
try{
ImageIO.write(image, "png", new File(filePath));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void mouseMoved(MouseEvent e) {
// Intentionally empty to implement inherited abstract method
}
@Override
public void mouseClicked(MouseEvent e) {
// Intentionally empty to implement inherited abstract method
}
@Override
public void mouseEntered(MouseEvent e) {
// Intentionally empty to implement inherited abstract method
}
@Override
public void mouseExited(MouseEvent e) {
// Intentionally empty to implement inherited abstract method
}
}
You can’t perform that action at this time.
