KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > sharededitor > models > Line


1 /*
2 @COPYRIGHT@
3 */

4 package demo.sharededitor.models;
5
6 import java.awt.geom.Ellipse2D JavaDoc;
7 import java.awt.geom.Line2D JavaDoc;
8 import java.awt.geom.RectangularShape JavaDoc;
9 import java.awt.Shape JavaDoc;
10
11 final class Line extends BaseObject
12 {
13     private Line2D.Double JavaDoc shape;
14     
15     protected Shape JavaDoc getShape()
16     {
17         return shape;
18     }
19     
20     protected Shape JavaDoc[] getAnchors()
21     {
22         return new Shape JavaDoc[] {
23             new Ellipse2D.Double JavaDoc(shape.x2 - 5, shape.y2 - 5, 10, 10),
24             new Ellipse2D.Double JavaDoc(shape.x1 - 5, shape.y1 - 5, 10, 10)
25         };
26     }
27     
28     public boolean isAt(int x, int y)
29     {
30         return (shape.ptLineDist(x, y) <= 5) || super.isAt(x, y);
31     }
32     
33     public synchronized void move(int dx, int dy)
34     {
35         shape.x1 += dx;
36         shape.y1 += dy;
37         shape.x2 += dx;
38         shape.y2 += dy;
39         this.notifyListeners(this);
40     }
41     
42     public synchronized void resize(int x, int y)
43     {
44         switch (grabbedAnchor())
45         {
46             case 0:
47                 shape.x2 = x;
48                 shape.y2 = y;
49                 break;
50             case 1:
51                 shape.x1 = x;
52                 shape.y1 = y;
53                 break;
54         }
55         this.notifyListeners(this);
56     }
57
58     public boolean isTransient()
59     {
60        double dx = shape.x1 - shape.x2;
61        double dy = shape.y1 - shape.y2;
62        return Math.sqrt((dx * dx) + (dy * dy)) < 4;
63     }
64     
65     public Line()
66     {
67         shape = new Line2D.Double JavaDoc();
68         shape.x1 = 0;
69         shape.y1 = 0;
70         shape.x2 = 0;
71         shape.y2 = 0;
72     }
73 }
74
Popular Tags