EOS 2  1.1.0
Einfache Objektbasierte Sprache
Figure.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.geo;
2 
3 import de.lathanda.eos.base.layout.Transform;
4 import de.lathanda.eos.base.math.Point;
5 
6 import de.lathanda.eos.base.Picture;
7 import de.lathanda.eos.base.layout.BalancePoint;
8 import de.lathanda.eos.base.layout.BoundingBox;
9 
15 public abstract class Figure implements Cloneable {
16  private boolean visible;
17  protected FigureGroup group;
18  protected Transform transform;
19  private BoundingBox bound = new BoundingBox();
20 
21  public Figure() {
23  group = null;
24  visible = true;
25  }
26 
27  // ***********
28  // * drawing *
29  // ***********
30  public void draw(Picture g) {
31  if (!visible)
32  return;
33  synchronized (this) {
34  g.pushTransform();
35  beforeDrawing(g);
37  drawObject(g);
38  g.restoreTransform();
39  }
40  }
41 
42  protected void beforeDrawing(Picture p) {
43  };
44 
45  protected abstract void drawObject(Picture p);
46 
47  // *******************
48  // * basic abilities *
49  // *******************
50  public void setVisible(boolean visible) {
51  this.visible = visible;
53  }
54 
55  public boolean getVisible() {
56  return visible;
57  }
58 
59  public Figure copy() {
60  try {
61  return (Figure) this.clone();
62  } catch (CloneNotSupportedException ex) {
63  System.exit(1);
64  return null;
65  }
66  }
67 
68  /* package */ void replaceGroup(FigureGroup group) {
69  if (this.group != null) {
70  this.group.removeFigure(this);
71  }
72  this.group = group;
73  }
74 
78  public void fireDataChanged() {
79  if (group != null) {
81  }
82  }
83 
89  protected void fireLayoutChanged() {
90  bound = null;
91  if (group != null) {
92  if (group.getGroup() != null) {
94  }
95  }
97  }
98 
102  public void free() {
103  visible = false;
104  if (group != null) {
105  group.removeFigure(this);
106  }
107  group = null;
108  }
109 
110  // **************************
111  // * transformation methods *
112  // **************************
113  protected void moveToInternal(double x, double y) {
115  // no update!
116  }
117 
118  protected void moveInternal(double x, double y) {
119  transform = transform.translate(x, y);
120  // no update!
121  }
122 
123  protected void resetTransformation() {
125  // no update!
126  }
127 
128  public void move(double dx, double dy) {
129  transform = transform.translate(dx, dy);
131  }
132 
133  public void moveTo(double x, double y) {
136  }
137 
138  public double getX() {
139  return transform.getdx();
140  }
141 
142  public void setX(double x) {
145  }
146 
147  public double getY() {
148  return transform.getdy();
149  }
150 
151  public void setY(double y) {
154  }
155 
156  public void rotate(double angle) {
157  transform = transform.rotate(angle / 180 * Math.PI);
158  fireDataChanged();
159  }
160 
161  public void setRotation(double angle) {
162  transform = transform.setAngle(angle / 180 * Math.PI);
163  fireDataChanged();
164  }
165 
166  public double getRotation() {
167  return transform.getAngle() / Math.PI * 180;
168  }
169 
170  protected double getRotationInternal() {
171  return transform.getAngle();
172  }
173 
174  public void rotateAround(double x, double y, double angle) {
175  transform = transform.rotate(x, y, angle / 180 * Math.PI);
177  }
178 
179  public void scaleAt(double x, double y, double factor) {
180  transform = transform.scalePositionAt(x, y, factor);
181  scaleInternal(factor);
183  }
184 
185  public void scale(double factor) {
186  scaleInternal(factor);
188  }
189 
196  protected abstract void scaleInternal(double factor);
197 
198  protected void transformMirrorX() {
200  }
201 
202  protected void transformMirrorY() {
204  }
205 
207  return getAbsolutePosition(new Point(0, 0));// transform.getdx(),transform.getdy()));
208  }
209 
210  private Point getAbsolutePosition(Point p) {
211  if (group != null) {
212  Group g = group.getGroup();
213  if (g != null) {
215  }
216  }
217  return transform.transform(p);
218  }
219 
220  protected Point getTransformedPosition(Point point) {
221  return transform.transform(point);
222  }
223 
224  protected Point getRelativePosition(Point absolute) {
225  if (group != null) {
226  Group g = group.getGroup();
227  if (g != null) {
228  Point p = g.getRelativePosition(absolute);
229  return (((Figure) g).transform).transformBack(p);
230  } else {
231  return absolute;
232  }
233  } else {
234  return absolute;
235  }
236  }
237 
238  protected void changeTransformation(Figure relativeTo) {
239  if (relativeTo.group != null) {
240  Figure parent = relativeTo.group.getGroup();
241  if (parent != null) {
242  changeTransformation(parent);
243  }
244  }
246  }
247 
248  protected void restoreTransformation(Group relativeTo) {
249  Group parent = relativeTo;
250  while (parent != null) {
252  parent = parent.getParentGroup().getGroup();
253  }
254  }
255 
256  // ******************
257  // * Layout Methods *
258  // ******************
259  protected abstract BalancePoint getBalancePoint();
260 
261  // *********************
262  // * BoundingBox logic *
263  // *********************
265  return calculateBoundingBox(base, transform);
266  };
267 
268  protected abstract BoundingBox calculateBoundingBox(Transform base, Transform own);
269 
271  if (bound == null) {
273  }
274  return bound;
275  }
276 
277  public void alignLeftTop(double left, double top) {
278  BoundingBox b = getBound();
279  move(left - b.getLeft(), top - b.getTop());
281  }
282 
283  public void alignRightBottom(double right, double bottom) {
284  BoundingBox b = getBound();
285  move(right - b.getRight(), bottom - b.getBottom());
287  }
288 
289  public void alignLeft(double left) {
290  BoundingBox b = getBound();
291  move(left - b.getLeft(), 0);
293  }
294 
295  public void alignRight(double right) {
296  BoundingBox b = getBound();
297  move(right - b.getRight(), 0);
299  }
300 
301  public void alignTop(double top) {
302  BoundingBox b = getBound();
303  move(0, top - b.getTop());
305  }
306 
307  public void alignBottom(double bottom) {
308  BoundingBox b = getBound();
309  move(0, bottom - b.getBottom());
311  }
312  public String getAngleText() {
313  double angle = transform.getAngle() / Math.PI * 180 % 360d;
314  if (angle < 0) {
315  angle += 360d;
316  }
317  return Integer.toString((int)Math.round(angle));
318  }
319  public boolean getMirrored() {
320  return transform.getMirrorX();
321  }
322 
323 }
abstract void applyTransform(Transform tf)
abstract void pushTransform()
abstract void restoreTransform()
Geometrische Transformation.
Definition: Transform.java:13
Transform rotate(double angle)
Definition: Transform.java:34
Transform transformBack(Transform child)
Definition: Transform.java:117
Transform translate(double dx, double dy)
Definition: Transform.java:67
Transform scalePositionAt(double x, double y, double factor)
Definition: Transform.java:63
Transform setAngle(double angle)
Definition: Transform.java:162
Transform setTranslation(double dx, double dy)
Definition: Transform.java:134
Transform transform(Transform child)
Definition: Transform.java:83
abstract void drawObject(Picture p)
void restoreTransformation(Group relativeTo)
Definition: Figure.java:248
void beforeDrawing(Picture p)
Definition: Figure.java:42
void setRotation(double angle)
Definition: Figure.java:161
void scaleAt(double x, double y, double factor)
Definition: Figure.java:179
void alignTop(double top)
Definition: Figure.java:301
void draw(Picture g)
Definition: Figure.java:30
void alignBottom(double bottom)
Definition: Figure.java:307
void moveInternal(double x, double y)
Definition: Figure.java:118
void setX(double x)
Definition: Figure.java:142
abstract BalancePoint getBalancePoint()
void moveToInternal(double x, double y)
Definition: Figure.java:113
void moveTo(double x, double y)
Definition: Figure.java:133
void alignLeft(double left)
Definition: Figure.java:289
void alignRight(double right)
Definition: Figure.java:295
abstract void scaleInternal(double factor)
void move(double dx, double dy)
Definition: Figure.java:128
void alignRightBottom(double right, double bottom)
Definition: Figure.java:283
BoundingBox calculateBoundingBox(Transform base)
Definition: Figure.java:264
void rotateAround(double x, double y, double angle)
Definition: Figure.java:174
void setVisible(boolean visible)
Definition: Figure.java:50
void alignLeftTop(double left, double top)
Definition: Figure.java:277
void setY(double y)
Definition: Figure.java:151
abstract BoundingBox calculateBoundingBox(Transform base, Transform own)
void rotate(double angle)
Definition: Figure.java:156
Point getTransformedPosition(Point point)
Definition: Figure.java:220
BoundingBox getBound()
Definition: Figure.java:270
void changeTransformation(Figure relativeTo)
Definition: Figure.java:238
Point getRelativePosition(Point absolute)
Definition: Figure.java:224
void scale(double factor)
Definition: Figure.java:185
FigureGroup getParentGroup()
Definition: Group.java:87
void removeFigure(Figure figure)
Impressum