KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > xjlib > appkit > gview > object > GElementRect


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32 package org.antlr.xjlib.appkit.gview.object;
33
34 import org.antlr.xjlib.appkit.gview.base.Anchor2D;
35 import org.antlr.xjlib.appkit.gview.base.Rect;
36 import org.antlr.xjlib.appkit.gview.base.Vector2D;
37 import org.antlr.xjlib.appkit.gview.shape.SLabel;
38 import org.antlr.xjlib.foundation.XJXMLSerializable;
39
40 import java.awt.*;
41
42 public class GElementRect extends GElement implements XJXMLSerializable {
43
44     public static final int DEFAULT_WIDTH = 40;
45     public static final int DEFAULT_HEIGHT = 40;
46
47     protected double width = DEFAULT_WIDTH;
48     protected double height = DEFAULT_HEIGHT;
49
50     public GElementRect() {
51     }
52
53     public void setPositionOfUpperLeftCorner(double x, double y) {
54         setPosition(x+width*0.5, y+height*0.5);
55     }
56
57     public void setSize(double width, double height) {
58         this.width = width;
59         this.height = height;
60         elementDimensionDidChange();
61     }
62
63     public void setWidth(double width) {
64         this.width = width;
65         elementDimensionDidChange();
66     }
67
68     public double getWidth() {
69         return width;
70     }
71
72     public void setHeight(double height) {
73         this.height = height;
74         elementDimensionDidChange();
75     }
76
77     public double getHeight() {
78         return height;
79     }
80
81     @Override JavaDoc
82     public void updateAnchors() {
83         setAnchor(ANCHOR_CENTER, position.copy(), Anchor2D.DIRECTION_FREE);
84         setAnchor(ANCHOR_TOP, position.add(new Vector2D(0, -height*0.5)), Anchor2D.DIRECTION_TOP);
85         setAnchor(ANCHOR_BOTTOM, position.add(new Vector2D(0, height*0.5)), Anchor2D.DIRECTION_BOTTOM);
86         setAnchor(ANCHOR_LEFT, position.add(new Vector2D(-width*0.5, 0)), Anchor2D.DIRECTION_LEFT);
87         setAnchor(ANCHOR_RIGHT, position.add(new Vector2D(width*0.5, 0)), Anchor2D.DIRECTION_RIGHT);
88     }
89
90     @Override JavaDoc
91     public Rect getFrame() {
92         double x = getPositionX()-getWidth()*0.5;
93         double y = getPositionY()-getHeight()*0.5;
94         double dx = getWidth();
95         double dy = getHeight();
96         return new Rect(x, y, dx, dy);
97     }
98
99     @Override JavaDoc
100     public boolean isInside(Point p) {
101         return getFrame().contains(p);
102     }
103
104     @Override JavaDoc
105     public void draw(Graphics2D g) {
106         if(isVisibleInClip(g)) {
107             if(labelVisible) {
108                 g.setColor(labelColor);
109                 if(label != null && label.length()>0)
110                     drawLabel(g);
111             }
112             
113             g.setColor(color);
114             g.setStroke(strokeSize);
115             drawShape(g);
116             g.setStroke(strokeNormal);
117         }
118     }
119
120     @Override JavaDoc
121     public void drawShape(Graphics2D g) {
122         super.drawShape(g);
123
124         Rectangle r = getFrame().rectangle();
125         g.drawRect(r.x, r.y, r.width, r.height);
126     }
127
128     public void drawLabel(Graphics2D g) {
129         SLabel.drawCenteredString(label, (int)getPositionX(), (int)getPositionY(), g);
130     }
131     
132 }
133
Popular Tags