KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 @COPYRIGHT@
3 */

4 package demo.sharededitor.models;
5
6 import java.awt.geom.Ellipse2D JavaDoc;
7 import java.awt.geom.RectangularShape JavaDoc;
8 import java.awt.geom.RoundRectangle2D JavaDoc;
9 import java.awt.Image JavaDoc;
10 import java.awt.Shape JavaDoc;
11
12 import demo.sharededitor.ui.ITexturable;
13
14 class Square
15     extends BaseObject
16     implements ITexturable
17 {
18     private RoundRectangle2D.Double JavaDoc shape;
19     
20     protected Shape JavaDoc getShape()
21     {
22         return shape;
23     }
24
25     protected Shape JavaDoc[] getAnchors()
26     {
27         return new Shape JavaDoc[] {
28             new Ellipse2D.Double JavaDoc(x1 - 5, y2 - 5, 10, 10),
29             new Ellipse2D.Double JavaDoc(x2 - 5, y2 - 5, 10, 10),
30             new Ellipse2D.Double JavaDoc(x2 - 5, y1 - 5, 10, 10),
31             new Ellipse2D.Double JavaDoc(x1 - 5, y1 - 5, 10, 10)
32         };
33     }
34
35     public synchronized void move(int dx, int dy)
36     {
37         x1 += dx;
38         y1 += dy;
39         x2 += dx;
40         y2 += dy;
41         shape.setFrameFromDiagonal(x1, y1, x2, y2);
42         this.notifyListeners(this);
43     }
44
45     public synchronized void resize(int x, int y)
46     {
47         switch (grabbedAnchor())
48         {
49             case 0:
50                 x1 = x;
51                 y2 = y;
52                 break;
53             case 1:
54                 x2 = x;
55                 y2 = y;
56                 break;
57             case 2:
58                 x2 = x;
59                 y1 = y;
60                 break;
61             case 3:
62                 x1 = x;
63                 y1 = y;
64                 break;
65         }
66         shape.setFrameFromDiagonal(x1, y1, x2, y2);
67         this.notifyListeners(this);
68     }
69     
70     public synchronized void setTexture(Image image)
71     {
72         super.setTexture(image);
73         notifyListeners(this);
74     }
75     
76     public boolean isTransient()
77     {
78        RectangularShape JavaDoc bounds = (RectangularShape JavaDoc)shape.getBounds();
79        return (bounds.getHeight() * bounds.getWidth()) < 4;
80     }
81
82     private int x1, y1, x2, y2;
83     
84     public Square()
85     {
86         x1 = y1 = x2 = y2 = 0;
87         shape = new RoundRectangle2D.Double JavaDoc();
88         shape.arcwidth = shape.archeight = 12;
89         shape.setFrameFromDiagonal(x1, y1, x2, y2);
90     }
91 }
92
Popular Tags