-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCartoonDrawing.java
More file actions
397 lines (328 loc) · 10.9 KB
/
CartoonDrawing.java
File metadata and controls
397 lines (328 loc) · 10.9 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
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
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.net.URL;
import java.io.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class CartoonDrawing extends JFrame {
private Color[] colors = {
Color.black, Color.red, Color.blue, Color.white, Color.gray,
new Color(242,229,177), new Color(255,204,193), new Color(62,62,60),
new Color(49,147,204), new Color(142,38,46), new Color(248,198,79)
};
private int colorIndex;
private static final Color DRAWING_COLOR = new Color(255, 100, 200);
private static final Color FINAL_DRAWING_COLOR = Color.green;
private BufferedImage buffImage;
private boolean hideImage = true;
private boolean hideDrawing = false;
private boolean hideInfo;
private Point startPoint;
private Point endPoint;
private Point currentPoint;
private Point ctrlPoint1;
private Point ctrlPoint2;
private GPPoint.Type currentGPType = GPPoint.Type.LINE;
private ShapeData currentData = new RectData();
private ShapeDataList shapeDataList = new ShapeDataList();
private static String filePath;
private static String dataPath;
private static String imagePath;
private MyPanel myPanel;
public static final ShapeData[] shapeChoices = {
new RectData(), new LineData(), new EllipseData(), new QuadCurveData(), new CubicCurveData(), new GPData()
};
public static final GPPoint.Type[] gpPointChoices = {
GPPoint.Type.LINE, GPPoint.Type.QUAD, GPPoint.Type.CUBIC,
};
public int gpPointChoice = 0;
public int choice = 0;
public void repaintFrame() {
validate();
repaint();
}
public class MyPanel extends JPanel {
public MyPanel() {
addMouseMotionListener(new MyMouseAdapter());
addMouseListener(new MyMouseAdapter());
addMouseWheelListener(new MyMouseWheelListener());
setBackground(Color.white);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.black);
if (!hideInfo) {
int x = 30;
int y = 50;
g2d.drawString("currentColor: " + currentData.color,x, y);
g2d.drawString("[A] to change color, or 1 to 5 important colors (black,red,blue,white,gray)", x, y += 15);
g2d.drawString("currentIsDraw: " + currentData.isDraw + " [F]", x, y += 15);
g2d.drawString("closeGap: " + currentData.closeGap + " [C]", x, y += 15);
g2d.drawString("save: [S], load: [L], hide image: [H], undo: [Z], hide drawing: [J]", x, y += 15);
g2d.drawString("[Q] change shape, current: " + shapeChoices[choice].shapeName(), x, y += 15);
g2d.drawString("mouse wheel to change stroke: " + currentData.stroke, x, y += 15);
g2d.drawString("draw point: L-click[start], M-click[ctrlPoint1], R-click[ctrlPoint2]",x, y += 15);
if (currentData instanceof GPData) {
g2d.drawString("gpData: " + currentGPType, x, y+=15);
}
}
if (!hideDrawing) {
//draw shapes
shapeDataList.draw(g2d);
//draw
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.75f));
currentData.draw(g2d);
}
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
if (!hideImage) { //[H] - hide image?
if (buffImage != null) {
g2d.drawImage(buffImage,200,200,null);
}
}
}
}
public void setImage(String path) {
try {
buffImage = ImageIO.read(new File(path));
} catch (Exception e) {
e.printStackTrace();
}
repaintFrame();
}
public void updateShapeData() {
if (!(currentData instanceof GPData) && startPoint == null) return;
if (currentData instanceof QuadCurveData) {
currentData.update(startPoint,currentPoint,ctrlPoint1);
} else if (currentData instanceof CubicCurveData){
currentData.update(startPoint,currentPoint,ctrlPoint1,ctrlPoint2);
} else if (currentData instanceof GPData) {
currentData.update(startPoint,endPoint,ctrlPoint1,ctrlPoint2,currentGPType);
} else {
currentData.update(startPoint,currentPoint);
}
repaintFrame();
}
public void load() {
shapeDataList = (ShapeDataList)deserialize(filePath);
System.out.println(shapeDataList);
}
public void save() {
serialize(filePath, shapeDataList);
}
private class MyKeyAdapter extends KeyAdapter {
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) { //add currentPoints to pointsList
if (currentData == null || currentData.isEmpty()) return;
if (currentData instanceof GPData) {
if (currentData.closeGap) {
GPData gpData = (GPData) currentData;
GPPoint gpPoint = gpData.get(0);
gpPoint.type = GPPoint.Type.LINE;
gpPoint.ctrlPoint1 = null;
gpPoint.ctrlPoint2 = null;
gpData.add(gpPoint.clone());
}
shapeDataList.add(currentData);
currentData = shapeChoices[choice].clone();
} else {
shapeDataList.add(currentData);
shapeChoices[choice] = currentData;
currentData = shapeChoices[choice].clone();
}
repaintFrame();
} else if (e.getKeyCode() == KeyEvent.VK_Z) {
if (currentData instanceof GPData) {
GPData gpData = (GPData) currentData;
gpData.removeLast();
repaintFrame();
} else {
shapeDataList.removeLast();
}
} else if (e.getKeyCode() == KeyEvent.VK_L) { //Load draw.txt TODO
load();
} else if (e.getKeyCode() == KeyEvent.VK_H) { //Hide / Unhide Image
hideImage = !hideImage; //toggle
} else if (e.getKeyCode() == KeyEvent.VK_S) { //Save TODO
save();
} else if (e.getKeyCode() == KeyEvent.VK_F) { // Toggle Draw / Fill
currentData.isDraw = !currentData.isDraw;
} else if (e.getKeyCode() == KeyEvent.VK_C) {
currentData.closeGap = !currentData.closeGap;
} else if (e.getKeyCode() == KeyEvent.VK_Q) {
choice = (++choice) % shapeChoices.length;
currentData = shapeChoices[choice].clone();
} else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
shapeDataList.clear();
} else if (e.getKeyCode() == KeyEvent.VK_J) {
hideDrawing = !hideDrawing;
} else if (e.getKeyCode() == KeyEvent.VK_K) {
gpPointChoice = (++gpPointChoice) % gpPointChoices.length;
currentGPType = gpPointChoices[gpPointChoice];
} else if (e.getKeyCode() == KeyEvent.VK_N) {
if (currentData instanceof GPData) {
updateShapeData();
repaintFrame();
}
} else if (e.getKeyCode() == KeyEvent.VK_I) {
hideInfo = !hideInfo;
} else if (e.getKeyCode() == KeyEvent.VK_1) {
updateColor(0);;
} else if (e.getKeyCode() == KeyEvent.VK_2) {
updateColor(1);
} else if (e.getKeyCode() == KeyEvent.VK_3) {
updateColor(2);
} else if (e.getKeyCode() == KeyEvent.VK_4) {
updateColor(3);
} else if (e.getKeyCode() == KeyEvent.VK_5) {
updateColor(4);
} else if (e.getKeyCode() == KeyEvent.VK_A) {
updateColor(((++colorIndex) % colors.length));
} else if (e.getKeyCode() == KeyEvent.VK_P) {
try {
shapeDataList.save(new BufferedImage(myPanel.getWidth(), myPanel.getHeight(), BufferedImage.TYPE_INT_ARGB));
} catch (Exception ex) {
}
} else if (e.getKeyCode() == KeyEvent.VK_O) {
try {
shapeDataList.saveSVG(new BufferedImage(myPanel.getWidth(), myPanel.getHeight(), BufferedImage.TYPE_INT_ARGB));
} catch (Exception ex) {
}
}
repaintFrame();
}
}
private void updateColor(int n) {
colorIndex = n;
currentData.color = colors[colorIndex];
}
private class MyMouseAdapter extends MouseAdapter {
@Override
public void mouseDragged(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
currentPoint = e.getPoint();
} else if (SwingUtilities.isMiddleMouseButton(e)) {
ctrlPoint1 = e.getPoint();
} else if (SwingUtilities.isRightMouseButton(e)) {
ctrlPoint2 = e.getPoint();
}
if (!(currentData instanceof GPData)) {
updateShapeData();
}
if (currentData instanceof GPData) {
GPData gpData = (GPData)currentData;
GPPoint gpPoint = gpData.getLast();
if (gpPoint != null) {
gpPoint.ctrlPoint1 = ctrlPoint1;
gpPoint.ctrlPoint2 = ctrlPoint2;
}
repaintFrame();
}
}
@Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
if (currentData instanceof GPData) {
endPoint = e.getPoint();
if (ctrlPoint1 == null) {
ctrlPoint1 = new Point(endPoint.x + -10, endPoint.y);
}
if (ctrlPoint2 == null) {
ctrlPoint2 = new Point(endPoint.x, endPoint.y -10);
}
return;
}
startPoint = e.getPoint();
if (ctrlPoint1 == null) {
ctrlPoint1 = new Point(startPoint.x + -10, startPoint.y);
}
if (ctrlPoint2 == null) {
ctrlPoint2 = new Point(startPoint.x, startPoint.y -10);
}
} else if (SwingUtilities.isMiddleMouseButton(e)) {
ctrlPoint1 = e.getPoint();
} else if (SwingUtilities.isRightMouseButton(e)) {
ctrlPoint2 = e.getPoint();
}
//updateShapeData();
}
@Override
public void mouseReleased(MouseEvent e) {
if (currentData instanceof GPData) {
return;
}
if (SwingUtilities.isLeftMouseButton(e)) {
endPoint = e.getPoint();
} else if (SwingUtilities.isMiddleMouseButton(e)) {
ctrlPoint1 = e.getPoint();
} else if (SwingUtilities.isRightMouseButton(e)) {
ctrlPoint2 = e.getPoint();
}
//currentPoint = null;
//addToList();
}
}
private class MyMouseWheelListener implements MouseWheelListener {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
int notches = -e.getWheelRotation();
int newStroke = currentData.stroke + notches;
if (newStroke <= 0) {
newStroke = 1;
} else if (newStroke > 10) {
newStroke = 10;
}
currentData.stroke = newStroke;
repaintFrame();
}
}
public Object deserialize(String path) {
Object object = null;
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File(path)));
object = ois.readObject();
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
return object;
}
public void serialize(String path, Object obj) {
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(path)));
oos.writeObject(obj);
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public CartoonDrawing(String title, int width, int height) {
super(title);
pack();
setSize(width,height);
setLocationRelativeTo(null);
setVisible(true);
addWindowListener(new CustomWindowListener());
addKeyListener(new MyKeyAdapter());
myPanel = new MyPanel();
getContentPane().add(myPanel);
}
public CartoonDrawing(String title) {
this(title,640,640);
load();
}
public static void main(String[] args) throws Exception {
dataPath = "data.txt";
Scanner scanner = new Scanner(System.in);
BufferedReader br = new BufferedReader(new FileReader(dataPath));
filePath = br.readLine().trim();
imagePath = br.readLine().trim();
CartoonDrawing app = new CartoonDrawing("Cartoon Drawing");
app.setImage(imagePath);
}
}