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