Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShapeController.java
More file actions
430 lines (368 loc) · 11.9 KB
/
Copy pathShapeController.java
File metadata and controls
430 lines (368 loc) · 11.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
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
package graphics.shapes.ui;
import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import graphics.shapes.SCircle;
import graphics.shapes.SCollection;
import graphics.shapes.SRectangle;
import graphics.shapes.SText;
import graphics.shapes.STriangle;
import graphics.shapes.Shape;
import graphics.shapes.attributes.ColorAttributes;
import graphics.shapes.attributes.FontAttributes;
import graphics.shapes.attributes.SelectionAttributes;
import graphics.ui.*;
public class ShapeController extends Controller
{
private int coordX;
private int coordY;
private boolean shiftdown;
private Shape shape;
private String action;
private Shape cp_shape;
private Shape Last_Selected_Copied_Shape;
private boolean justCoppied = false;
public ShapeController(Object newModel) {
super(newModel);
this.action = "";
}
@Override
public void mousePressed(MouseEvent e) {
/*
* System.out.println(e); Shape s = this.getTarget(e.getPoint());
* System.out.println(s); coordX = e.getX(); coordY = e.getY();
*/
coordX = e.getX();
coordY = e.getY();
Point pnt = e.getPoint();
this.shape = this.getTarget(pnt);
Shape lt = this.getLTBox(pnt);
Shape rb = this.getRBBox(pnt);
if (lt != null) {
this.shape = lt;
this.action = "lt";
} else if (rb != null) {
this.shape = rb;
this.action = "rb";
} else {
this.action = "";
if (shiftDown() == false) {
this.unselectAll();
}
if (this.shape != null) {
SelectionAttributes at = (SelectionAttributes) this.shape.getAttributes("select");
at.toggleSelection();
}
}
this.getView().repaint();
// System.out.println(s);
}
@Override
public void mouseReleased(MouseEvent e) {
Point pnt = new Point(-10, -10);
this.shape = this.getTarget(pnt);
this.action = "";
}
@Override
public void mouseClicked(MouseEvent e) {
coordX = e.getX();
coordY = e.getY();
Shape s = this.getTarget(e.getPoint());
if (s == null) {
this.unselectAll();
} else {
((SelectionAttributes) s.getAttributes("select")).toggleSelection();
}
System.out.println("j'agrippe :");
System.out.println(s);
this.action = "";
/*
* if (!shiftDown()) { for(Iterator it = ((SCollection)
* model).iterator(); it.hasNext();) { ((SelectionAttributes) ((Shape)
* it.next()).getAttributes("select")).selected = false; } } else {
* ((SelectionAttributes) target.getAttributes("select")).selected =
* true; }
*
* if (target != null) { for(Iterator it = ((SCollection)
* model).iterator(); it.hasNext();) { ((SelectionAttributes) ((Shape)
* it.next()).getAttributes("select")).toggleSelection(); } }
*/
}
@Override
public void mouseEntered(MouseEvent e) {
// System.out.println(e.getPoint());
// Quant la souris entre dans la fenêtre d'affichage
super.getView().requestFocus();
}
@Override
public void mouseExited(MouseEvent e) {
// Quant la souris sort de la fenêtre
}
@Override
public void mouseMoved(MouseEvent evt) {
}
@Override
public void mouseDragged(MouseEvent evt) {
Point pnt = evt.getPoint();
if (this.action == "lt" && ((SelectionAttributes) this.shape.getAttributes("select")).isSelected()) {
System.out.println("Je change la taille");
this.shape.setBounds(coordX - pnt.x, coordY - pnt.y);
this.shape.translate(pnt.x - coordX, pnt.y - coordY);
}
else if (this.action == "rb" && ((SelectionAttributes) this.shape.getAttributes("select")).isSelected()) {
System.out.println("Je change la taille");
this.shape.setBounds(pnt.x - coordX, pnt.y - coordY);
} else {
if (this.shape != null) {
this.shape.translate(pnt.x - coordX, pnt.y - coordY);
}
}
// view.ShapesView(model);
this.getView().repaint();
coordX = evt.getX();
coordY = evt.getY();
}
public boolean shiftDown()
{
return shiftdown;
}
private void translateSelected(int dx, int dy) {
SCollection model = (SCollection) this.getModel();
for (Iterator i = model.iterator(); i.hasNext();) {
Shape shape = (Shape) i.next();
if (((SelectionAttributes) (shape).getAttributes("select")).isSelected()) {
shape.translate(dx - coordX, dy - coordY);
System.out.println("Je déplace :");
System.out.println(shape);
}
}
}
private void changeSize(Point loc) {
Shape lt = getLTBox(loc);
Shape rb = getRBBox(loc);
if (lt != null && ((SelectionAttributes) lt.getAttributes("select")).isSelected()) {
System.out.println("Je change la taille");
lt.setBounds(coordX - loc.x, coordY - loc.y);
lt.translate(loc.x - coordX, loc.y - coordY);
}
if (rb != null && ((SelectionAttributes) rb.getAttributes("select")).isSelected()) {
System.out.println("Je change la taille");
rb.setBounds(loc.x - coordX, loc.y - coordY);
}
}
private Shape getTarget(Point pnt) {
SCollection model = (SCollection) this.getModel();
for (Iterator it = model.iterator(); it.hasNext();) {
Shape shape = (Shape) it.next();
if (shape.getBounds().contains(pnt)) {
return shape;
}
}
return null;
}
private Shape getLTBox(Point pnt) {
SCollection model = (SCollection) this.getModel();
Rectangle box;
Point loc;
for (Iterator<Shape> it = model.iterator(); it.hasNext();) {
Shape shape = (Shape) it.next();
shape.getBounds();
loc = shape.getLoc();
box = new Rectangle(loc.x - 6, loc.y - 6, 6, 6);
if (box.contains(pnt)) {
System.out.println(shape);
return shape;
}
}
return null;
}
private Shape getRBBox(Point pnt) {
SCollection model = (SCollection) this.getModel();
Rectangle box;
Rectangle bounds;
Point loc;
for (Iterator<Shape> it = model.iterator(); it.hasNext();) {
Shape shape = (Shape) it.next();
bounds = shape.getBounds();
loc = shape.getLoc();
box = new Rectangle(loc.x + bounds.width, loc.y + bounds.height, 6, 6);
System.out.println("Bijour");
if (box.contains(pnt)) {
System.out.println("J'ai trouvé");
return shape;
}
}
return null;
}
private boolean copyShape() {
SCollection model = (SCollection) this.getModel();
for (Iterator<Shape> i = model.iterator(); i.hasNext();) {
Shape shape = (Shape) i.next();
if (((SelectionAttributes) (shape).getAttributes("select")).isSelected()) {
Last_Selected_Copied_Shape = shape;
((SelectionAttributes) (shape).getAttributes("select")).toggleSelection();
cp_shape = addCopiedShape(shape);
return true;
}
}
return false;
}
private void pasteShape(KeyEvent evt) {
SCollection model = (SCollection) this.getModel();
if (cp_shape != null) {
if (justCoppied == false) {
Shape new_shape = addCopiedShape(Last_Selected_Copied_Shape);
new_shape.translate(20, 20);
model.add(new_shape);
System.out.println("was here");
} else {
cp_shape.translate(20, 20);
model.add(cp_shape);
}
justCoppied = false;
}
this.getView().repaint();
}
private void unselectAll() {
Shape s = null;
SCollection model = (SCollection) this.getModel();
for (Iterator i = model.iterator(); i.hasNext();) {
s = (Shape) i.next();
((SelectionAttributes) s.getAttributes("select")).unselect();
}
}
@Override
public void keyTyped(KeyEvent evt) {
}
@Override
public void keyPressed(KeyEvent evt) {
int key = evt.getKeyCode();
}
@Override
public void keyReleased(KeyEvent evt) {
int key = evt.getKeyCode();
if ((evt.getKeyCode() == KeyEvent.VK_C) && ((evt.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
copyShape();
System.out.println("coppied Shape");
}
if ((evt.getKeyCode() == KeyEvent.VK_V) && ((evt.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
pasteShape(evt);
System.out.println("pasted Shape");
}
if ((evt.getKeyCode() == KeyEvent.VK_A) && ((evt.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
SelectAll();
System.out.println("selected all Shapes");
}
if ((evt.getKeyCode() == KeyEvent.VK_R) && ((evt.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
removeAll();
System.out.println("selected all Shapes");
}
if((evt.getKeyCode()==KeyEvent.VK_BACK_SPACE)){
deleteselected();
}
}
private Shape addCopiedShape(Shape shape) {
Shape new_shape;
if (shape instanceof SRectangle) {
new_shape = createNewRect(shape);
justCoppied = true;
return (SRectangle) new_shape;
} else if (shape instanceof SCircle) {
new_shape = createNewCircle(shape);
justCoppied = true;
return (SCircle) new_shape;
} else if (shape instanceof STriangle) {
new_shape = createNewTriangle(shape);
justCoppied = true;
return (STriangle) new_shape;
} else if (shape instanceof SText) {
new_shape = createNewText(shape);
justCoppied = true;
return (SText) new_shape;
} else if (shape instanceof SCollection) {
new_shape = createNewCollection((SCollection)shape);
justCoppied = true;
return (SCollection) new_shape;
}
System.out.println("no way");
return null;
}
private Shape createNewRect(Shape _shape) {
Shape new_shape;
new_shape = new SRectangle((SRectangle) _shape);
new_shape.addAttributes(new ColorAttributes((ColorAttributes) _shape.getAttributes("color")));
new_shape.addAttributes(new SelectionAttributes());
return new_shape;
}
private Shape createNewCircle(Shape _shape) {
Shape new_shape;
new_shape = new SCircle((SCircle) _shape);
new_shape.addAttributes(new ColorAttributes((ColorAttributes) _shape.getAttributes("color")));
new_shape.addAttributes(new SelectionAttributes());
return new_shape;
}
private Shape createNewTriangle(Shape _shape) {
Shape new_shape;
new_shape = new STriangle((STriangle)_shape);
new_shape.addAttributes(new ColorAttributes((ColorAttributes) _shape.getAttributes("color")));
new_shape.addAttributes(new SelectionAttributes());
return new_shape;
}
private Shape createNewText(Shape _shape) {
Shape new_shape;
new_shape = new SText((SText) _shape);
new_shape.addAttributes(new ColorAttributes((ColorAttributes) _shape.getAttributes("color")));
new_shape.addAttributes(new SelectionAttributes());
new_shape.addAttributes(new FontAttributes());
return new_shape;
}
private Shape createNewCollection(SCollection _shape) {
SCollection new_Collection = new SCollection();
new_Collection.addAttributes(new SelectionAttributes());
Shape s;
for (Iterator<Shape> i = _shape.iterator(); i.hasNext();) {
s = (Shape) i.next();
if (s instanceof SRectangle) {
new_Collection.add(createNewRect(s));
} else if (s instanceof SCircle) {
new_Collection.add(createNewCircle(s));
} else if (s instanceof STriangle) {
new_Collection.add(createNewTriangle(s));
} else if (s instanceof SText) {
new_Collection.add(createNewText(s));
} else if (s instanceof SCollection) {
new_Collection.add(createNewCollection((SCollection) s));
}
}
return new_Collection;
}
private void SelectAll(){
SCollection model = (SCollection) this.getModel();
Shape s;
for (Iterator<Shape> i = model.iterator(); i.hasNext();) {
s = (Shape) i.next();
((SelectionAttributes) s.getAttributes("select")).toggleSelection();
this.getView().repaint();
}
}
private void removeAll(){
SCollection model = (SCollection) this.getModel();
model.shapes=new ArrayList<>();
this.getView().repaint();
}
private void deleteselected(){
SCollection model = (SCollection) this.getModel();
for (Iterator<Shape> i = model.iterator(); i.hasNext();) {
Shape shape = (Shape) i.next();
if (((SelectionAttributes) (shape).getAttributes("select")).isSelected()) {
model.shapes.remove(shape);
}
this.getView().repaint();
}
}
}
You can’t perform that action at this time.
