-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjeto1App.java
More file actions
493 lines (414 loc) · 13.1 KB
/
Projeto1App.java
File metadata and controls
493 lines (414 loc) · 13.1 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
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.Point;
import figures.*;
import button_.*;
import toolbox.*;
class Projeto1App
{
public static void main(String[] args)
{
ListFrame frame = new ListFrame();
frame.setVisible(true);
}
}
class ListFrame extends JFrame
{
int SIZE = 8;
int defaultH = 80;
int defaultW = 80;
int windowH = 500;
int windowW = 500;
int pos = -1;
int buttonId = -1;
Point2D[] lastPoints = new Point2D[3];
Rectangle2D.Double[] points = { new Rectangle2D.Double(50, 50, SIZE, SIZE),
new Rectangle2D.Double(150, 100, SIZE, SIZE),
new Rectangle2D.Double(100, 75, SIZE, SIZE) };
Point pointOfmouse = null;
Figure selectedFigure = null;
Figure auxFigure = null;
Point prevPt = null;
ArrayList<Button_> buttons = new ArrayList<Button_>();
Toolbox mainToolbox;
ArrayList<Figure> fg = new ArrayList<Figure>();
ListFrame()
{
buttons.add( new Button_(1, new Rect(1, 2, 3, 4, Color.BLACK, Color.BLACK)) );
buttons.add( new Button_(2, new Roundrect(1, 2, 3, 4, Color.BLACK, Color.BLACK)) );
buttons.add( new Button_(3, new Ellipse(1, 2, 3, 4, Color.BLACK, Color.BLACK)) );
buttons.add( new Button_(4, new Triangle(1, 2, 3, -1, Color.BLACK, Color.BLACK)) );
mainToolbox = new Toolbox(10, 50, buttons);
try
{
FileInputStream f = new FileInputStream("proj.bin");
ObjectInputStream o = new ObjectInputStream(f);
this.fg = (ArrayList<Figure>)o.readObject();
o.close();
}catch(Exception x)
{
System.out.println("ERRO! ao abrir arquivo");
System.out.println(x.getMessage());
}
this.addWindowListener
(
new WindowAdapter()
{
public void windowClosing (WindowEvent e)
{
try
{
FileOutputStream f = new FileOutputStream("proj.bin");
ObjectOutputStream o = new ObjectOutputStream(f);
o.writeObject(fg);
o.flush();
o.close();
}
catch(Exception x)
{
System.out.println("ERRO! ao salvar arquivo");
System.out.println(x.getMessage());
}
System.exit(0);
}
}
);
repaint();
this.addKeyListener(
new KeyAdapter() {
public void keyPressed(KeyEvent evt)
{
int x = (int)pointOfmouse.getX();
int y = (int)pointOfmouse.getY();
if( evt.getKeyChar() == 'r' || evt.getKeyChar() == 'R')
{
fg.add( new Rect(x, y, defaultW, defaultH, Color.BLACK, Color.WHITE) );
repaint();
}
else if( evt.getKeyChar() == 'e' || evt.getKeyChar() == 'E')
{
fg.add( new Ellipse(x, y, defaultW, defaultH, Color.BLACK, Color.WHITE) );
repaint();
}
else if( evt.getKeyChar() == 't' || evt.getKeyChar() == 'T' )
{
fg.add(new Triangle(x, y, defaultW, -1, Color.BLACK, Color.WHITE) );
repaint();
}else if( evt.getKeyChar() == 'w' || evt.getKeyChar() == 'W')
{
fg.add( new Roundrect(x, y, defaultW, defaultH, Color.BLACK, Color.WHITE) );
repaint();
}
else if( (evt.getKeyCode() == KeyEvent.VK_DELETE) ||
evt.getKeyChar() == 'x' || evt.getKeyChar() == 'X')
{
if( selectedFigure != null){
fg.remove(selectedFigure);
auxFigure = null;
selectedFigure = null;
repaint();
}
}
else if( evt.getKeyChar() == 'f' || evt.getKeyChar() == 'F')
{
if( selectedFigure != null){
Color newColor = JColorChooser.showDialog(null, "Choose a color", selectedFigure.BorderColor);
selectedFigure.BorderColor = newColor;
repaint();
}
}
else if( evt.getKeyChar() == 'b' || evt.getKeyChar() == 'B')
{
if( selectedFigure != null){
Color newColor = JColorChooser.showDialog(null, "Choose a color", selectedFigure.BorderColor);
selectedFigure.BckgColor = newColor;
repaint();
}
}
else if( evt.getKeyChar() == 's' || evt.getKeyChar() == 'S' )
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Salvar no formato SVG. Defina o local e o nome do arquivo");
int userSelection = fileChooser.showSaveDialog(null);
if (userSelection == JFileChooser.APPROVE_OPTION)
{
File fileToSave = fileChooser.getSelectedFile();
CreateSVG(fg, fileToSave.getAbsolutePath() );
}
}
else if (evt.getKeyCode() == KeyEvent.VK_UP)
{
if( selectedFigure != null){
selectedFigure.y -= 1;
repaint();
}
}
else if(evt.getKeyCode() == KeyEvent.VK_DOWN)
{
if( selectedFigure != null ){
selectedFigure.y += 1;
repaint();
}
}
else if (evt.getKeyCode() == KeyEvent.VK_LEFT)
{
if( selectedFigure != null){
selectedFigure.x -= 1;
repaint();
}
}
else if (evt.getKeyCode() == KeyEvent.VK_RIGHT)
{
if( selectedFigure != null){
selectedFigure.x += 1;
repaint();
}
}
}
}
);
this.addMouseListener(
new MouseAdapter(){
public void mousePressed(MouseEvent evt)
{
pointOfmouse = evt.getPoint();
for( Button_ bt: buttons){
if( bt.clicked( (int)pointOfmouse.getX(), (int)pointOfmouse.getY() ) )
{
buttonId = bt.id;
repaint();
return;
}
}
if( buttonId != -1)
{
int x = (int)pointOfmouse.getX();
int y = (int)pointOfmouse.getY();
if(buttonId == 1)
fg.add( new Rect(x, y, defaultW, defaultH, Color.BLACK, Color.WHITE) );
else if( buttonId == 2)
fg.add( new Roundrect(x, y, defaultW, defaultH, Color.BLACK, Color.WHITE) );
else if( buttonId == 3)
fg.add( new Ellipse(x, y, defaultW, defaultH, Color.BLACK, Color.WHITE) );
else if(buttonId == 4)
fg.add(new Triangle(x, y, defaultW, -1, Color.BLACK, Color.WHITE) );
buttonId = -1;
repaint();
return;
}
boolean flag1 = false;
boolean flag2 = false;
for (int i = 0; i < points.length; i++)
{
if (points[i].contains(prevPt))
{
pos = i;
flag1 = true;
//System.out.printf("point %d\n",pos);
for(int j = 0; j < 3; j++){
lastPoints[j] = new Point2D.Double(points[j].getX(), points[j].getY());
}
break;
}
}
ListIterator<Figure> li = fg.listIterator(fg.size());
Figure fig = null;
while( li.hasPrevious() )
{
fig = li.previous();
if( fig.clicked( (int)prevPt.getX(), (int)prevPt.getY() ) )
{
flag2 = true;
selectedFigure = fig;
int index = fg.indexOf(selectedFigure);
fg.remove(index);
fg.add(selectedFigure);
break;
}
}
if( !flag1 && !flag2){
selectedFigure = null;
}
repaint();
}
public void mouseReleased(MouseEvent evt)
{
pos = -1;
}
}
);
this.addMouseMotionListener(
new MouseMotionAdapter(){
public void mouseDragged(MouseEvent evt)
{
Point currentPt = evt.getPoint();
if (pos == -1)
return;
if( pos != 2)
{
points[pos].x = currentPt.x;
points[pos].y = currentPt.y;
int otherEnd = (pos==1)?2:1;
//System.out.printf("otherEnd %d\n",otherEnd);
double newPoint2X = points[otherEnd].getX() + (points[pos].getX() - points[otherEnd].getX())/2;
double newPoint2Y = points[otherEnd].getY() + (points[pos].getY() - points[otherEnd].getY())/2;
points[2].x = newPoint2X;
points[2].y = newPoint2Y;
selectedFigure.w = (int)Math.abs(points[1].getCenterX()-points[0].getCenterX());
//Tratamento especial para o triangulo
if( selectedFigure.h != -1)
selectedFigure.h = (int)Math.abs(points[1].getCenterY()-points[0].getCenterY());
}
else
{
double deltaX = currentPt.x - lastPoints[2].getX();
double deltaY = currentPt.y - lastPoints[2].getY();
for(int j = 0; j < 3; j++){
points[j].x = lastPoints[j].getX() + deltaX;
points[j].y = lastPoints[j].getY() + deltaY;
}
//Tratamento especial para o triangulo
if( selectedFigure.h == -1)
{
selectedFigure.x = (int)points[0].getCenterX()+(selectedFigure.w/2);
selectedFigure.y = (int)points[0].getCenterY()-selectedFigure.w-SIZE;
}
else
{
selectedFigure.x = (int)points[0].getCenterX();
selectedFigure.y = (int)points[0].getCenterY();
}
}
repaint();
}
public void mouseMoved(MouseEvent evt)
{
pointOfmouse = evt.getPoint();
prevPt = pointOfmouse;
ListIterator<Figure> li = fg.listIterator(fg.size());
Figure fig = null;
while( li.hasPrevious() )
{
fig = li.previous();
if( fig.clicked((int)prevPt.getX(), (int)prevPt.getY()) ){
auxFigure = fig;
repaint();
break;
}
else
{
auxFigure = null;
}
}
repaint();
}
}
);
this.getContentPane().setBackground(Color.WHITE);
this.setTitle("Projeto1App");
this.setSize(500, 500);
}
public void paint(Graphics g)
{
super.paint(g);
mainToolbox.Show(g, buttonId);
for( Figure fig: this.fg)
{
fig.paint(g, fig.equals(selectedFigure) );
}
if( auxFigure != null ){
rectFormouseOverFigure(g);
}
if( selectedFigure != null){
rectForselectedFigure(g);
}
}
public void rectFormouseOverFigure(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
BasicStroke bs1 = new BasicStroke(3, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER);
g2d.setStroke(bs1);
g2d.setColor(Color.RED);
//Tratamento especial para o triangulo
if( auxFigure.h == -1 )
{
int w = auxFigure.w;
g2d.drawRect(auxFigure.x-(w/2)-5, auxFigure.y-5, auxFigure.w+10, auxFigure.w+10);
}
else
g2d.drawRect(auxFigure.x-5,auxFigure.y-5, auxFigure.w+10,auxFigure.h+10);
}
public void rectForselectedFigure(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
BasicStroke bs1 = new BasicStroke(3, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER);
g2d.setStroke(bs1);
points = selectedFigure.GetPointsOfSelection();
g2d.setColor(Color.BLACK);
for (int i = 0; i < points.length; i++)
g2d.fill(points[i]);
}
public void CreateSVG(ArrayList<Figure> figs, String fileName)
{
String format = ".svg";
try
{
File Stream = new File( fileName+format );
if ( !Stream.createNewFile() ) {
System.out.println("Arquivo já existe\n");
}
FileWriter Writer = new FileWriter(fileName+format);
Writer.write("<svg width=\"1500\" height=\"1000\">\n");
Writer.write(" <rect width=\"100%\" height=\"100%\" fill=\"white\" />\n");
for( Figure fig: figs)
{
String ForergbColor = String.format("rgb(%d,%d,%d)", fig.BorderColor.getRed(), fig.BorderColor.getGreen(), fig.BorderColor.getBlue() );
String BackrgbColor = String.format("rgb(%d,%d,%d)", fig.BckgColor.getRed(), fig.BckgColor.getGreen(), fig.BckgColor.getBlue() );
if(fig instanceof Rect)
{
Writer.write("<rect x=\""+ fig.x +"\" y=\""+ fig.y +"\" width=\""+ fig.w +
"\" height=\"" + fig.h + "\" style=\"fill:"+ BackrgbColor +
";stroke-width:3;stroke:"+ ForergbColor +"\" />\n");
}
else if( fig instanceof Roundrect)
{
Writer.write("<rect x=\""+ fig.x +"\" y=\""+ fig.y +
"\" rx=\"10\" ry=\"10\" width=\""+ fig.w +
"\" height=\"" + fig.h + "\" style=\"fill:"+ BackrgbColor +
";stroke-width:3;stroke:"+ ForergbColor +"\" />\n");
}
else if( fig instanceof Ellipse)
{
Writer.write("<ellipse cx=\""+ (fig.x + (fig.w*0.5)) +"\" cy=\""+ (fig.y + (fig.h*0.5))+ "\" rx=\""+ (fig.w*0.5) + "\"" +
" ry=\""+ (fig.h*0.5) + "\""+
" style=\"fill:"+ BackrgbColor +
";stroke-width:3;stroke:"+ ForergbColor +"\" />\n");
}
else if( fig instanceof Triangle)
{
String points = String.format("%d,%d %d,%d %d,%d",
fig.x, fig.y,
fig.x-(int)(fig.w/2.0), fig.y+fig.w,
fig.x+(int)(fig.w/2.0), fig.y+fig.w);
Writer.write("<polygon points=\""+ points +"\" "+
"style=\"fill:"+ BackrgbColor +
";stroke-width:3;stroke:"+ ForergbColor +"\" />\n");
}
}
Writer.write("</svg>");
Writer.close();
}
catch (IOException e)
{
System.out.println("Ocorreu um erro.");
e.printStackTrace();
}
}
}