KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > field > Rectangle


1 package jimm.datavision.field;
2 import jimm.datavision.Writeable;
3 import jimm.util.XMLWriter;
4 import java.util.Observable JavaDoc;
5
6 /**
7  * A rectangle with double coordinates.
8  *
9  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
10  */

11 public class Rectangle extends Observable JavaDoc implements Writeable {
12
13 /**
14  * Warning: only read the x coordinate. When writing, make sure to use
15  * setter method so observers are notified.
16  */

17 public double x;
18 /**
19  * Warning: only read the y coordinate. When writing, make sure to use
20  * setter method so observers are notified.
21  */

22 public double y;
23 /**
24  * Warning: only read the width. When writing, make sure to use
25  * setter method so observers are notified.
26  */

27 public double width;
28 /**
29  * Warning: only read the height. When writing, make sure to use
30  * setter method so observers are notified.
31  */

32 public double height;
33
34 /**
35  * Constructor.
36  */

37 public Rectangle() { this(0, 0, 0, 0); }
38
39 /**
40  * Constructor.
41  *
42  * @param r the <em>other</em> kind of rectangle
43  */

44 public Rectangle(java.awt.Rectangle JavaDoc r) { this(r.x, r.y, r.width, r.height); }
45
46 /**
47  * Constructor.
48  *
49  * @param r another rectangle
50  */

51 public Rectangle(Rectangle r) { this(r.x, r.y, r.width, r.height); }
52
53 /**
54  * Constructor.
55  *
56  * @param x a double
57  * @param y a double
58  * @param width a double
59  * @param height a double
60  */

61 public Rectangle(double x, double y, double width, double height) {
62     this.x = x;
63     this.y = y;
64     this.width = width;
65     this.height = height;
66 }
67
68 /**
69  * Returns the x coordinate.
70  *
71  * @return the doubleing-point x coordinate
72  */

73 public double getX() { return x; }
74
75 /**
76  * Sets the x coordinate.
77  *
78  * @param newX the new x coordinate
79  */

80 public void setX(double newX) {
81     if (x != newX) {
82     x = newX;
83     setChanged();
84     notifyObservers();
85     }
86 }
87
88 /**
89  * Returns the y coordinate.
90  *
91  * @return the doubleing-point y coordinate
92  */

93 public double getY() { return y; }
94
95 /**
96  * Sets the y coordinate.
97  *
98  * @param newY the new y coordinate
99  */

100 public void setY(double newY) {
101     if (y != newY) {
102     y = newY;
103     setChanged();
104     notifyObservers();
105     }
106 }
107
108 /**
109  * Returns the width.
110  *
111  * @return the doubleing-point width
112  */

113 public double getWidth() { return width; }
114
115 /**
116  * Sets the width.
117  *
118  * @param newWidth the new width
119  */

120 public void setWidth(double newWidth) {
121     if (width != newWidth) {
122     width = newWidth;
123     setChanged();
124     notifyObservers();
125     }
126 }
127
128 /**
129  * Returns the height.
130  *
131  * @return the doubleing-point height
132  */

133 public double getHeight() { return height; }
134
135 /**
136  * Sets the height.
137  *
138  * @param newHeight the new height
139  */

140 public void setHeight(double newHeight) {
141     if (height != newHeight) {
142     height = newHeight;
143     setChanged();
144     notifyObservers();
145     }
146 }
147
148 /**
149  * Sets everything at once.
150  *
151  * @param newX the new x coordinate
152  * @param newY the new y coordinate
153  * @param newWidth the new width
154  * @param newHeight the new height
155  */

156 public void setBounds(double newX, double newY, double newWidth,
157               double newHeight)
158 {
159     // Instead of calling setX(), sety(), etc. individually, copy the
160
// code so we can call notifyObservers() once.
161
boolean needsToNotify = false;
162     if (x != newX) {
163     x = newX;
164     needsToNotify = true;
165     }
166     if (y != newY) {
167     y = newY;
168     needsToNotify = true;
169     }
170     if (width != newWidth) {
171     width = newWidth;
172     needsToNotify = true;
173     }
174     if (height != newHeight) {
175     height = newHeight;
176     needsToNotify = true;
177     }
178
179     if (needsToNotify) {
180     setChanged();
181     notifyObservers();
182     }
183 }
184
185 /**
186  * Sets everything at once.
187  *
188  * @param r a jimm.datavision.Rectangle
189  */

190 public void setBounds(jimm.datavision.field.Rectangle r) {
191     setBounds(r.x, r.y, r.width, r.height);
192 }
193
194 /**
195  * Sets everything at once.
196  *
197  * @param r a java.awt.Rectangle
198  */

199 public void setBounds(java.awt.Rectangle JavaDoc r) {
200     setBounds(r.x, r.y, r.width, r.height);
201 }
202
203 /**
204  * Returns a string representation of this rectangle.
205  *
206  * @return a string representing this rectangle
207  */

208 public String JavaDoc toString() {
209     return "[x=" + x + ", y=" + y + ", w=" + width + ", h=" + height + "]";
210 }
211
212 /**
213  * Writes this rectangle as an XML tag.
214  *
215  * @param out a writer that knows how to write XML
216  */

217 public void writeXML(XMLWriter out) {
218     out.startElement("bounds");
219     out.attr("x", x);
220     out.attr("y", y);
221     out.attr("width", width);
222     out.attr("height", height);
223     out.endElement();
224 }
225
226 }
227
Popular Tags