01: /*
02: @COPYRIGHT@
03: */
04: package demo.sharededitor.models;
05:
06: import java.awt.geom.Ellipse2D;
07: import java.awt.geom.Line2D;
08: import java.awt.Shape;
09:
10: final class Line extends BaseObject {
11: private Line2D.Double shape;
12:
13: protected Shape getShape() {
14: return shape;
15: }
16:
17: private transient Shape[] anchors = null;
18:
19: private Shape[] updateAnchors() {
20: if (anchors == null) {
21: anchors = new Shape[] {
22: new Ellipse2D.Double(shape.x2 - 5, shape.y2 - 5,
23: 10, 10),
24: new Ellipse2D.Double(shape.x1 - 5, shape.y1 - 5,
25: 10, 10) };
26: return anchors;
27: }
28:
29: ((Ellipse2D.Double) anchors[0]).x = shape.x2 - 5;
30: ((Ellipse2D.Double) anchors[0]).y = shape.y2 - 5;
31: ((Ellipse2D.Double) anchors[1]).x = shape.x1 - 5;
32: ((Ellipse2D.Double) anchors[1]).y = shape.y1 - 5;
33: return anchors;
34: }
35:
36: protected Shape[] getAnchors() {
37: return updateAnchors();
38: }
39:
40: public boolean isAt(int x, int y) {
41: return (shape.ptSegDist(x, y) <= 5) || super .isAt(x, y);
42: }
43:
44: public void move(int dx, int dy) {
45: synchronized (this ) {
46: shape.x1 += dx;
47: shape.y1 += dy;
48: shape.x2 += dx;
49: shape.y2 += dy;
50: updateAnchors();
51: }
52: this .notifyListeners(this );
53: }
54:
55: public void resize(int x, int y) {
56: synchronized (this ) {
57: switch (grabbedAnchor()) {
58: case 0:
59: shape.x2 = x;
60: shape.y2 = y;
61: break;
62: case 1:
63: shape.x1 = x;
64: shape.y1 = y;
65: break;
66: }
67: updateAnchors();
68: }
69: this .notifyListeners(this );
70: }
71:
72: public boolean isTransient() {
73: double dx = shape.x1 - shape.x2;
74: double dy = shape.y1 - shape.y2;
75: return Math.sqrt((dx * dx) + (dy * dy)) < 4;
76: }
77:
78: public Line() {
79: shape = new Line2D.Double();
80: shape.x1 = 0;
81: shape.y1 = 0;
82: shape.x2 = 0;
83: shape.y2 = 0;
84: }
85: }
|