-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnippingTool.java
115 lines (103 loc) · 4.06 KB
/
SnippingTool.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
import java.util.ArrayList;
public class SnippingTool extends RectangleTool {
protected CanvasSquare selectedSquare;
protected CanvasSquare[][] copiedPixels;
protected byte[] canvasBeforeRectangle;
public SnippingTool(String name) {
super(name);
previousColours = new ArrayList<SquareColour>();
}
/**
* Sets the pixels of the rectangle to alternate between black and white
* so that they can be viewed on top of any colour
*/
@Override
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());
if(i % 2 == 0) {
pixels.get(i).setColour(new SquareColour(0,0,0));
}
else {
pixels.get(i).setColour(new SquareColour(150,150,150));
}
}
}
}
/**
* Cuts all the pixels within the selected area, setting them to their default colour
*/
public void cutPixels() {
CanvasSquare[][] grid = Window.canvas.getGrid();
for(int x = startX; x < endX + 1; x++) {
for(int y = startY; y < endY + 1; y++) {
grid[x][y].setColour((x + y) % 2 == 0 ? Window.canvas.getDefaultLight() : Window.canvas.getDefaultDark());
}
}
canvasBeforeRectangle = null;
Window.canvas.repaint();
}
/**
* Copies all of the pixels within the selected area
*/
public void copyPixels() {
CanvasSquare[][] grid = Window.canvas.getGrid();
copiedPixels = new CanvasSquare[(endX - 1) - (startX)][(endY - 1) - (startY)];
for(int x = startX + 1; x < endX; x++) {
for(int y = startY + 1; y < endY; y++) {
copiedPixels[(x-1) - startX][(y-1) - startY] = grid[x][y];
}
}
Window.canvas.openCanvas(canvasBeforeRectangle);
canvasBeforeRectangle = null;
Window.canvas.repaint();
}
/**
* Pastes all of the copied pixels around the point that the user has selected
*/
public void pastePixels() {
if(selectedSquare == null) {return;} //if no square is selected then dont paste
CanvasSquare[][] grid = Window.canvas.getGrid();
int selectedX = selectedSquare.getX();
int selectedY = selectedSquare.getY();
for(int x = 0; x < copiedPixels.length; x++) {
for(int y = 0; y < copiedPixels[0].length; y++) {
if(copiedPixels[x][y].getColour().compare(Window.canvas.getDefaultDark()) || copiedPixels[x][y].getColour().compare(Window.canvas.getDefaultLight())) {
continue;
}
//pastes the pixels around the selected square
try {
grid[(selectedX - copiedPixels.length/2) + x][(selectedY - copiedPixels[0].length/2) + y].setColour(copiedPixels[x][y].getColour());
} catch (IndexOutOfBoundsException e) {continue;}
}
}
canvasBeforeRectangle = null;
Window.canvas.repaint();
}
@Override
public void action(PixelCanvas canvas, CanvasSquare square, ToolState state) {
switch(state){
case PRESSED:
canvasBeforeRectangle = Window.canvas.serializeCanvas();
startX = square.getX();
startY = square.getY();
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);
reset();
break;
}
}
}