-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPixelCanvas.java
399 lines (356 loc) · 14.7 KB
/
PixelCanvas.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
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.event.MouseInputListener;
import java.awt.event.MouseEvent;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;
public class PixelCanvas extends JPanel implements MouseInputListener {
private int width;
private int height;
private CanvasSquare[][] grid;
private int startX;
private int startY;
private boolean isMovingX;
private final SquareColour defaultDark = new SquareColour(60, 60, 60);
private final SquareColour defaultLight = new SquareColour(80, 80, 80);
/**
* Represents the Canvas in the pixel editor UI, which is composed of
* CanvasSquare objects
*
* @param width The width of the canvas in CanvasSquares
* @param height The height of the canvas in CanvasSquares
*/
public PixelCanvas(int width, int height) {
this.width = width;
this.height = height;
this.grid = new CanvasSquare[width][height];
this.setLayout(new GridLayout(width, height));
setDefaultCanvasSquares();
addMouseListener(this);
addMouseMotionListener(this);
}
/**
* Gets the current state of the grid, iterates through each pixel and converts
* the colour to
* a single int with red | blue | green in each 2 byte position.
*
* @return 1D grid of pixels with colours represented as int
*/
public byte[] serializeCanvas() {
byte[] serializedData = new byte[width * height * 3];
int index = 0;
for (CanvasSquare[] column : grid) {
for (CanvasSquare pixel : column) {
serializedData[index + 0] = (byte) pixel.getColour().red;
serializedData[index + 1] = (byte) pixel.getColour().green;
serializedData[index + 2] = (byte) pixel.getColour().blue;
index += 3;
}
}
return serializedData;
}
/**
* Sets the default colours of the canvas squares when the canvas is initalised
*/
public void setDefaultCanvasSquares() {
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
grid[i][j] = new CanvasSquare(
(i + j) % 2 == 0 ? defaultLight : defaultDark,
i, j);
}
}
}
/**
* Also set the colour to default background, but doesn't create a new grid,
* only clears the only specified
*/
public void resetToDefault() {
}
/**
* Updates the current view of the canvas
*
* @param g The graphics object associated with the canvas, get with
* getGraphics()
*/
protected void paintComponent(Graphics g) {
super.paintComponent(g);
SquareColour colour;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
colour = grid[i][j].getColour();
g.setColor(colour);
g.fillRect(i * 10, j * 10, 10, 10);
}
}
}
/**
* Takes a pixel coordinate on the canvas, for example from a click event, and
* returns the
* CanvasPixel clicked on.
*
* @param x The x-coordinate of the screen pixel on the canvas
* @param y The y-coordinate of the screen pixel on the canvas
* @return The CanvasSquare clicked on
*/
private CanvasSquare convertCoordToSquare(int x, int y) {
int xSquare = x / 10;
int ySquare = y / 10;
return grid[xSquare][ySquare];
};
/**
* Get height of the canvas in CanvasSquares
*
* @return Height in squares
*/
public int getCanvasWidth() {
return this.width;
}
/**
* Get width of the canvas in CanvasSquares
*
* @return Width in squares
*/
public int getCanvasHeight() {
return this.height;
}
public void setCanvasWidth(int width) {
this.width = width;
}
public void setCanvasHeight(int height) {
this.height = height;
}
public void resizeCanvas(int width, int height) {
if(width < 0) {
width *=-1;
}
if(height < 0) {
height *=-1;
}
this.setCanvasWidth(width);
this.setCanvasHeight(height);
this.setGrid(new CanvasSquare[width][height]);
this.setLayout(new GridLayout(width, height));
this.setDefaultCanvasSquares();
AppManager.previousCanvasStates.clear();
this.revalidate();
this.repaint();
}
public CanvasSquare[][] getGrid() {
return this.grid;
}
public void setGrid(CanvasSquare[][] grid) {
this.grid = grid;
}
/**
* Saves the canvas and current tool settings to the specified file. Will apply
* run-length
* encoding compression if specified.
*
* @param file The file to save to
* @param compress True if the file should use RLE compression
*/
public void save(File file) {
byte[] canvasData = this.serializeCanvas();
// get size of canvas and tool size as bytes
byte canvasWidthBytes = Integer.valueOf(Window.canvas.getCanvasWidth()).byteValue();
byte canvasHeightBytes = Integer.valueOf(Window.canvas.getCanvasHeight()).byteValue();
byte toolThickness = Integer.valueOf(AppManager.toolThickness).byteValue();
// write the data to the file
try {
FileOutputStream stream = new FileOutputStream(file);
stream.write(canvasWidthBytes);
stream.write(canvasHeightBytes);
stream.write(toolThickness);
stream.write(canvasData);
stream.close();
} catch (IOException exception) {
System.out.println("Could not save file.");
exception.printStackTrace();
}
}
/**
* Rotates the canvas 90 degrees anticlockwise or flips the canvas
* @param rotate true if the canvas is to be rotated, false if it is to be flipped
*/
public void rotate(boolean rotate) {
AppManager.previousCanvasStates.push(Window.canvas.serializeCanvas());
SquareColour[][] flippedGrid = new SquareColour[width][height];
final int M = grid.length;
//rotates the canvas grid by 90 degrees
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if(rotate) {
flippedGrid[j][M-1-i] = grid[i][j].getColour();
}
else {
flippedGrid[M-1-i][j] = grid[i][j].getColour();
}
}
}
Window.canvas.setDefaultCanvasSquares();
//sets the new grid colours to the flipped grid colours
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
SquareColour flippedCol = flippedGrid[i][j];
if(flippedCol.compare(defaultDark) || flippedCol.compare(defaultLight)) {
continue;
}
grid[i][j].setColour(flippedCol);
}
}
this.repaint();
}
/**
* Gets the square on the canvas at the given coordinate
*
* @param x The x-coord of the square
* @param y The y-coord of the square
* @return The square at that coordinate, or `null` if invalid coordinate
*/
public CanvasSquare getSquare(int x, int y) {
try {
return grid[x][y];
}
catch (ArrayIndexOutOfBoundsException e) {
return null;
}
}
public SquareColour getDefaultLight() {
return defaultLight;
}
public SquareColour getDefaultDark() {
return defaultDark;
}
/**
* Takes the array of pixel data and uploads it to the canvas, restoring it.
*
* @param pixelBuffer Buffer of pixel data to upload to the canvas
*/
public void openCanvas(byte[] pixelBuffer) {
Window.canvas.setDefaultCanvasSquares();
int canvasWidth = Window.canvas.getCanvasWidth();
for (int i = 0; i < pixelBuffer.length; i += 3) {
SquareColour colour = new SquareColour(
Byte.toUnsignedInt(pixelBuffer[i]),
Byte.toUnsignedInt(pixelBuffer[i + 1]),
Byte.toUnsignedInt(pixelBuffer[i + 2]));
CanvasSquare square = Window.canvas.getSquare(
(i / 3) / canvasWidth,
(i / 3) % canvasWidth);
square.setColour(colour);
}
Window.canvas.repaint();
}
public void mouseClicked(MouseEvent event) {
try {
if (AppManager.currentTool == AppManager.CIRCLE_TOOL){
return;
}
else if(AppManager.currentTool == null) {
AppManager.SNIPPING.selectedSquare = convertCoordToSquare(event.getX(), event.getY());
return;
}
AppManager.currentTool.action(this, convertCoordToSquare(event.getX(), event.getY()));
} catch (ArrayIndexOutOfBoundsException e) {}
}
public void mousePressed(MouseEvent event) { // when mouse pressed, store the coordinates of the current X and Y
// values, as well as the canvas state
try {
AppManager.previousCanvasStates.push(this.serializeCanvas()); //stores the canvas state before change for undo/redo functions
} catch(StackOverflowError e) {AppManager.previousCanvasStates.remove(0);}
if(AppManager.SNIPPING.canvasBeforeRectangle != null) { //MAKE THIS SO THAT IT CLEARS WHEN OPENED
Window.canvas.openCanvas(AppManager.SNIPPING.canvasBeforeRectangle);
AppManager.SNIPPING.canvasBeforeRectangle = null;
}
startX = event.getX();
startY = event.getY();
// Handles tools that can be assigned to both the left and right mouse
if (event.getButton() == MouseEvent.BUTTON2 && AppManager.currentTool == AppManager.SOLID_PEN) {
AppManager.currentTool = AppManager.ERASER;
Window.canvas.setCursor(Toolbar.getEraserCursor());
} else if (event.getButton() == MouseEvent.BUTTON2 && AppManager.currentTool == AppManager.ERASER) {
AppManager.currentTool = AppManager.SOLID_PEN;
Window.canvas.setCursor(Toolbar.getPenCursor());
}
if (event.getButton() == MouseEvent.BUTTON3 && AppManager.currentTool == AppManager.LIGHTEN) {
AppManager.currentTool = AppManager.DARKEN;
} else if (event.getButton() == MouseEvent.BUTTON1 && AppManager.currentTool == AppManager.DARKEN) {
AppManager.currentTool = AppManager.LIGHTEN;
}
// if the middle mouse button is pressed with the symmetry tool, the canvas will
// display the line of symmetry
if (event.getButton() == MouseEvent.BUTTON2 && AppManager.currentTool == AppManager.SYMMETRY) {
SymmetryLine.canvasWithoutLine = Window.canvas.serializeCanvas();
if (AppManager.SYMMETRY.islineOfSymmetryVertical) {
SymmetryLine.drawVerticalLine(SymmetryLine.xVal);
} else {
SymmetryLine.drawHorizontalLine(SymmetryLine.yVal);
}
}
if (AppManager.currentTool == AppManager.CIRCLE_TOOL || AppManager.currentTool == AppManager.RECTANGLE_TOOL) {
// get starting point of where the mouse is first pressed
AppManager.currentTool.action(this, convertCoordToSquare(startX, startY), ToolState.PRESSED);
}
if(AppManager.currentTool == null) {
AppManager.SNIPPING.action(this, convertCoordToSquare(startX, startY), ToolState.PRESSED);
}
}
/**
* When the mouse is dragged normally, all squares that it passes over are
* coloured.
* If the shift key is being held, the line drawn will snap to the closest axis.
*/
public void mouseDragged(MouseEvent event) {
try {
if(AppManager.currentTool == null) {
AppManager.SNIPPING.action(this, convertCoordToSquare(event.getX(), event.getY()), ToolState.DRAGGED);
return;
}
if (AppManager.currentTool == AppManager.BUCKET || AppManager.currentTool == AppManager.SYMMETRY_LINE || AppManager.currentTool == AppManager.COLOUR_PICKER) {
return;
} else if (AppManager.currentTool == AppManager.CIRCLE_TOOL
|| AppManager.currentTool == AppManager.RECTANGLE_TOOL) {
// dragging
AppManager.currentTool.action(this, convertCoordToSquare(event.getX(), event.getY()),
ToolState.DRAGGED);
return;
}
if (event.getX() != startX && (event.getY() < startY + 5 && event.getY() > startY - 5)) {
isMovingX = true;
} // if x axis is closer, snap to x
if (event.getY() != startY && (event.getX() < startX + 5 && event.getX() > startX - 5)) {
isMovingX = false;
} // if y axis is closer, snap to y
if (isMovingX && event.isShiftDown()) { // if shift key held while moving diagonally, mouse will snap to the
// x axis
AppManager.currentTool.action(this, convertCoordToSquare(event.getX(), startY));
} else if (!isMovingX && event.isShiftDown()) { // if shift key held while moving vertically, mouse will
// snap to the y axis
AppManager.currentTool.action(this, convertCoordToSquare(startX, event.getY()));
} else { // if the shift key is not being held, colour all squares that the cursor passes
// over
AppManager.currentTool.action(this, convertCoordToSquare(event.getX(), event.getY()));
}
} catch (ArrayIndexOutOfBoundsException e) {
return;
}
}
public void mouseReleased(MouseEvent event) {
if (event.getButton() == MouseEvent.BUTTON2 && AppManager.currentTool == AppManager.SYMMETRY) {
Window.canvas.openCanvas(SymmetryLine.canvasWithoutLine);
}
// reset the start/end points
if (AppManager.currentTool == AppManager.CIRCLE_TOOL || AppManager.currentTool == AppManager.RECTANGLE_TOOL) {
// end
AppManager.currentTool.action(this, convertCoordToSquare(event.getX(), event.getY()), ToolState.RELEASED);
}
if(AppManager.currentTool == null) {
AppManager.SNIPPING.action(this, convertCoordToSquare(event.getX(), event.getY()), ToolState.RELEASED);
}
}
public void mouseMoved(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
}