1 |
/* Rectangle.java -- Class representing a rectangle. |
/* Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation |
|
Copyright (C) 1999 Free Software Foundation, Inc. |
|
2 |
|
|
3 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
4 |
|
|
26 |
|
|
27 |
package java.awt; |
package java.awt; |
28 |
|
|
29 |
/** |
import java.awt.geom.*; |
30 |
* This class represents a rectangle and all the interesting things you |
import java.io.Serializable; |
|
* might want to do with it. Note that the coordinate system uses |
|
|
* the origin (0,0) as the top left of the screen, with the x and y |
|
|
* values increasing as they move to the right and down respectively. |
|
|
* |
|
|
* @author Aaron M. Renn (arenn@urbanophile.com) |
|
|
*/ |
|
|
public class Rectangle implements Shape, java.io.Serializable |
|
|
{ |
|
31 |
|
|
32 |
/* |
/* Status: Mostly complete. Some of the Java2D stuff is commented out. */ |
|
* Instance Variables |
|
|
*/ |
|
33 |
|
|
34 |
/** |
/** |
35 |
|
* This class represents a rectangle and all the interesting things you |
36 |
|
* might want to do with it. Note that the coordinate system uses |
37 |
|
* the origin (0,0) as the top left of the screen, with the x and y |
38 |
|
* values increasing as they move to the right and down respectively. |
39 |
|
* |
40 |
|
* @author Warren Levy <warrenl@cygnus.com> |
41 |
|
* @author Aaron M. Renn (arenn@urbanophile.com) |
42 |
|
*/ |
43 |
|
public class Rectangle extends Rectangle2D |
44 |
|
implements Cloneable, Shape, Serializable |
45 |
|
{ |
46 |
|
/** |
47 |
* The X coordinate of the top-left corner of the rectangle. |
* The X coordinate of the top-left corner of the rectangle. |
48 |
*/ |
*/ |
49 |
public int x; |
public int x; |
50 |
|
|
51 |
/** |
/** |
52 |
* The Y coordinate of the top-left corner of the rectangle; |
* The Y coordinate of the top-left corner of the rectangle; |
53 |
*/ |
*/ |
54 |
public int y; |
public int y; |
55 |
|
|
56 |
/** |
/** |
57 |
* The width of the rectangle |
* The width of the rectangle |
58 |
*/ |
*/ |
59 |
public int width; |
public int width; |
60 |
|
|
61 |
/** |
/** |
62 |
* The height of the rectangle |
* The height of the rectangle |
63 |
*/ |
*/ |
64 |
public int height; |
public int height; |
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/* |
|
|
* Constructors |
|
|
*/ |
|
|
|
|
|
/** |
|
|
* Initializes a new instance of <code>Rectangle</code> with a top |
|
|
* left corner at (0,0) and a width and heigth of 0. |
|
|
*/ |
|
|
public |
|
|
Rectangle() |
|
|
{ |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Initializes a new instance of <code>Rectangle</code> from the |
|
|
* coordinates of the specified rectangle. |
|
|
* |
|
|
* @param rect The rectangle to copy from. |
|
|
*/ |
|
|
public |
|
|
Rectangle(Rectangle rect) |
|
|
{ |
|
|
this.x = rect.x; |
|
|
this.y = rect.y; |
|
|
this.width = rect.width; |
|
|
this.height = rect.height; |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Initializes a new instance of <code>Rectangle</code> from the specified |
|
|
* inputs. |
|
|
* |
|
|
* @param x The X coordinate of the top left corner of the rectangle. |
|
|
* @param y The Y coordinate of the top left corner of the rectangle. |
|
|
* @param width The width of the rectangle. |
|
|
* @param height The height of the rectangle. |
|
|
*/ |
|
|
public |
|
|
Rectangle(int x, int y, int width, int height) |
|
|
{ |
|
|
this.x = x; |
|
|
this.y = y; |
|
|
this.width = width; |
|
|
this.height = height; |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Initializes a new instance of <code>Rectangle</code> with the specified |
|
|
* width and height. The upper left corner of the rectangle will be at |
|
|
* the origin (0,0). |
|
|
* |
|
|
* @param width The width of the rectangle. |
|
|
* @param height the height of the rectange. |
|
|
*/ |
|
|
public |
|
|
Rectangle(int width, int height) |
|
|
{ |
|
|
this(0, 0, width, height); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Initializes a new instance of <code>Rectangle</code> with a top-left |
|
|
* corner represented by the specified point and the width and height |
|
|
* represented by the specified dimension. |
|
|
* |
|
|
* @param point The upper left corner of the rectangle. |
|
|
* @param dim The width and height of the rectangle. |
|
|
*/ |
|
|
public |
|
|
Rectangle(Point point, Dimension dim) |
|
|
{ |
|
|
this(point.x, point.y, dim.width, dim.height); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Initializes a new instance of <code>Rectangle</code> with a top left |
|
|
* corner at the specified point and a width and height of zero. |
|
|
* |
|
|
* @param poin The upper left corner of the rectangle. |
|
|
*/ |
|
|
public |
|
|
Rectangle(Point point) |
|
|
{ |
|
|
this(point.x, point.y, 0, 0); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Initializes a new instance of <code>Rectangle</code> with an |
|
|
* upper left corner at the origin (0,0) and a width and height represented |
|
|
* by the specified dimension. |
|
|
* |
|
|
* @param dim The width and height of the rectangle. |
|
|
*/ |
|
|
public |
|
|
Rectangle(Dimension dim) |
|
|
{ |
|
|
this(0, 0, dim.width, dim.height); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/* |
|
|
* Instance Methods |
|
|
*/ |
|
|
|
|
|
/** |
|
|
* Returns the bounding rectangle for this rectangle, which is simply |
|
|
* this rectange itself. |
|
|
* |
|
|
* @return This rectangle. |
|
|
*/ |
|
|
public Rectangle |
|
|
getBounds() |
|
|
{ |
|
|
return(this); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Updates this rectangle to match the dimensions of the specified |
|
|
* rectangle. |
|
|
* |
|
|
* @param rect The rectangle to update from. |
|
|
*/ |
|
|
public void |
|
|
setBounds(Rectangle rect) |
|
|
{ |
|
|
synchronized(this) |
|
|
{ |
|
|
this.x = rect.x; |
|
|
this.y = rect.y; |
|
|
this.width = rect.width; |
|
|
this.height = rect.height; |
|
|
} |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Updates this rectangle to have the specified dimensions. |
|
|
* |
|
|
* @param x The new X coordinate of the upper left hand corner. |
|
|
* @param y The new Y coordinate of the upper left hand corner. |
|
|
* @param width The new width of this rectangle. |
|
|
* @param height The new height of this rectangle. |
|
|
*/ |
|
|
public void |
|
|
setBounds(int x, int y, int width, int height) |
|
|
{ |
|
|
synchronized(this) |
|
|
{ |
|
|
this.x = x; |
|
|
this.y = y; |
|
|
this.width = width; |
|
|
this.height = height; |
|
|
} |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Updates this rectangle to have the specified dimensions. |
|
|
* |
|
|
* @param x The new X coordinate of the upper left hand corner. |
|
|
* @param y The new Y coordinate of the upper left hand corner. |
|
|
* @param width The new width of this rectangle. |
|
|
* @param height The new height of this rectangle. |
|
|
* |
|
|
* @deprecated This method is deprecated in favor of |
|
|
* <code>setBounds(int, int, int, int)</code>. |
|
|
*/ |
|
|
public void |
|
|
reshape(int x, int y, int width, int height) |
|
|
{ |
|
|
setBounds(x, y, width, height); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Returns the location of this rectangle, which is the coordinates of |
|
|
* its upper left corner. |
|
|
* // FIXME: Is this true? |
|
|
* |
|
|
* @return The point where this rectangle is located. |
|
|
*/ |
|
|
public Point |
|
|
getLocation() |
|
|
{ |
|
|
return(new Point(x, y)); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Moves the location of this rectangle by setting its upper left |
|
|
* corner to the specified point. |
|
|
* // FIXME: Is this true? |
|
|
* |
|
|
* @param point The point to move the rectange to. |
|
|
*/ |
|
|
public void |
|
|
setLocation(Point point) |
|
|
{ |
|
|
synchronized(this) |
|
|
{ |
|
|
this.x = point.x; |
|
|
this.y = point.y; |
|
|
} |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Moves the location of this rectangle by setting its upper left |
|
|
* corner to the specified coordinates. |
|
|
* // FIXME: Is this true? |
|
|
* |
|
|
* @param x The new X coordinate for this rectangle. |
|
|
* @param y The new Y coordinate for this rectangle. |
|
|
*/ |
|
|
public void |
|
|
setLocation(int x, int y) |
|
|
{ |
|
|
synchronized(this) |
|
|
{ |
|
|
this.x = x; |
|
|
this.y = y; |
|
|
} |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Moves the location of this rectangle by setting its upper left |
|
|
* corner to the specified coordinates. |
|
|
* // FIXME: Is this true? |
|
|
* |
|
|
* @param x The new X coordinate for this rectangle. |
|
|
* @param y The new Y coordinate for this rectangle. |
|
|
* |
|
|
* @deprecated This method is deprecated in favor of |
|
|
* <code>setLocation(int, int)</code>. |
|
|
*/ |
|
|
public void |
|
|
move(int x, int y) |
|
|
{ |
|
|
setLocation(x, y); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
65 |
|
|
66 |
/** |
/** |
67 |
* Returns the size of this rectangle. |
* Initializes a new instance of <code>Rectangle</code> with a top |
68 |
* |
* left corner at (0,0) and a width and height of 0. |
69 |
* @return The size of this rectangle. |
*/ |
70 |
*/ |
public Rectangle() |
71 |
public Dimension |
{ |
72 |
getSize() |
x = 0; |
73 |
{ |
y = 0; |
74 |
return(new Dimension(width, height)); |
width = 0; |
75 |
|
height = 0; |
76 |
|
} |
77 |
|
|
78 |
|
/** |
79 |
|
* Initializes a new instance of <code>Rectangle</code> from the |
80 |
|
* coordinates of the specified rectangle. |
81 |
|
* |
82 |
|
* @param rect The rectangle to copy from. |
83 |
|
*/ |
84 |
|
public Rectangle(Rectangle r) |
85 |
|
{ |
86 |
|
x = r.x; |
87 |
|
y = r.y; |
88 |
|
width = r.width; |
89 |
|
height = r.height; |
90 |
|
} |
91 |
|
|
92 |
|
/** |
93 |
|
* Initializes a new instance of <code>Rectangle</code> from the specified |
94 |
|
* inputs. |
95 |
|
* |
96 |
|
* @param x The X coordinate of the top left corner of the rectangle. |
97 |
|
* @param y The Y coordinate of the top left corner of the rectangle. |
98 |
|
* @param width The width of the rectangle. |
99 |
|
* @param height The height of the rectangle. |
100 |
|
*/ |
101 |
|
public Rectangle(int x, int y, int width, int height) |
102 |
|
{ |
103 |
|
this.x = x; |
104 |
|
this.y = y; |
105 |
|
this.width = width; |
106 |
|
this.height = height; |
107 |
|
} |
108 |
|
|
109 |
|
/** |
110 |
|
* Initializes a new instance of <code>Rectangle</code> with the specified |
111 |
|
* width and height. The upper left corner of the rectangle will be at |
112 |
|
* the origin (0,0). |
113 |
|
* |
114 |
|
* @param width The width of the rectangle. |
115 |
|
* @param height the height of the rectange. |
116 |
|
*/ |
117 |
|
public Rectangle(int width, int height) |
118 |
|
{ |
119 |
|
x = 0; |
120 |
|
y = 0; |
121 |
|
this.width = width; |
122 |
|
this.height = height; |
123 |
|
} |
124 |
|
|
125 |
|
/** |
126 |
|
* Initializes a new instance of <code>Rectangle</code> with a top-left |
127 |
|
* corner represented by the specified point and the width and height |
128 |
|
* represented by the specified dimension. |
129 |
|
* |
130 |
|
* @param point The upper left corner of the rectangle. |
131 |
|
* @param dim The width and height of the rectangle. |
132 |
|
*/ |
133 |
|
public Rectangle(Point p, Dimension d) |
134 |
|
{ |
135 |
|
x = p.x; |
136 |
|
y = p.y; |
137 |
|
width = d.width; |
138 |
|
height = d.height; |
139 |
|
} |
140 |
|
|
141 |
|
/** |
142 |
|
* Initializes a new instance of <code>Rectangle</code> with a top left |
143 |
|
* corner at the specified point and a width and height of zero. |
144 |
|
* |
145 |
|
* @param poin The upper left corner of the rectangle. |
146 |
|
*/ |
147 |
|
public Rectangle(Point p) |
148 |
|
{ |
149 |
|
x = p.x; |
150 |
|
y = p.y; |
151 |
|
width = 0; |
152 |
|
height = 0; |
153 |
|
} |
154 |
|
|
155 |
|
/** |
156 |
|
* Initializes a new instance of <code>Rectangle</code> with an |
157 |
|
* upper left corner at the origin (0,0) and a width and height represented |
158 |
|
* by the specified dimension. |
159 |
|
* |
160 |
|
* @param dim The width and height of the rectangle. |
161 |
|
*/ |
162 |
|
public Rectangle(Dimension d) |
163 |
|
{ |
164 |
|
x = 0; |
165 |
|
y = 0; |
166 |
|
width = d.width; |
167 |
|
height = d.height; |
168 |
|
} |
169 |
|
|
170 |
|
/** |
171 |
|
* Returns the bounding rectangle for this rectangle, which is simply |
172 |
|
* this rectange itself. |
173 |
|
* |
174 |
|
* @return This rectangle. |
175 |
|
*/ |
176 |
|
public Rectangle getBounds () |
177 |
|
{ |
178 |
|
return (Rectangle) this.clone(); |
179 |
|
} |
180 |
|
|
181 |
|
/** |
182 |
|
* Modifies this rectangle so that it represents the smallest rectangle |
183 |
|
* that contains both the existing rectangle and the specified point. |
184 |
|
* |
185 |
|
* @param x The X coordinate of the point to add to this rectangle. |
186 |
|
* @param y The Y coordinate of the point to add to this rectangle. |
187 |
|
*/ |
188 |
|
public void add(int newx, int newy) |
189 |
|
{ |
190 |
|
int x = this.x > newx ? newx : this.x; |
191 |
|
int y = this.y > newy ? newy : this.y; |
192 |
|
width = (this.x + width > newx ? this.x + width : newx) - x; |
193 |
|
height = (this.y + height > newy ? this.y + height : newy) - y; |
194 |
|
this.x = x; |
195 |
|
this.y = y; |
196 |
|
} |
197 |
|
|
198 |
|
/** |
199 |
|
* Modifies this rectangle so that it represents the smallest rectangle |
200 |
|
* that contains both the existing rectangle and the specified point. |
201 |
|
* |
202 |
|
* @param point The point to add to this rectangle. |
203 |
|
*/ |
204 |
|
public void add(Point pt) |
205 |
|
{ |
206 |
|
add (pt.x, pt.y); |
207 |
|
} |
208 |
|
|
209 |
|
/** |
210 |
|
* Modifies this rectangle so that it represents the smallest rectangle |
211 |
|
* that contains both the existing rectangle and the specified rectangle. |
212 |
|
* |
213 |
|
* @param rect The rectangle to add to this rectangle. |
214 |
|
*/ |
215 |
|
public void add(Rectangle r) |
216 |
|
{ |
217 |
|
int x = this.x > r.x ? r.x : this.x; |
218 |
|
int y = this.y > r.y ? r.y : this.y; |
219 |
|
width = (this.x + width > r.x + r.width ? |
220 |
|
this.x + width : r.x + r.width) - x; |
221 |
|
height = (this.y + height > r.y + r.height ? |
222 |
|
this.y + height : r.y + r.height) - y; |
223 |
|
this.x = x; |
224 |
|
this.y = y; |
225 |
|
} |
226 |
|
|
227 |
|
/** |
228 |
|
* Tests whether or not the specified point is inside this rectangle. |
229 |
|
* |
230 |
|
* @param x The X coordinate of the point to test. |
231 |
|
* @param y The Y coordinate of the point to test. |
232 |
|
* |
233 |
|
* @return <code>true</code> if the point is inside the rectangle, |
234 |
|
* <code>false</code> otherwise. |
235 |
|
*/ |
236 |
|
public boolean contains(int x, int y) |
237 |
|
{ |
238 |
|
return (x >= this.x && x <= this.x + this.width |
239 |
|
&& y >= this.y && y <= this.y + this.height); |
240 |
|
} |
241 |
|
|
242 |
|
public boolean contains(int x, int y, int w, int h) |
243 |
|
{ |
244 |
|
return (x >= this.x && x + w <= this.x + this.width |
245 |
|
&& y >= this.y && y + h <= this.y + this.height); |
246 |
|
} |
247 |
|
|
248 |
|
/** |
249 |
|
* Tests whether or not the specified point is inside this rectangle. |
250 |
|
* |
251 |
|
* @param point The point to test. |
252 |
|
* |
253 |
|
* @return <code>true</code> if the point is inside the rectangle, |
254 |
|
* <code>false</code> otherwise. |
255 |
|
*/ |
256 |
|
public boolean contains(Point p) |
257 |
|
{ |
258 |
|
return contains(p.x, p.y); |
259 |
|
} |
260 |
|
|
261 |
|
public boolean contains(Rectangle r) |
262 |
|
{ |
263 |
|
return contains(r.x, r.y, r.width, r.height); |
264 |
|
} |
265 |
|
|
266 |
|
/** |
267 |
|
* Tests this rectangle for equality against the specified object. This |
268 |
|
* will be true if an only if the specified object: |
269 |
|
* <p> |
270 |
|
* <ul> |
271 |
|
* <li>Is not <code>null</code>. |
272 |
|
* <li>Is an instance of <code>Rectangle</code>. |
273 |
|
* <li>Has X and Y coordinates identical to this rectangle. |
274 |
|
* <li>Has a width and height identical to this rectangle. |
275 |
|
* </ul> |
276 |
|
* |
277 |
|
* @param obj The object to test against for equality. |
278 |
|
* |
279 |
|
* @return <code>true</code> if the specified object is equal to this one, |
280 |
|
* <code>false</code> otherwise. |
281 |
|
*/ |
282 |
|
public boolean equals(Object obj) |
283 |
|
{ |
284 |
|
if (obj instanceof Rectangle) |
285 |
|
{ |
286 |
|
Rectangle r = (Rectangle) obj; |
287 |
|
return (r.x == x |
288 |
|
&& r.y == y |
289 |
|
&& r.width == width |
290 |
|
&& r.height == height); |
291 |
|
} |
292 |
|
return false; |
293 |
|
} |
294 |
|
|
295 |
|
public double getHeight() |
296 |
|
{ |
297 |
|
return (double) this.height; |
298 |
|
} |
299 |
|
|
300 |
|
/** |
301 |
|
* Returns the location of this rectangle, which is the coordinates of |
302 |
|
* its upper left corner. |
303 |
|
* |
304 |
|
* @return The point where this rectangle is located. |
305 |
|
*/ |
306 |
|
public Point getLocation() |
307 |
|
{ |
308 |
|
return new Point(x,y); |
309 |
|
} |
310 |
|
|
311 |
|
/** |
312 |
|
* Returns the size of this rectangle. |
313 |
|
* |
314 |
|
* @return The size of this rectangle. |
315 |
|
*/ |
316 |
|
public Dimension getSize() |
317 |
|
{ |
318 |
|
return new Dimension(width, height); |
319 |
|
} |
320 |
|
|
321 |
|
public double getWidth() |
322 |
|
{ |
323 |
|
return (double) this.width; |
324 |
|
} |
325 |
|
|
326 |
|
public double getX() |
327 |
|
{ |
328 |
|
return (double) x; |
329 |
|
} |
330 |
|
|
331 |
|
public double getY() |
332 |
|
{ |
333 |
|
return (double) y; |
334 |
|
} |
335 |
|
|
336 |
|
/** |
337 |
|
* Expands the rectangle by the specified amount. The horizontal |
338 |
|
* and vertical expansion values are applied both to the X,Y coordinate |
339 |
|
* of this rectangle, and its width and height. Thus the width and |
340 |
|
* height will increase by 2h and 2v accordingly. |
341 |
|
* |
342 |
|
* @param h The horizontal expansion value. |
343 |
|
* @param v The vertical expansion value. |
344 |
|
*/ |
345 |
|
public void grow(int h, int v) |
346 |
|
{ |
347 |
|
width += h; |
348 |
|
height += v; |
349 |
|
} |
350 |
|
|
351 |
|
/** |
352 |
|
* Tests whether or not the specified point is inside this rectangle. |
353 |
|
* |
354 |
|
* @param x The X coordinate of the point to test. |
355 |
|
* @param y The Y coordinate of the point to test. |
356 |
|
* |
357 |
|
* @return <code>true</code> if the point is inside the rectangle, |
358 |
|
* <code>false</code> otherwise. |
359 |
|
* |
360 |
|
* @deprecated This method is deprecated in favor of |
361 |
|
* <code>contains(int, int)</code>. |
362 |
|
*/ |
363 |
|
public boolean inside(int x, int y) |
364 |
|
{ |
365 |
|
return contains(x, y); |
366 |
|
} |
367 |
|
|
368 |
|
/** |
369 |
|
* Determines the rectange which is formed by the intersection of this |
370 |
|
* rectangle with the specified rectangle. |
371 |
|
* |
372 |
|
* @param rect The rectange to calculate the intersection with. |
373 |
|
* |
374 |
|
* @return The rectangle bounding the intersection. |
375 |
|
* |
376 |
|
* @specnote If there is no intersection, an empty rectangle at 0,0 |
377 |
|
* is returned. |
378 |
|
*/ |
379 |
|
public Rectangle intersection(Rectangle r) |
380 |
|
{ |
381 |
|
int newx = x < r.x ? r.x : x; |
382 |
|
int newy = y < r.y ? r.y : y; |
383 |
|
int neww = (x + width < r.x + r.width ? |
384 |
|
x + width : r.x + r.width) - newx; |
385 |
|
int newh = (y + height < r.y + r.height ? |
386 |
|
y + height : r.y + r.height) - newy; |
387 |
|
if (neww >= 0 && newh >= 0) |
388 |
|
return new Rectangle(newx, newy, neww, newh); |
389 |
|
else |
390 |
|
return new Rectangle(0, 0, 0, 0); |
391 |
|
} |
392 |
|
|
393 |
|
/** |
394 |
|
* Tests whether or not the specified rectangle intersects this rectangle. |
395 |
|
* |
396 |
|
* @param rect The rectangle to test against. |
397 |
|
* |
398 |
|
* @return <code>true</code> if the specified rectangle intersects this |
399 |
|
* one, <code>false</code> otherwise. |
400 |
|
* |
401 |
|
* @specnote If the intersection is at an edge or corner only (an empty |
402 |
|
* intersection with a non-zero location), false is returned. |
403 |
|
*/ |
404 |
|
public boolean intersects(Rectangle r) |
405 |
|
{ |
406 |
|
int neww = (x + width < r.x + r.width ? |
407 |
|
x + width : r.x + r.width) - (x < r.x ? r.x : x); |
408 |
|
int newh = (y + height < r.y + r.height ? |
409 |
|
y + height : r.y + r.height) - (y < r.y ? r.y : y); |
410 |
|
return (neww > 0 && newh > 0); |
411 |
|
} |
412 |
|
|
413 |
|
/** |
414 |
|
* Tests whether or not this rectangle is empty. An empty rectangle |
415 |
|
* has a width or height of zero. |
416 |
|
* |
417 |
|
* @return <code>true</code> if the rectangle is empty, <code>false</code> |
418 |
|
* otherwise. |
419 |
|
*/ |
420 |
|
public boolean isEmpty() |
421 |
|
{ |
422 |
|
return !(width > 0 && height > 0); |
423 |
|
} |
424 |
|
|
425 |
|
/** |
426 |
|
* Moves the location of this rectangle by setting its upper left |
427 |
|
* corner to the specified coordinates. |
428 |
|
* // FIXME: Is this true? |
429 |
|
* |
430 |
|
* @param x The new X coordinate for this rectangle. |
431 |
|
* @param y The new Y coordinate for this rectangle. |
432 |
|
* |
433 |
|
* @deprecated This method is deprecated in favor of |
434 |
|
* <code>setLocation(int, int)</code>. |
435 |
|
*/ |
436 |
|
public void move(int x, int y) |
437 |
|
{ |
438 |
|
setLocation(x, y); |
439 |
|
} |
440 |
|
|
441 |
|
public int outcode(double x, double y) |
442 |
|
{ |
443 |
|
// FIXME |
444 |
|
return 0; |
445 |
|
} |
446 |
|
|
447 |
|
/** |
448 |
|
* Updates this rectangle to have the specified dimensions. |
449 |
|
* |
450 |
|
* @param x The new X coordinate of the upper left hand corner. |
451 |
|
* @param y The new Y coordinate of the upper left hand corner. |
452 |
|
* @param width The new width of this rectangle. |
453 |
|
* @param height The new height of this rectangle. |
454 |
|
* |
455 |
|
* @deprecated This method is deprecated in favor of |
456 |
|
* <code>setBounds(int, int, int, int)</code>. |
457 |
|
*/ |
458 |
|
public void reshape(int x, int y, int width, int height) |
459 |
|
{ |
460 |
|
setBounds(x, y, width, height); |
461 |
|
} |
462 |
|
|
463 |
|
/** |
464 |
|
* Sets the size of this rectangle based on the specified dimensions. |
465 |
|
* |
466 |
|
* @param width The new width of the rectangle. |
467 |
|
* @param height The new height of the rectangle. |
468 |
|
* |
469 |
|
* @deprecated This method is deprecated in favor of |
470 |
|
* <code>setSize(int, int)</code>. |
471 |
|
*/ |
472 |
|
public void resize(int width, int height) |
473 |
|
{ |
474 |
|
setSize(width, height); |
475 |
|
} |
476 |
|
|
477 |
|
/** |
478 |
|
* Updates this rectangle to have the specified dimensions. |
479 |
|
* |
480 |
|
* @param x The new X coordinate of the upper left hand corner. |
481 |
|
* @param y The new Y coordinate of the upper left hand corner. |
482 |
|
* @param width The new width of this rectangle. |
483 |
|
* @param height The new height of this rectangle. |
484 |
|
*/ |
485 |
|
public void setBounds(int x, int y, int width, int height) |
486 |
|
{ |
487 |
|
this.x = x; |
488 |
|
this.y = y; |
489 |
|
this.width = width; |
490 |
|
this.height = height; |
491 |
|
} |
492 |
|
|
493 |
|
/** |
494 |
|
* Updates this rectangle to match the dimensions of the specified |
495 |
|
* rectangle. |
496 |
|
* |
497 |
|
* @param rect The rectangle to update from. |
498 |
|
*/ |
499 |
|
public void setBounds(Rectangle r) |
500 |
|
{ |
501 |
|
this.x = r.x; |
502 |
|
this.y = r.y; |
503 |
|
this.width = r.width; |
504 |
|
this.height = r.height; |
505 |
|
} |
506 |
|
|
507 |
|
/** |
508 |
|
* Moves the location of this rectangle by setting its upper left |
509 |
|
* corner to the specified coordinates. |
510 |
|
* // FIXME: Is this true? |
511 |
|
* |
512 |
|
* @param x The new X coordinate for this rectangle. |
513 |
|
* @param y The new Y coordinate for this rectangle. |
514 |
|
*/ |
515 |
|
public void setLocation(int x, int y) |
516 |
|
{ |
517 |
|
this.x = x; |
518 |
|
this.y = y; |
519 |
|
} |
520 |
|
|
521 |
|
/** |
522 |
|
* Moves the location of this rectangle by setting its upper left |
523 |
|
* corner to the specified point. |
524 |
|
* // FIXME: Is this true? |
525 |
|
* |
526 |
|
* @param point The point to move the rectange to. |
527 |
|
*/ |
528 |
|
public void setLocation(Point p) |
529 |
|
{ |
530 |
|
this.x = p.x; |
531 |
|
this.y = p.y; |
532 |
|
} |
533 |
|
|
534 |
|
public void setRect(double x, double y, double width, double height) |
535 |
|
{ |
536 |
|
this.x = (int) x; |
537 |
|
this.y = (int) y; |
538 |
|
this.width = (int) width; |
539 |
|
this.height = (int) height; |
540 |
|
} |
541 |
|
|
542 |
|
/** |
543 |
|
* Sets the size of this rectangle based on the specified dimensions. |
544 |
|
* |
545 |
|
* @param dim The new dimensions of the rectangle. |
546 |
|
*/ |
547 |
|
public void setSize(Dimension d) |
548 |
|
{ |
549 |
|
this.width = d.width; |
550 |
|
this.height = d.height; |
551 |
|
} |
552 |
|
|
553 |
|
/** |
554 |
|
* Sets the size of this rectangle based on the specified dimensions. |
555 |
|
* |
556 |
|
* @param width The new width of the rectangle. |
557 |
|
* @param height The new height of the rectangle. |
558 |
|
*/ |
559 |
|
public void setSize(int width, int height) |
560 |
|
{ |
561 |
|
this.width = width; |
562 |
|
this.height = height; |
563 |
|
} |
564 |
|
|
565 |
|
public void translate(int x, int y) |
566 |
|
{ |
567 |
|
x += x; |
568 |
|
y += y; |
569 |
|
} |
570 |
|
|
571 |
|
/** |
572 |
|
* Returns the smallest rectangle that contains both this rectangle |
573 |
|
* and the specified rectangle. |
574 |
|
* |
575 |
|
* @param rect The rectangle to compute the union with. |
576 |
|
* |
577 |
|
* @return The smallest rectangle containing both rectangles. |
578 |
|
*/ |
579 |
|
public Rectangle union(Rectangle r) |
580 |
|
{ |
581 |
|
int newx = x > r.x ? r.x : x; |
582 |
|
int newy = y > r.y ? r.y : y; |
583 |
|
int neww = (this.x + width > r.x + r.width ? |
584 |
|
this.x + width : r.x + r.width) - newx; |
585 |
|
int newh = (this.y + height > r.y + r.height ? |
586 |
|
this.y + height : r.y + r.height) - newy; |
587 |
|
return new Rectangle(newx, newy, neww, newh); |
588 |
|
} |
589 |
|
|
590 |
|
// Commented out until we have Rectangle2D |
591 |
|
public Rectangle2D createIntersection(Rectangle2D r) |
592 |
|
{ |
593 |
|
// FIXME: maybe we should consider returning a Rectangle or |
594 |
|
// Rectangle2D.Float depending on type of R. |
595 |
|
Rectangle2D.Double res = new Rectangle2D.Double (); |
596 |
|
intersect (this, r, res); |
597 |
|
return res; |
598 |
|
} |
599 |
|
|
600 |
|
public Rectangle2D createUnion(Rectangle2D r) |
601 |
|
{ |
602 |
|
// FIXME: maybe we should consider returning a Rectangle or |
603 |
|
// Rectangle2D.Float depending on type of R. |
604 |
|
Rectangle2D.Double res = new Rectangle2D.Double (); |
605 |
|
union (this, r, res); |
606 |
|
return res; |
607 |
|
} |
608 |
|
|
609 |
|
public Rectangle2D getBounds2D() |
610 |
|
{ |
611 |
|
return new Rectangle (x, y, width, height); |
612 |
|
} |
613 |
|
|
614 |
|
/** |
615 |
|
* Returns a string representation of this rectangle. |
616 |
|
* |
617 |
|
* @return A string representation of this rectangle. |
618 |
|
*/ |
619 |
|
public String toString() |
620 |
|
{ |
621 |
|
return getClass().getName() + "[x=" + x + ",y=" + y + ",width=" + width + |
622 |
|
",height=" + height + "]"; |
623 |
|
} |
624 |
|
|
625 |
|
/** |
626 |
|
* Returns a hash value for this object. |
627 |
|
* |
628 |
|
* @return A hash value for this object. |
629 |
|
*/ |
630 |
|
public int hashCode() |
631 |
|
{ |
632 |
|
return x * y * width * height * 37; |
633 |
|
} |
634 |
} |
} |
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Sets the size of this rectangle based on the specified dimensions. |
|
|
* |
|
|
* @param dim The new dimensions of the rectangle. |
|
|
*/ |
|
|
public void |
|
|
setSize(Dimension dim) |
|
|
{ |
|
|
synchronized(this) |
|
|
{ |
|
|
this.width = dim.width; |
|
|
this.height = dim.height; |
|
|
} |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Sets the size of this rectangle based on the specified dimensions. |
|
|
* |
|
|
* @param width The new width of the rectangle. |
|
|
* @param height The new height of the rectangle. |
|
|
*/ |
|
|
public void |
|
|
setSize(int width, int height) |
|
|
{ |
|
|
synchronized(this) |
|
|
{ |
|
|
this.width = width; |
|
|
this.height = height; |
|
|
} |
|
|
} |
|
|
|
|
|
public void |
|
|
translate (int dx, int dy) |
|
|
{ |
|
|
synchronized (this) |
|
|
{ |
|
|
this.x += dx; |
|
|
this.y += dy; |
|
|
} |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Sets the size of this rectangle based on the specified dimensions. |
|
|
* |
|
|
* @param width The new width of the rectangle. |
|
|
* @param height The new height of the rectangle. |
|
|
* |
|
|
* @deprecated This method is deprecated in favor of |
|
|
* <code>setSize(int, int)</code>. |
|
|
*/ |
|
|
public void |
|
|
resize(int width, int height) |
|
|
{ |
|
|
setSize(width, height); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Tests whether or not the specified point is inside this rectangle. |
|
|
* |
|
|
* @param point The point to test. |
|
|
* |
|
|
* @return <code>true</code> if the point is inside the rectangle, |
|
|
* <code>false</code> otherwise. |
|
|
*/ |
|
|
public boolean |
|
|
contains(Point point) |
|
|
{ |
|
|
return(contains(point.x, point.y)); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Tests whether or not the specified point is inside this rectangle. |
|
|
* |
|
|
* @param x The X coordinate of the point to test. |
|
|
* @param y The Y coordinate of the point to test. |
|
|
* |
|
|
* @return <code>true</code> if the point is inside the rectangle, |
|
|
* <code>false</code> otherwise. |
|
|
*/ |
|
|
public boolean |
|
|
contains(int x, int y) |
|
|
{ |
|
|
// FIXME: Do we count points on the rectangle? |
|
|
if ((x < this.x) || (x > (this.x + width))) |
|
|
return(false); |
|
|
if ((y < this.y) || (y > (this.y + height))) |
|
|
return(false); |
|
|
|
|
|
return(true); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Tests whether or not the specified point is inside this rectangle. |
|
|
* |
|
|
* @param x The X coordinate of the point to test. |
|
|
* @param y The Y coordinate of the point to test. |
|
|
* |
|
|
* @return <code>true</code> if the point is inside the rectangle, |
|
|
* <code>false</code> otherwise. |
|
|
* |
|
|
* @deprecated This method is deprecated in favor of |
|
|
* <code>contains(int, int)</code>. |
|
|
*/ |
|
|
public boolean |
|
|
inside(int x, int y) |
|
|
{ |
|
|
return(contains(x, y)); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Tests whether or not the specified rectangle intersects this rectangle. |
|
|
* |
|
|
* @param rect The rectangle to test against. |
|
|
* |
|
|
* @return <code>true</code> if the specified rectangle intersects this |
|
|
* one, <code>false</code> otherwise. |
|
|
*/ |
|
|
public boolean |
|
|
intersects(Rectangle rect) |
|
|
{ |
|
|
if (contains(rect.x, rect.y)) |
|
|
return(true); |
|
|
if (contains(rect.x + width, rect.y)) |
|
|
return(true); |
|
|
if (contains(rect.x, rect.y + height)) |
|
|
return(true); |
|
|
if (contains(rect.x + width, rect.y + height)) |
|
|
return(true); |
|
|
|
|
|
return(false); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Determines the rectange which is formed by the intersection of this |
|
|
* rectangle with the specified rectangle. |
|
|
* |
|
|
* @param rect The rectange to calculate the intersection with. |
|
|
* |
|
|
* @return The rectangle bounding the intersection. |
|
|
*/ |
|
|
public Rectangle |
|
|
intersection(Rectangle rect) |
|
|
{ |
|
|
// FIXME: What do we do if there is no intersection? I'll return an |
|
|
// empty rectangle. |
|
|
if (!intersects(rect)) |
|
|
return(new Rectangle()); |
|
|
|
|
|
int nx, ny, nw, nh; |
|
|
|
|
|
if (x > rect.x) |
|
|
nx = x; |
|
|
else |
|
|
nx = rect.x; |
|
|
|
|
|
if (y > rect.y) |
|
|
ny = y; |
|
|
else |
|
|
ny = rect.y; |
|
|
|
|
|
if ((x + width) < (rect.x + rect.width)) |
|
|
nw = (x + width) - nx; |
|
|
else |
|
|
nw = (rect.x + rect.width) - nx; |
|
|
|
|
|
if ((y + height) < (rect.y + rect.height)) |
|
|
nh = (y + height) - ny; |
|
|
else |
|
|
nh = (rect.y + rect.height) - ny; |
|
|
|
|
|
return(new Rectangle(nx, ny, nw, nh)); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Returns the smallest rectangle that contains both this rectangle |
|
|
* and the specified rectangle. |
|
|
* |
|
|
* @param rect The rectangle to compute the union with. |
|
|
* |
|
|
* @return The smallest rectangle containing both rectangles. |
|
|
*/ |
|
|
public Rectangle |
|
|
union(Rectangle rect) |
|
|
{ |
|
|
int nx, ny, nw, nh; |
|
|
|
|
|
if (x < rect.x) |
|
|
nx = x; |
|
|
else |
|
|
nx = rect.x; |
|
|
|
|
|
if (y < rect.y) |
|
|
ny = y; |
|
|
else |
|
|
ny = rect.y; |
|
|
|
|
|
if ((x + width) > (rect.x + rect.width)) |
|
|
nw = (x + width) - nx; |
|
|
else |
|
|
nw = (rect.x + rect.width) - nx; |
|
|
|
|
|
if ((y - height) > (rect.y - rect.height)) |
|
|
nh = (y + height) - ny; |
|
|
else |
|
|
nh = (rect.y + rect.height) - ny; |
|
|
|
|
|
return(new Rectangle(nx, ny, nw, nh)); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Modifies this rectangle so that it represents the smallest rectangle |
|
|
* that contains both the existing rectangle and the specified point. |
|
|
* |
|
|
* @param point The point to add to this rectangle. |
|
|
*/ |
|
|
public void |
|
|
add(Point point) |
|
|
{ |
|
|
add(new Rectangle(point)); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Modifies this rectangle so that it represents the smallest rectangle |
|
|
* that contains both the existing rectangle and the specified point. |
|
|
* |
|
|
* @param x The X coordinate of the point to add to this rectangle. |
|
|
* @param y The Y coordinate of the point to add to this rectangle. |
|
|
*/ |
|
|
public void |
|
|
add(int x, int y) |
|
|
{ |
|
|
add(new Rectangle(new Point(x, y))); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Modifies this rectangle so that it represents the smallest rectangle |
|
|
* that contains both the existing rectangle and the specified rectangle. |
|
|
* |
|
|
* @param rect The rectangle to add to this rectangle. |
|
|
*/ |
|
|
public void |
|
|
add(Rectangle rect) |
|
|
{ |
|
|
Rectangle newrect = union(rect); |
|
|
|
|
|
synchronized(this) |
|
|
{ |
|
|
this.x = newrect.x; |
|
|
this.y = newrect.y; |
|
|
this.width = newrect.width; |
|
|
this.height = newrect.height; |
|
|
} |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Expands the rectangle by the specified amount. The horizontal |
|
|
* and vertical expansion values are applied both to the X,Y coordinate |
|
|
* of this rectangle, and its width and height. Thus the width and |
|
|
* height will increase by 2h and 2v accordingly. |
|
|
* |
|
|
* @param h The horizontal expansion value. |
|
|
* @param v The vertical expansion value. |
|
|
*/ |
|
|
public void |
|
|
grow(int h, int v) |
|
|
{ |
|
|
synchronized(this) |
|
|
{ |
|
|
x -= h; |
|
|
y -= v; |
|
|
width += (2*h); |
|
|
height += (2*h); |
|
|
} |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Tests whether or not this rectangle is empty. An empty rectangle |
|
|
* has a width or height of zero. |
|
|
* |
|
|
* @return <code>true</code> if the rectangle is empty, <code>false</code> |
|
|
* otherwise. |
|
|
*/ |
|
|
public boolean |
|
|
isEmpty() |
|
|
{ |
|
|
if ((width <= 0) || (height <= 0)) |
|
|
return(true); |
|
|
else |
|
|
return(false); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Tests this rectangle for equality against the specified object. This |
|
|
* will be true if an only if the specified object: |
|
|
* <p> |
|
|
* <ul> |
|
|
* <li>Is not <code>null</code>. |
|
|
* <li>Is an instance of <code>Rectangle</code>. |
|
|
* <li>Has X and Y coordinates identical to this rectangle. |
|
|
* <li>Has a width and height identical to this rectangle. |
|
|
* </ul> |
|
|
* |
|
|
* @param obj The object to test against for equality. |
|
|
* |
|
|
* @return <code>true</code> if the specified object is equal to this one, |
|
|
* <code>false</code> otherwise. |
|
|
*/ |
|
|
public boolean |
|
|
equals(Object obj) |
|
|
{ |
|
|
if (obj == null) |
|
|
return(false); |
|
|
|
|
|
if (!(obj instanceof Rectangle)) |
|
|
return(false); |
|
|
|
|
|
Rectangle rect = (Rectangle)obj; |
|
|
|
|
|
if ((rect.x != x) || (rect.y != y) || (rect.width != width) || |
|
|
(rect.height != height)) |
|
|
return(false); |
|
|
|
|
|
return(true); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Returns a hash value for this object. |
|
|
* |
|
|
* @return A hash value for this object. |
|
|
*/ |
|
|
public int |
|
|
hashCode() |
|
|
{ |
|
|
return(x*y*width*height*37); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Returns a string representation of this rectangle. |
|
|
* |
|
|
* @return A string representation of this rectangle. |
|
|
*/ |
|
|
public String |
|
|
toString() |
|
|
{ |
|
|
return(getClass().getName() + "(x=" + x + ", y=" + y + ", width=" + |
|
|
width + ", height=" + height + ")"); |
|
|
} |
|
|
|
|
|
} // class Rectangle |
|
|
|
|