Bonjour,
Dans le cadre d'une étude des patterns, et plus précisément du Composite Pattern,
J'ai été amené à implémenter une application simple avec le composite pattern, avec JavaFx
Je comprends le concept du pattern, cependant j'ai un problème dans le déplacement des groupes de formes
Donc j'ai mes leafs qui sont Rectangle et Circle
J'ai aussi ma classe abstraite qui est donc le Component
J'ai créer le composite CompositeShape.java
Dans les méthodes move et moveTo, les formes groupées partent dans tout les sens, or je trouvais logique et assez simple la redéfinition de ces méthodes pourtant mon code n'est pas fonctionnel. Je suppose donc que dans ma classe CompositeShape, il y a une erreur quelque part, du moins, c'est mon avis. Si vous pouviez m'aider ou du moins m'induire dans la résolution du problème, je vous en serais reconnaissant.
Merci d'avance
Shape.java
CompositeShape.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package model; import javafx.scene.canvas.GraphicsContext; import javafx.scene.paint.Color; public abstract class Shape { protected int x; // Le coin supérieur gauche du carré englobant protected int y; protected Color color; public int getX() { return x; } public int getY() { return y; } public void move(int directionX, int directionY) { x += directionX; y += directionY; } public void moveTo(int x, int y) { this.x = x; this.y = y; } public void setColor(Color c) { color = c; } public Color getColor() { return color; } abstract public int getWidth(); abstract public int getHeight(); abstract public boolean isOn(int x, int y); abstract public void paintOn(GraphicsContext gc); }
DrawingPane.java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package model; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javafx.scene.canvas.GraphicsContext; import javafx.scene.paint.Color; public class CompositeShape extends Shape implements Iterable<Shape> { List<Shape> compoShapes = new ArrayList <>(); public CompositeShape(){ } public CompositeShape(CompositeShape compoS) { for(Shape s : compoS) this.compoShapes.add(s); } public boolean content(Shape s){ for(Shape sh : compoShapes){ if(s == sh) return true; } return false; } public int getLength(){ return compoShapes.size(); } public void add(Shape s){ this.compoShapes.add(s); } public void remove(Shape s){ this.compoShapes.remove(s); } public void clear(){ this.compoShapes.clear(); } @Override public Iterator<Shape> iterator() { return compoShapes.iterator(); } @Override public int getX() { int xmin = 0; if(compoShapes.size() > 0){ xmin = compoShapes.get(0).getX(); for(Shape f:compoShapes){ xmin = Math.min(xmin, f.getX()); } } return xmin; } @Override public int getY() { int ymin = 0; if(compoShapes.size() > 0){ ymin = compoShapes.get(0).getY(); for(Shape f:compoShapes){ ymin = Math.min(ymin, f.getY()); } } return ymin; } // @Override // public int getWidth() { // int xmax = 0; // if(compoShapes.size() > 0){ // xmax = compoShapes.get(0).getX() + compoShapes.get(0).getWidth() ; // for(Shape f:compoShapes){ // xmax = Math.max(xmax, f.getX()+f.getWidth()); // } // } // return xmax - getX(); // } // // @Override // public int getHeight() { // int ymax = 0 ; // // if(compoShapes.size() > 0){ // ymax = compoShapes.get(0).getY() + compoShapes.get(0).getHeight() ; // for(Shape f:compoShapes){ // ymax = Math.max(ymax, f.getY()+f.getHeight()); // } // } // return ymax - getY(); // } @Override public void move(int directionX, int directionY) { for(Shape s:compoShapes){ s.move(directionX, directionY); } } @Override public void moveTo(int x, int y) { // for(Shape s : compoShapes){ // s.moveTo(x, y); // } boolean first=true; int firstX=0; int firstY=0; for(Shape s : compoShapes){ if(first){ firstX=s.getX(); firstY =s.getY(); s.moveTo(x, y); firstX=s.getX() - firstX; firstY=s.getY() - firstY; first=false; } else //s.moveTo(firstX+s.getX(), firstY+s.getY()); s.move(firstX, firstY); } } @Override public int getWidth() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public int getHeight() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public boolean isOn(int x, int y) { boolean result=false; for(Shape f:compoShapes) { if(f.isOn(x,y)) result=true; } return result; } @Override public void paintOn(GraphicsContext gc) { for(Shape s : compoShapes ) s.paintOn(gc); } @Override public void setColor(Color c){ for(Shape s : compoShapes){ s.setColor(c); } } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package view; import java.awt.Point; import java.util.ArrayList; import java.util.List; import javafx.scene.Cursor; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.input.MouseEvent; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import model.Shape; import model.Circle; import model.Rectangle; import model.ShapeList; import javafx.scene.paint.Paint; import model.CompositeShape; public class DrawingPane extends Pane { int startX, startY; int x0, y0; Mode mode = Mode.FREE; Shape selection; CompositeShape compoS = new CompositeShape(); Color defaultColor = Color.CHARTREUSE; private final Canvas canvas = new Canvas(); private final ShapeList shapeList; public DrawingPane(ShapeList list) { this.shapeList = list; this.setOnMousePressed((MouseEvent e) -> { startX = (int) e.getX(); startY = (int) e.getY(); selection = null; for (Shape f : shapeList) { if (f.isOn((int) e.getX(), (int) e.getY())) { selection = f; x0 = selection.getX(); y0 = selection.getY(); } } if (mode == Mode.GROUP && !compoS.content(selection) && shapeList.content(selection)){ compoS.add(selection); } if(mode == Mode.DONE){ addGroup(); } if (mode == Mode.SPLIT){ splitGroup(selection); mode = Mode.FREE; } }); this.setOnMouseReleased((MouseEvent e) -> { int stopX = (int) e.getX(); int stopY = (int) e.getY(); if (stopX != startX && stopY != startY) { if (mode == Mode.RECTANGLE) { shapeList.add(new Rectangle(startX, startY, stopX - startX, stopY - startY, defaultColor)); } else if (mode == Mode.CIRCLE) { shapeList.add(new Circle(startX, startY, Math.min(stopX - startX, stopY - startY) / 2, defaultColor)); } } else if (selection != null) { selection.setColor(defaultColor); } repaint(); }); this.setOnMouseDragged((MouseEvent e) -> { if (mode == Mode.MOVE && selection != null) { selection.moveTo(x0 + (int) e.getX() - startX, y0 + (int) e.getY() - startY); repaint(); } else if (mode == Mode.CIRCLE || mode == Mode.RECTANGLE) { drawGhost((int) e.getX(), (int) e.getY()); } }); canvas.setCursor(Cursor.CROSSHAIR); super.getChildren().add(canvas); } void setMode(Mode mode) { this.mode = mode; } void setDefaultColor(Color color) { defaultColor = color; } void drawGhost(int xir, int yir) { repaint(); GraphicsContext gc = canvas.getGraphicsContext2D(); Paint p = gc.getStroke(); gc.setStroke(Color.GREY); gc.strokeRect(startX, startY, xir - startX, yir - startY); gc.setStroke(p); } public void repaint() { GraphicsContext gc = canvas.getGraphicsContext2D(); gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight()); for (Shape f : shapeList) { f.paintOn(gc); } } @Override protected void layoutChildren() { canvas.setWidth(getWidth()); canvas.setHeight(getHeight()); repaint(); } public void addGroup(){ if(compoS.getLength()>1){ shapeList.add(new CompositeShape(compoS)); for(Shape s : compoS) shapeList.remove(s); compoS.clear(); } } public void splitGroup(Shape shape){ if (shape instanceof CompositeShape){ CompositeShape cs = new CompositeShape(); cs = (CompositeShape) shape; for(Shape s : cs){ shapeList.add(s); } shapeList.remove(shape); cs = null; } else { throw new UnsupportedOperationException("Ceci n'est pas un groupe de formes"); } } }
Partager