GraphicsApp API
Eine überarbeitete und angepasste Variante der originalen GraphicsApp-Umgebung
Compound.java
gehe zur Dokumentation dieser Datei
1package de.ur.mi.oop.graphics;
2
3import de.ur.mi.oop.app.GraphicsApp;
4import de.ur.mi.oop.colors.Color;
5import de.ur.mi.oop.colors.Colors;
6
7import java.util.ArrayList;
8import java.util.Iterator;
9
10/**
11 * Diese Klasse definiert ein grafisches Objekt, das aus einer Sammlung von anderen
12 * grafische Objekten besteht. Nach dem Aufbau können die internen Objekte wie eine Einheit
13 * behandelt werden.
14 */
15public class Compound extends GraphicsObject {
16
17 ///////////////////////////////////////////////////////////////////////////
18 // Members and constants
19 ///////////////////////////////////////////////////////////////////////////
20
21 private ArrayList<GraphicsObject> objects;
22 private Rectangle backgroundRectangle;
23
24 ///////////////////////////////////////////////////////////////////////////
25 // Constructors
26 ///////////////////////////////////////////////////////////////////////////
27
28 /**
29 * Erzeugt ein neues Compound-Objekt ohne interne Komponenten an der angegebenen Position.
30 *
31 * @param x Die x-Position in Pixeln
32 * @param y Die y-Position in Pixeln
33 */
34 public Compound(float x, float y) {
35 super(x, y);
37 this.objects = new ArrayList<>();
38 this.backgroundRectangle = new Rectangle(getXPos(), getYPos(), getWidth(), getHeight());
39 }
40
41 /**
42 * Erzeugt ein neues Compound-Objekt ohne interne Komponenten an der angegebenen Position
43 * mit einer Hintergrundfarbe.
44 *
45 * @param x Die x-Position in Pixeln
46 * @param y Die y-Position in Pixeln
47 * @param backgroundColor Hintergrundfarbe
48 */
49 public Compound(float x, float y, Color backgroundColor) {
50 this(x, y);
51 this.setColor(backgroundColor);
52 updateBackground();
53 }
54
55 /**
56 * Erzeugt ein neues Compound Objekt ohne interne Komponenten an der mittels Point-Objekt angegebenen Position.
57 *
58 * @param position Point-Objekt, welches die Stelle markiert
59 */
60 public Compound(Point position) {
61 this(position.getXPos(), position.getYPos());
62 }
63
64 /**
65 * Erzeugt ein neues Compound-Objekt ohne interne Komponenten an der mittels Point-Objekt angegebenen Position
66 * mit einer Hintergrundfarbe.
67 *
68 * @param position Point-Objekt, welches die Stelle markiert
69 * @param backgroundColor Hintergrundfarbe
70 */
71 public Compound(Point position, Color backgroundColor) {
72 this(position.getXPos(), position.getYPos(), backgroundColor);
73 }
74
75 /**
76 * Erzeugt ein neues Compound-Objekt ohne interne Komponenten an der Stelle (0, 0).
77 */
78 public Compound() {
79 this(0.0f, 0.0f);
80 }
81
82 ///////////////////////////////////////////////////////////////////////////
83 // Drawing and Updates
84 ///////////////////////////////////////////////////////////////////////////
85
86 /**
87 * Zeichnet das Compound und alle hinzugefügten Objekte.
88 */
89 public void draw() {
90 updateBackground();
91 drawBackground();
92
93 for (GraphicsObject object : this.objects) {
95 }
96 }
97
98 private void updateBackground() {
99 backgroundRectangle.setXPos(getXPos());
100 backgroundRectangle.setYPos(getYPos());
101 backgroundRectangle.setWidth(getWidth());
102 backgroundRectangle.setHeight(getHeight());
103 backgroundRectangle.setColor(this.getColor());
104 }
105
106 // TODO: Add possibility of transparent background rectangle with only stroke
107 private void drawBackground() {
108 if (backgroundRectangle != null) {
109 GraphicsApp.getApp().addToDrawBuffer(backgroundRectangle);
110 }
111 }
112
113 ///////////////////////////////////////////////////////////////////////////
114 // Content Setting and Getting
115 ///////////////////////////////////////////////////////////////////////////
116
117 /**
118 * Fügt dem Compound ein Grafikobjekt hinzu. Das hinzugefügte Objekt behält seine
119 * absolute Position.
120 *
121 * @param object Das Grafikobjekt, das hinzugefügt werden soll
122 */
123 public void add(GraphicsObject object) {
124 this.objects.add(object);
125 }
126
127 /**
128 * Fügt dem Compound ein Grafikobjekt hinzu. Das hinzugefügte Objekt wird positioniert
129 * bezogen auf die Position der Verbindung.
130 *
131 * @param object Das Grafikobjekt, das hinzugefügt werden soll
132 */
133 public void addRelative(GraphicsObject object) {
134 this.add(object);
135 object.setPosition(this.getXPos() + object.getXPos(), this.getYPos() + object.getYPos());
136 }
137
138 /**
139 * Liefert das Objekt am angegebenen Index, die Nummerierung geht von hinten nach vorne
140 * (oberstes Objekt hat den höchsten Index).
141 *
142 * @param index Der Index des zurückzugebenden Objekts
143 * @return Das Objekt mit dem angegebenen Index
144 */
145 public GraphicsObject get(int index) {
146 return this.objects.get(index);
147 }
148
149 /**
150 * Liefert das oberste Grafikobjekt, das den Punkt (x, y) enthält, oder
151 * null, wenn kein solches Objekt existiert. Die Koordinaten müssen Absolutwerte sein.
152 *
153 * @param x Die x-Koordinate des Punktes
154 * @param y Die y-Koordinate des Punktes
155 * @return Das Objekt an der angegebenen Stelle oder null, wenn kein solches Objekt vorhanden ist.
156 */
157 public GraphicsObject getObjectAt(float x, float y) {
158 for (int i = this.objects.size() - 1; i >= 0; --i) {
159 GraphicsObject object = this.objects.get(i);
160 if (object.hitTest(x, y)) {
161 return object;
162 }
163 }
164
165 return null;
166 }
167
168 /**
169 * Liefert das oberste Grafikobjekt, das den Punkt an der Stelle Point enthält, oder
170 * null, wenn kein solches Objekt existiert. Die Koordinaten müssen Absolutwerte sein.
171 *
172 * @param point Die Koordinaten des Punktes
173 * @return Das Objekt an der angegebenen Stelle oder null, wenn kein solches Objekt vorhanden ist.
174 */
176 return this.getObjectAt(point.getXPos(), point.getYPos());
177 }
178
179 /**
180 * Entfernt alle Objekte aus diesem Compound.
181 */
182 public void removeAll() {
183 this.objects.clear();
184 this.backgroundRectangle = null;
185 setColor(null);
186 }
187
188 /**
189 * Entfernt ein Objekt aus diesem Compound.
190 *
191 * @param object Das zu entfernende Objekt
192 */
193 public void remove(GraphicsObject object) {
194 this.objects.remove(object);
195 }
196
197 /**
198 * Entfernt ein Objekt aus diesem Compound.
199 *
200 * @param index Der Index des zu entfernenden Objekts
201 */
202 public void remove(int index) {
203 this.objects.remove(index);
204 }
205
206 /**
207 * Liefert einen Iterator, der die Elemente innerhalb dieses Compounds
208 * in der Standardrichtung durchläuft, von hinten nach vorne.
209 *
210 * @return Ein Iterator, der durch die Elemente des Compounds iteriert, von
211 * hinten nach vorne
212 */
213 public Iterator<GraphicsObject> iterator() {
214 return this.objects.iterator();
215 }
216
217 /**
218 * Überprüft, ob sich ein Punkt "innerhalb" des Compounds befindet,
219 * was bedeutet, dass er sich innerhalb einer der Komponenten befindet.
220 *
221 * @param x Die x-Koordinate des zu prüfenden Punktes.
222 * @param y Die y-Koordinate des zu prüfenden Punktes.
223 * @return true, wenn sich der Punkt (x, y) innerhalb des Compounds befindet,
224 * und false andernfalls
225 */
226 public boolean contains(float x, float y) {
227 return this.getObjectAt(x, y) != null;
228 }
229
230 /**
231 * Liefert die Anzahl der Objekte.
232 *
233 * @return Die Anzahl der Objekte in diesem Compound.
234 */
235 public int size() {
236 return this.objects.size();
237 }
238
239 ///////////////////////////////////////////////////////////////////////////
240 // Geometry Related Getting and Setting
241 ///////////////////////////////////////////////////////////////////////////
242
243 public float getWidth() {
244 float maxRightBorder = -Float.MAX_VALUE;
245
246 if (objects.isEmpty()) {
247 return 0;
248 }
249
250 for (GraphicsObject object : this.objects) {
251 float rightBorder = object.getRightBorder();
252 if (rightBorder > maxRightBorder) {
253 maxRightBorder = rightBorder;
254 }
255 }
256
257 return maxRightBorder - this.getXPos();
258 }
259
260 public float getHeight() {
261 float maxBottomBorder = -Float.MAX_VALUE;
262
263 if (objects.isEmpty()) {
264 return 0;
265 }
266
267 for (GraphicsObject object : this.objects) {
268 float bottomBorder = object.getBottomBorder();
269 if (bottomBorder > maxBottomBorder) {
270 maxBottomBorder = bottomBorder;
271 }
272 }
273
274 return maxBottomBorder - this.getYPos();
275 }
276
277 public void setXPos(float x) {
278 float dx = x - this.getXPos();
279 super.setXPos(x);
280
281 for (GraphicsObject object : this.objects) {
282 object.move(dx, 0);
283 }
284 }
285
286 public void setYPos(float y) {
287 float dy = y - this.getYPos();
288 super.setYPos(y);
289
290 for (GraphicsObject object : this.objects) {
291 object.move(0, dy);
292 }
293
294 }
295
296 public void setPosition(float x, float y) {
297 float dx = x - this.getXPos();
298 float dy = y - this.getYPos();
299
300 setXPos(x);
301 setYPos(y);
302
303 for (GraphicsObject object : this.objects) {
304 object.move(dx, dy);
305 }
306 }
307
308 public void setPosition(Point point) {
309 this.setPosition(point.getXPos(), point.getYPos());
310 }
311
312
313
314 ///////////////////////////////////////////////////////////////////////////
315 // Utility Methods
316 ///////////////////////////////////////////////////////////////////////////
317
318 /**
319 * Bewegt das Compound und alle darin enthaltenen Objekte
320 * um die angegebene Verschiebung in x- und y-Richtung
321 *
322 * @param dx Delta-X - Verschiebung in x-Richtung
323 * @param dy Delta-Y - Verschiebung in y-Richtung
324 */
325 public void move(float dx, float dy) {
326 super.move(dx, dy);
327
328 for (GraphicsObject object : this.objects) {
329 object.move(dx, dy);
330 }
331
332 }
333
334 /**
335 * Prüft, ob am angegebenen Punkt ein GraphicsObject liegt
336 *
337 * @param x Die x-Koordinate des zu prüfenden Punktes.
338 * @param y Die y-Koordinate des zu prüfenden Punktes.
339 * @return true, wenn sich der Punkt (x, y) innerhalb eines enthaltenen GraphicObjects befindet,
340 * und false andernfalls
341 */
342 public boolean hitTest(float x, float y) {
343 double minX = Float.MAX_VALUE;
344 double maxX = Float.MIN_VALUE;
345 double minY = Float.MAX_VALUE;
346 double maxY = Float.MIN_VALUE;
347
348 for (GraphicsObject object : this.objects) {
349 double objMinX = object.getLeftBorder();
350 double objMaxX = object.getRightBorder();
351 double objMinY = object.getTopBorder();
352 double objMaxY = object.getBottomBorder();
353
354 if (objMinX < minX) {
355 minX = objMinX;
356 }
357
358 if (objMaxX > maxX) {
359 maxX = objMaxX;
360 }
361
362 if (objMinY < minY) {
363 minY = objMinY;
364 }
365
366 if (objMaxY > maxY) {
367 maxY = objMaxY;
368 }
369 }
370 return x >= minX && x <= maxX && y >= minY && y <= maxY;
371 }
372}
void addToDrawBuffer(GraphicsObject object)
static final Color TRANSPARENT
Definition: Colors.java:23
void setPosition(Point point)
Definition: Compound.java:308
boolean contains(float x, float y)
Definition: Compound.java:226
boolean hitTest(float x, float y)
Definition: Compound.java:342
GraphicsObject getObjectAt(Point point)
Definition: Compound.java:175
GraphicsObject getObjectAt(float x, float y)
Definition: Compound.java:157
Compound(Point position, Color backgroundColor)
Definition: Compound.java:71
void setPosition(float x, float y)
Definition: Compound.java:296
Compound(float x, float y)
Definition: Compound.java:34
Iterator< GraphicsObject > iterator()
Definition: Compound.java:213
void move(float dx, float dy)
Definition: Compound.java:325
Compound(float x, float y, Color backgroundColor)
Definition: Compound.java:49
void addRelative(GraphicsObject object)
Definition: Compound.java:133
void add(GraphicsObject object)
Definition: Compound.java:123