-
Notifications
You must be signed in to change notification settings - Fork 0
/
DrawingPanel.java
464 lines (410 loc) · 19.1 KB
/
DrawingPanel.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
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
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.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.*;
/**
* Handles the drawing functionality of the doily.
* Does all the calculations required for reflection and erasing
*/
public class DrawingPanel extends JPanel {
private ArrayList<Point> alCurrentShapes = new ArrayList<>();
private Stack<DraggedPoints> stCurrentStack = new Stack<>();
private Stack<DraggedPoints> stRedoStack = new Stack<>();
private int iSectors;
private int iSize;
private Color cColor;
private boolean bReflecting;
private boolean bErasing;
private boolean bLines;
/**
* Constructor that initialises the size, background colour, initial variables and listeners
*/
protected DrawingPanel() {
this.setPreferredSize(new Dimension(800, 750));
this.setBackground(Color.BLACK);
this.addMouseListener(new DragListener());
this.addMouseMotionListener(new DragListener());
this.setSectors(12);
this.setStrokeSize(5);
this.setDrawingLines(true);
this.setColor(Color.RED);
}
/**
* Listener that handles the user drawing on the screen
*/
class DragListener implements MouseListener, MouseMotionListener {
//when first pressed, get the coordinates and add to an arraylist. repaint to show new point
public void mousePressed(MouseEvent e) {
DrawingPanel.this.getCurrentShapes().add(new Point(e.getX(), e.getY()));
repaint();
}
//when mouse is released, if not erasing then add the new shape to the stack, repaint
//if erasing, send the arraylist to removePoints and then repaint once complete
//clear the list once done
public void mouseReleased(MouseEvent e) {
if(!DrawingPanel.this.isErasing()) {
ArrayList<Point> newCurrentShapes;
newCurrentShapes = (ArrayList) DrawingPanel.this.getCurrentShapes().clone();
DrawingPanel.this.getCurrentStack().push(new DraggedPoints(newCurrentShapes
, DrawingPanel.this.getStrokeSize()
, DrawingPanel.this.getColor()
, DrawingPanel.this.isReflecting()
, DrawingPanel.this.isErasing())
);
repaint();
}
else {
DrawingPanel.this.removePoints(DrawingPanel.this.getCurrentShapes());
repaint();
}
DrawingPanel.this.getCurrentShapes().clear();
}
public void mouseDragged(MouseEvent e) {
DrawingPanel.this.getCurrentShapes().add(new Point(e.getX(), e.getY()));
repaint();
}
//unimplemented methods
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
}
}
/**
* Method that paints the panel, once called
* @param g
*/
public void paintComponent(Graphics g) {
//initialising variables and calling super-class
super.paintComponent(g);
Ellipse2D.Double e;
DraggedPoints d;
Graphics2D g2d = (Graphics2D) g;
AffineTransform atx;
//setting antialiasing on to make it look pretty!
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setBackground(Color.BLACK);
//draw the lines if required
if(DrawingPanel.this.isDrawingLines()) {
this.drawBackgroundLines(g2d);
}
//for the alCurrentShapes currently inside of the current points stack
Iterator<DraggedPoints> it = DrawingPanel.this.getCurrentStack().iterator();
while (it.hasNext()) {
d = it.next();
g2d.setColor(d.getcPointsColor());
//if only one Point in the arraylist
if(d.getAlListOfPoints().size() == 1) {
this.drawPoint(g2d, d);
}
//if multiple points in the arraylist
else {
this.drawLines(g2d, d);
}
}
//if erasing, temporarily set the alphacomposite to DST_OUT.
if(this.isErasing()) {
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_OUT));
}
//for the currently drawn alCurrentShapes
if(DrawingPanel.this.getCurrentShapes().size() == 1) {
this.drawPoint(g2d, DrawingPanel.this.getCurrentShapes().get(0));
}
else {
this.drawLines(g2d, DrawingPanel.this.getCurrentShapes());
}
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
}
/**
* Draw the background lines
* @param g2d
*/
private void drawBackgroundLines(Graphics2D g2d) {
g2d.setColor(Color.WHITE);
for(int i = 0 ; i < this.getSectors() ; i++) {
g2d.rotate(Math.toRadians(360.0 / this.getSectors()), this.getSize().getWidth() / 2, this.getSize().getHeight() / 2);
g2d.drawLine((int) this.getSize().getWidth() / 2, 0, (int) this.getSize().getWidth() / 2, (int) this.getSize().getHeight() / 2);
g2d.setStroke(new BasicStroke(1));
}
}
/**
* Drawing a singular point from the stack
* @param g2d
* @param d DraggedPoint from the stack
*/
private void drawPoint(Graphics2D g2d, DraggedPoints d) {
//define variables
Ellipse2D.Double e;
AffineTransform atx;
e = new Ellipse2D.Double(d.getAlListOfPoints().get(0).getX() - (d.getiSize() / 2)
, d.getAlListOfPoints().get(0).getY() - (d.getiSize() / 2)
, d.getiSize()
, d.getiSize());
//if reflecting, perform an affinetranform and then draw the reflected parts.
if(d.isbReflected()) {
for (int i = 0; i < this.getSectors(); i++) {
atx = AffineTransform.getTranslateInstance(this.getSize().getWidth() / 2, 0);
atx.scale(-1, 1);
atx.translate(-this.getSize().getWidth() / 2, 0);
atx.rotate(Math.toRadians((360.0 / this.getSectors()) * i), this.getSize().getWidth() / 2, this.getSize().getHeight() / 2);
g2d.setTransform(atx);
g2d.draw(e);
g2d.fill(e);
g2d.setTransform(AffineTransform.getScaleInstance(1, 1));
}
}
//draw the normal points
for (int i = 0; i < this.getSectors(); i++) {
g2d.rotate(Math.toRadians(360.0 / this.getSectors()), this.getSize().getWidth() / 2, this.getSize().getHeight() / 2);
g2d.draw(e);
g2d.fill(e);
}
}
/**
* Drawing a singular point from the current shapes
* @param g2d
* @param p Point from the ArrayList
*/
private void drawPoint(Graphics2D g2d, Point p) {
//initialise variables, set color to the current one being used
Ellipse2D.Double e;
AffineTransform atx;
g2d.setColor(DrawingPanel.this.getColor());
e = new Ellipse2D.Double(p.getX() - (DrawingPanel.this.getStrokeSize() / 2)
, p.getY() - (DrawingPanel.this.getStrokeSize() / 2)
, DrawingPanel.this.getStrokeSize()
, DrawingPanel.this.getStrokeSize());
//if reflecting, perform an affinetranform and then draw the reflected parts.
if(DrawingPanel.this.isReflecting()) {
for (int i = 0; i < this.getSectors(); i++) {
atx = AffineTransform.getTranslateInstance(this.getSize().getWidth() / 2, 0);
atx.scale(-1, 1);
atx.translate(-this.getSize().getWidth() / 2, 0);
atx.rotate(Math.toRadians((360.0 / this.getSectors()) * i), this.getSize().getWidth() / 2, this.getSize().getHeight() / 2);
g2d.setTransform(atx);
g2d.draw(e);
g2d.fill(e);
}
g2d.setTransform(AffineTransform.getScaleInstance(1, 1));
}
//draw the normal points
for (int i = 0; i < this.getSectors(); i++) {
g2d.rotate(Math.toRadians(360.0 / this.getSectors()), this.getSize().getWidth() / 2, this.getSize().getHeight() / 2);
g2d.draw(e);
g2d.fill(e);
}
}
/**
* Drawing a list of points (as lines) from the stack
* @param g2d
* @param d DraggedPoints from the stack
*/
private void drawLines(Graphics2D g2d, DraggedPoints d) {
AffineTransform atx;
//start from one, easier than requiring an if statement inside each time to check if 0
for (int i = 1; i < d.getAlListOfPoints().size(); i++) {
//for the pair of points, draw a line between them and rotate by the appropriate amount.
if(d.isbReflected()) {
for (int j = 0; j < this.getSectors(); j++) {
atx = AffineTransform.getTranslateInstance(this.getSize().getWidth() / 2, 0);
atx.scale(-1, 1);
atx.translate(-this.getSize().getWidth() / 2, 0);
atx.rotate(Math.toRadians((360.0 / this.getSectors()) * j), this.getSize().getWidth() / 2, this.getSize().getHeight() / 2);
g2d.setTransform(atx);
g2d.drawLine(d.getAlListOfPoints().get(i).getX()
, d.getAlListOfPoints().get(i).getY()
, d.getAlListOfPoints().get(i - 1).getX()
, d.getAlListOfPoints().get(i - 1).getY());
g2d.setStroke(new BasicStroke((float)(d.getiSize() / Math.sqrt(2.0))));
}
g2d.setTransform(AffineTransform.getScaleInstance(1,1));
}
for (int j = 0; j < this.getSectors(); j++) {
g2d.rotate(Math.toRadians(360.0 / this.getSectors()), this.getSize().getWidth() / 2, this.getSize().getHeight() / 2);
g2d.drawLine(d.getAlListOfPoints().get(i).getX()
, d.getAlListOfPoints().get(i).getY()
, d.getAlListOfPoints().get(i - 1).getX()
, d.getAlListOfPoints().get(i - 1).getY());
g2d.setStroke(new BasicStroke((float)(d.getiSize() / Math.sqrt(2.0))));
}
}
}
/**
* Drawing a list of points (as lines) from the current shapes list
* @param g2d
* @param d ArrayList from the current shapes list
*/
private void drawLines(Graphics2D g2d, ArrayList<Point> d) {
AffineTransform atx;
g2d.setColor(DrawingPanel.this.getColor());
//start from one, easier than requiring an if statement inside each time to check if 0
for (int i = 1; i < d.size(); i++) {
//for the pair of points, draw a line between them and rotate by the appropriate amount.
if(DrawingPanel.this.isReflecting()) {
for (int j = 0; j < this.getSectors(); j++) {
atx = AffineTransform.getTranslateInstance(this.getSize().getWidth() / 2, 0);
atx.scale(-1, 1);
atx.translate(-this.getSize().getWidth() / 2, 0);
atx.rotate(Math.toRadians((360.0 / this.getSectors()) * j), this.getSize().getWidth() / 2, this.getSize().getHeight() / 2);
g2d.setTransform(atx);
g2d.drawLine(d.get(i).getX()
, d.get(i).getY()
, d.get(i - 1).getX()
, d.get(i - 1).getY());
g2d.setStroke(new BasicStroke((float)(DrawingPanel.this.getStrokeSize() / Math.sqrt(2.0))));
}
g2d.setTransform(AffineTransform.getScaleInstance(1,1));
}
for (int j = 0; j < this.getSectors(); j++) {
g2d.rotate(Math.toRadians(360.0 / this.getSectors()), this.getSize().getWidth() / 2, this.getSize().getHeight() / 2);
g2d.drawLine(d.get(i).getX()
, d.get(i).getY()
, d.get(i - 1).getX()
, d.get(i - 1).getY());
g2d.setStroke(new BasicStroke((float)(DrawingPanel.this.getStrokeSize() / Math.sqrt(2.0))));
}
}
}
/**
* Used to remove points once the user has released the mouse
* @param alEraserPoints
*/
private void removePoints(ArrayList<Point> alEraserPoints) {
//declare and initialise variables
Iterator<DraggedPoints> it = this.getCurrentStack().iterator();
DraggedPoints d;
TreeSet<Integer> tPointsToRemove = new TreeSet<>();
ArrayList<DraggedPoints> alListOfNewPoints = new ArrayList<>();
ArrayList<Point> alInnerPoints = new ArrayList<>();
double dLength1, dAngle1;
boolean bErased;
//iterate through the stack
while(it.hasNext()) {
bErased = false;
d = it.next();
//for all points of DraggedPoint, check it with each drawn eraser point in each sector
for(int i = 0 ; i < d.getAlListOfPoints().size() ; i++) {
for(int j = 0 ; j < alEraserPoints.size() ; j++) {
dLength1 = Math.sqrt(Math.pow(alEraserPoints.get(j).getX() - (this.getWidth() / 2), 2) + Math.pow(alEraserPoints.get(j).getY() - (this.getHeight() / 2), 2));
dAngle1 = Math.atan2((double)(alEraserPoints.get(j).getY() - (this.getHeight() / 2)), (double)(alEraserPoints.get(j).getX() - (this.getWidth() / 2)));
//if the distance between the two points is less than the radius of the eraser, mark for deletion in a set
for(int k = 0 ; k < this.getSectors() ; k++) {
if(Math.sqrt(Math.pow(d.getAlListOfPoints().get(i).getX() - (dLength1 * Math.cos(dAngle1 + Math.toRadians((360 / this.getSectors()) * k)) + (this.getWidth() / 2)), 2)
+ Math.pow(d.getAlListOfPoints().get(i).getY() - (dLength1 * Math.sin(dAngle1 + Math.toRadians((360 / this.getSectors()) * k)) + (this.getHeight() / 2)), 2)) <= (this.getStrokeSize() / 2)) {
tPointsToRemove.add(i);
bErased = true;
}
}
//if the points in question are reflected, check the alternate side and iterate through them
if(d.isbReflected()) {
dLength1 = Math.sqrt(Math.pow((this.getWidth() / 2) - alEraserPoints.get(j).getX(), 2) + Math.pow((this.getHeight() / 2) - alEraserPoints.get(j).getY(), 2));
dAngle1 = Math.atan2((double)((this.getHeight() / 2) - alEraserPoints.get(j).getY()), (double)((this.getWidth() / 2) - alEraserPoints.get(j).getX()));
//if the distance between the two points is less than the radius of the eraser, mark for deletion in a set
for(int k = 0 ; k < this.getSectors() ; k++) {
if(Math.sqrt(Math.pow(d.getAlListOfPoints().get(i).getX() - (dLength1 * Math.cos(dAngle1 + Math.toRadians((360 / this.getSectors()) * k)) + (this.getWidth() / 2)), 2)
+ Math.pow(d.getAlListOfPoints().get(i).getY() - (dLength1 * Math.sin(dAngle1 + Math.toRadians((360 / this.getSectors()) * k)) + (this.getHeight() / 2)), 2)) <= (this.getStrokeSize() / 2)) {
tPointsToRemove.add(i);
bErased = true;
}
}
}
}
}
//if erasing, iterate through each point in the line
if(bErased) {
int iStartLine = 0;
int iEndLine = 0;
for(int i = 0 ; i < d.getAlListOfPoints().size() ; i++) {
//if a point marked for deletion appears, and does not follow another point, or is the last point
if((tPointsToRemove.contains(i)) || (i == d.getAlListOfPoints().size() - 1)) {
//mark the end of the line
iEndLine = i;
if(tPointsToRemove.contains(i - 1)) {
iStartLine++;
continue;
}
//iterate through the subset of the points marked and add to an arraylist of erased lines
for(int j = iStartLine ; j < iEndLine ; j++) {
alInnerPoints.add(d.getAlListOfPoints().get(j));
}
alListOfNewPoints.add(new DraggedPoints(new ArrayList<>(alInnerPoints)
, d.getiSize()
, d.getcPointsColor()
, d.isbReflected()
, d.isbEraser()));
iStartLine = i + 1;
alInnerPoints.clear();
}
else {
iEndLine++;
}
}
//remove the old line
it.remove();
}
}
//add each new line to the stack
for(DraggedPoints newD : alListOfNewPoints) {
this.getCurrentStack().add(newD);
}
}
//getters
private ArrayList<Point> getCurrentShapes() {
return alCurrentShapes;
}
protected Stack<DraggedPoints> getCurrentStack() {
return stCurrentStack;
}
protected Stack<DraggedPoints> getRedoStack() {
return stRedoStack;
}
private int getSectors() {
return iSectors;
}
private int getStrokeSize() {
return iSize;
}
private Color getColor() {
return cColor;
}
private boolean isReflecting() {
return bReflecting;
}
private boolean isErasing() {
return bErasing;
}
private boolean isDrawingLines() {
return bLines;
}
//setters
protected void setSectors(int iSectors) {
this.iSectors = iSectors;
}
protected void setStrokeSize(int iSize) {
this.iSize = iSize;
}
protected void setColor(Color cColor) {
this.cColor = cColor;
}
protected void setReflecting(boolean bReflecting) {
this.bReflecting = bReflecting;
}
protected void setErasing(boolean bErasing) {
this.bErasing = bErasing;
}
protected void setDrawingLines(boolean bLines) {
this.bLines = bLines;
}
}