GraphicsApp API
Eine überarbeitete und angepasste Variante der originalen GraphicsApp-Umgebung
GraphicsObject.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.awt.image.BufferedImage;
8
9/**
10 * Diese Klasse ist die gemeinsame Oberklasse aller grafischen Objekte, die in
11 * einer GraphicsApp angezeigt werden können. Da es sich um eine abstrakte Klasse handelt, ist es nicht erlaubt,
12 * direkt ein Objekt vom Typ GraphicsObject zu konstruieren. Stattdessen wird eine der konkreten Unterklassen
13 * wie Rectangle oder Line erzeugt. Der Zweck dieser Klassendefinition ist es, Methoden zu definieren, die für alle
14 * grafische Objekte unabhängig von ihrer spezifischen Klasse gelten.
15 */
16public abstract class GraphicsObject {
17
18 ///////////////////////////////////////////////////////////////////////////
19 // Members and constants
20 ///////////////////////////////////////////////////////////////////////////
21
22 protected static final Color DEFAULT_COLOR = Colors.RED;
23 protected static final Color DEFAULT_BORDER_COLOR = Colors.BLACK;
24
25 private float xPos;
26 private float yPos;
27
28 private float width;
29 private float height;
30
31 private Color color;
32 private Color strokeColor;
33 protected float strokeWeight;
34
36
37 ///////////////////////////////////////////////////////////////////////////
38 // Constructors
39 ///////////////////////////////////////////////////////////////////////////
40
41 public GraphicsObject(float x, float y, Color color) {
42 this.xPos = x;
43 this.yPos = y;
44 this.width = 0;
45 this.height = 0;
46 this.color = color;
47 this.strokeWeight = 0;
48 this.strokeColor = DEFAULT_BORDER_COLOR;
49 this.type = GraphicsObjectType.NONE;
50 }
51
52 public GraphicsObject(float x, float y) {
54 }
55
56 public GraphicsObject(float x, float y, float width, float height, Color color) {
57 this(x, y, color);
58 this.width = width;
59 this.height = height;
60 }
61
62 public GraphicsObject(float x, float y, float width, float height, Color color, Color borderColor) {
63 this(x, y, width, height, color);
64 this.strokeColor = borderColor;
65 }
66
67 public GraphicsObject(Point point) {
68 this(point.getXPos(), point.getYPos());
69 }
70
71 ///////////////////////////////////////////////////////////////////////////
72 // Drawing
73 ///////////////////////////////////////////////////////////////////////////
74
75 /**
76 * Zeichnet das Grafikobjekt auf den Canvas.
77 */
78 public void draw() {
80 }
81
82 ///////////////////////////////////////////////////////////////////////////
83 // Getters and Setters
84 ///////////////////////////////////////////////////////////////////////////
85
86 /**
87 * Liefert die x-Koordinate des Objekts.
88 *
89 * @return die x-Koordinate des Objekts.
90 */
91 public float getXPos() {
92 return xPos;
93 }
94
95 /**
96 * Setzt die x-Koordinate des Objekts.
97 *
98 * @param xPos Die neue x-Koordinate
99 */
100 public void setXPos(float xPos) {
101 this.xPos = xPos;
102 }
103
104 /**
105 * Liefert die y-Koordinate des Objekts.
106 *
107 * @return die y-Koordinate des Objekts.
108 */
109 public float getYPos() {
110 return yPos;
111 }
112
113 /**
114 * Setzt die y-Koordinate des Objekts.
115 *
116 * @param yPos Die neue y-Koordinate
117 */
118 public void setYPos(float yPos) {
119 this.yPos = yPos;
120 }
121
122 /**
123 * Setzt die Position des Objekts,
124 * gegeben durch xPos und yPos
125 *
126 * @param xPos Die neue x-Koordinate
127 * @param yPos Die neue y-Koordinate
128 */
129 public void setPosition(float xPos, float yPos) {
130 setXPos(xPos);
131 setYPos(yPos);
132 }
133
134 /**
135 * Liefert die Breite des Objekts.
136 *
137 * @return die Breite des Objekts.
138 */
139 public float getWidth() {
140 return this.width;
141 }
142
143 /**
144 * Liefert die Höhe des Objekts.
145 *
146 * @return die Höhe des Objekts.
147 */
148 public float getHeight() {
149 return this.height;
150 }
151
152 /**
153 * Setzt die Breite des Objekts.
154 *
155 * @param width die neue Breite
156 */
157 public void setWidth(float width) {
158 this.width = width;
159 }
160
161 /**
162 * Setzt die Höhe des Objekts.
163 *
164 * @param height die neue Breite
165 */
166 public void setHeight(float height) {
167 this.height = height;
168 }
169
170 /**
171 * Liefert den oberen Rand des Objekts.
172 *
173 * @return der obere Rand des Objekts.
174 */
175 public float getTopBorder() {
176 return this.getYPos();
177 }
178
179 /**
180 * Liefert den unteren Rand des Objekts.
181 *
182 * @return der untere Rand des Objekts.
183 */
184 public float getBottomBorder() {
185 return this.getYPos() + this.getHeight();
186 }
187
188 /**
189 * Liefert den linken Rand des Objekts.
190 *
191 * @return der linke Rand des Objekts.
192 */
193 public float getLeftBorder() {
194 return this.getXPos();
195 }
196
197 /**
198 * Liefert den rechten Rand des Objekts.
199 *
200 * @return der rechte Rand des Objekts.
201 */
202 public float getRightBorder() {
203 return this.getXPos() + this.getWidth();
204 }
205
206 /**
207 * Liefert die Hintergrundfarbe des Objekts
208 *
209 * @return die Hintergrundfarbe
210 */
211 public Color getColor() {
212 return color;
213 }
214
215 /**
216 * Setzt die Hintergrundfarbe des Objekts
217 *
218 * @param color die neue Hintergrundfarbe
219 */
220 public void setColor(Color color) {
221 this.color = color;
222 }
223
224 /**
225 * Setzt die Randfarbe des Objekts
226 *
227 * @param color die neue Randfarbe
228 */
229 public void setBorderColor(Color color) {
230 this.strokeColor = color;
231 }
232
233 /**
234 * Setzt die Randstärke des Objekts
235 *
236 * @param weight die neue Randstärke
237 */
238 public void setBorderWeight(float weight) {
239 this.strokeWeight = weight;
240 }
241
242 /**
243 * Setzt die Randeigenschaften des Objekts
244 *
245 * @param color die neue Randfarbe
246 * @param weight die neue Randstärke
247 */
248 public void setBorder(Color color, float weight) {
249 this.setBorderColor(color);
250 this.setBorderWeight(weight);
251 }
252
253 /**
254 * Liefert die Randstärke des Objekts
255 *
256 * @return die Randstärke des Objekts
257 */
258 public float getBorderWeight() {
259 return this.strokeWeight;
260 }
261
262 /**
263 * Liefert die Randfarbe des Objekts
264 *
265 * @return die Randfarbe des Objekts
266 */
268 return this.strokeColor;
269 }
270
271 /**
272 * Liefert den Grafik-Typ des Objekts
273 *
274 * @return der Grafik-Typ des Objekts
275 */
277 return type;
278 }
279
280 ///////////////////////////////////////////////////////////////////////////
281 // Utility Methods
282 ///////////////////////////////////////////////////////////////////////////
283
284 /**
285 * Bewegt das Objekt auf dem Bildschirm mit den Verschiebungen dx und dy.
286 *
287 * @param dx Die horizontale Änderung der Position
288 * @param dy Die vertikale Änderung der Position
289 */
290 public void move(float dx, float dy) {
291 this.xPos += dx;
292 this.yPos += dy;
293 }
294
295 /**
296 * Prüft, ob dieses Objekt die angegebenen Koordinaten enthält.
297 *
298 * @param x Die zu testende x-Position
299 * @param y y
300 * Die zu testende y-Position
301 * @return true, wenn sich der Punkt (x, y) innerhalb des Objekts befindet,
302 * und false andernfalls
303 */
304 public boolean hitTest(float x, float y) {
305 return x >= this.getLeftBorder() && x <= this.getRightBorder() && y >= this.getTopBorder() && y <= this.getBottomBorder();
306 }
307
308 /**
309 * Liefert den Abstand zu einem anderen Grafikobjekt.
310 *
311 * @param object das andere Grafikobjekt
312 * @return Abstand zwischen diesem Objekt und dem angegebenen Objekt
313 */
314 public double distanceTo(GraphicsObject object) {
315 double dx = object.getXPos() - this.getXPos();
316 double dy = object.getYPos() - this.getYPos();
317 return Math.sqrt(dx * dx + dy * dy);
318 }
319}
void addToDrawBuffer(GraphicsObject object)
static final Color RED
Definition: Colors.java:12
static final Color BLACK
Definition: Colors.java:21
GraphicsObject(float x, float y, Color color)
void setPosition(float xPos, float yPos)
GraphicsObject(float x, float y, float width, float height, Color color)
GraphicsObject(float x, float y, float width, float height, Color color, Color borderColor)
boolean hitTest(float x, float y)
void setBorder(Color color, float weight)
double distanceTo(GraphicsObject object)