-
Notifications
You must be signed in to change notification settings - Fork 0
/
RectangleTool.java
119 lines (104 loc) · 4 KB
/
RectangleTool.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
import java.util.ArrayList;
public class RectangleTool extends PenTool {
protected String name;
protected final SquareColour defaultDark = new SquareColour(60, 60, 60);
protected final SquareColour defaultLight = new SquareColour(80, 80, 80);
protected int startX;
protected int startY;
protected int endX;
protected int endY;
protected CanvasSquare[][] currentCoords;
protected ArrayList<CanvasSquare> oldCoords;
protected ArrayList<CanvasSquare> newCoords;
protected ArrayList<CanvasSquare> allCoords;
protected ArrayList<SquareColour> previousColours;
private CanvasSquare lastSquare;
public RectangleTool(String name) {
this.name = name;
oldCoords = new ArrayList<>();
newCoords = new ArrayList<>();
allCoords = new ArrayList<>();
previousColours = new ArrayList<SquareColour>();
}
public void drawRectangle(int startX, int startY, int endX, int endY) {
//because the direction where the user drags the tool can messed up the for loop if the end conditions are < or >
int xDirection = Integer.signum(endX - startX); //0 = neg, 1 = pos
int yDirection = Integer.signum(endY - startY);
for (int x = startX; x != endX + xDirection; x += xDirection) {
if(x == startX || x == endX){
for (int y = startY; y != endY + yDirection; y += yDirection) {
newCoords.add(currentCoords[x][y]);
}
} else{
newCoords.add(currentCoords[x][startY]);
newCoords.add(currentCoords[x][endY]);
}
}
clearPixels(oldCoords);
setPixels(newCoords);
try {
lastSquare = newCoords.get(newCoords.size()-1);
} catch(IndexOutOfBoundsException e) {}
oldCoords = new ArrayList<>(newCoords);
newCoords.clear();
}
/**
* Set the pixels passed in to the current selected colours
* @param pixels ArrayList of pixels to be changed
*/
public void setPixels(ArrayList<CanvasSquare> pixels) {
for (int i = 0; i < pixels.size() - 1; i++) {
int x = pixels.get(i).getX();
int y = pixels.get(i).getY();
if (x >= 0 && x < currentCoords.length && y >= 0 && y < currentCoords[0].length) {
previousColours.add(pixels.get(i).getColour());
pixels.get(i).setColour(AppManager.primaryColour);
}
}
}
public void clearPixels(ArrayList<CanvasSquare> pixels) {
for (int i = 0; i < pixels.size() - 1; i++) {
int x = pixels.get(i).getX();
int y = pixels.get(i).getY();
if (x >= 0 && x < currentCoords.length && y >= 0 && y < currentCoords[0].length && !allCoords.contains(pixels.get(i))) {
pixels.get(i).setColour(previousColours.get(i));
}
}
previousColours.clear();
}
@Override
public void action(PixelCanvas canvas, CanvasSquare square) {}
@Override
public String getName() {
return this.name;
}
@Override
public void action(PixelCanvas canvas, CanvasSquare square, ToolState state) {
switch(state){
case PRESSED:
startX = square.getX();
startY = square.getY();
square.setColour(AppManager.primaryColour);
currentCoords = canvas.getGrid();
oldCoords.add(square);
canvas.repaint();
break;
case DRAGGED:
endX = square.getX();
endY = square.getY();
drawRectangle(startX, startY, endX, endY);
canvas.repaint();
break;
case RELEASED:
allCoords.addAll(oldCoords);
lastSquare.setColour(AppManager.primaryColour);
reset();
canvas.repaint();
break;
}
}
public void reset(){
oldCoords.clear();
newCoords.clear();
}
}