2
|
1 |
/*
|
|
2 |
* Copyright 1995-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package java.awt;
|
|
27 |
|
|
28 |
import java.awt.geom.Rectangle2D;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* A <code>Rectangle</code> specifies an area in a coordinate space that is
|
|
32 |
* enclosed by the <code>Rectangle</code> object's upper-left point
|
|
33 |
* {@code (x,y)}
|
|
34 |
* in the coordinate space, its width, and its height.
|
|
35 |
* <p>
|
|
36 |
* A <code>Rectangle</code> object's <code>width</code> and
|
|
37 |
* <code>height</code> are <code>public</code> fields. The constructors
|
|
38 |
* that create a <code>Rectangle</code>, and the methods that can modify
|
|
39 |
* one, do not prevent setting a negative value for width or height.
|
|
40 |
* <p>
|
|
41 |
* <a name="Empty">
|
|
42 |
* A {@code Rectangle} whose width or height is exactly zero has location
|
|
43 |
* along those axes with zero dimension, but is otherwise considered empty.
|
|
44 |
* The {@link #isEmpty} method will return true for such a {@code Rectangle}.
|
|
45 |
* Methods which test if an empty {@code Rectangle} contains or intersects
|
|
46 |
* a point or rectangle will always return false if either dimension is zero.
|
|
47 |
* Methods which combine such a {@code Rectangle} with a point or rectangle
|
|
48 |
* will include the location of the {@code Rectangle} on that axis in the
|
|
49 |
* result as if the {@link #add(Point)} method were being called.
|
|
50 |
* </a>
|
|
51 |
* <p>
|
|
52 |
* <a name="NonExistant">
|
|
53 |
* A {@code Rectangle} whose width or height is negative has neither
|
|
54 |
* location nor dimension along those axes with negative dimensions.
|
|
55 |
* Such a {@code Rectangle} is treated as non-existant along those axes.
|
|
56 |
* Such a {@code Rectangle} is also empty with respect to containment
|
|
57 |
* calculations and methods which test if it contains or intersects a
|
|
58 |
* point or rectangle will always return false.
|
|
59 |
* Methods which combine such a {@code Rectangle} with a point or rectangle
|
|
60 |
* will ignore the {@code Rectangle} entirely in generating the result.
|
|
61 |
* If two {@code Rectangle} objects are combined and each has a negative
|
|
62 |
* dimension, the result will have at least one negative dimension.
|
|
63 |
* </a>
|
|
64 |
* <p>
|
|
65 |
* Methods which affect only the location of a {@code Rectangle} will
|
|
66 |
* operate on its location regardless of whether or not it has a negative
|
|
67 |
* or zero dimension along either axis.
|
|
68 |
* <p>
|
|
69 |
* Note that a {@code Rectangle} constructed with the default no-argument
|
|
70 |
* constructor will have dimensions of {@code 0x0} and therefore be empty.
|
|
71 |
* That {@code Rectangle} will still have a location of {@code (0,0)} and
|
|
72 |
* will contribute that location to the union and add operations.
|
|
73 |
* Code attempting to accumulate the bounds of a set of points should
|
|
74 |
* therefore initially construct the {@code Rectangle} with a specifically
|
|
75 |
* negative width and height or it should use the first point in the set
|
|
76 |
* to construct the {@code Rectangle}.
|
|
77 |
* For example:
|
|
78 |
* <pre>
|
|
79 |
* Rectangle bounds = new Rectangle(0, 0, -1, -1);
|
|
80 |
* for (int i = 0; i < points.length; i++) {
|
|
81 |
* bounds.add(points[i]);
|
|
82 |
* }
|
|
83 |
* </pre>
|
|
84 |
* or if we know that the points array contains at least one point:
|
|
85 |
* <pre>
|
|
86 |
* Rectangle bounds = new Rectangle(points[0]);
|
|
87 |
* for (int i = 1; i < points.length; i++) {
|
|
88 |
* bounds.add(points[i]);
|
|
89 |
* }
|
|
90 |
* </pre>
|
|
91 |
* <p>
|
|
92 |
* This class uses 32-bit integers to store its location and dimensions.
|
|
93 |
* Frequently operations may produce a result that exceeds the range of
|
|
94 |
* a 32-bit integer.
|
|
95 |
* The methods will calculate their results in a way that avoids any
|
|
96 |
* 32-bit overflow for intermediate results and then choose the best
|
|
97 |
* representation to store the final results back into the 32-bit fields
|
|
98 |
* which hold the location and dimensions.
|
|
99 |
* The location of the result will be stored into the {@link #x} and
|
|
100 |
* {@link #y} fields by clipping the true result to the nearest 32-bit value.
|
|
101 |
* The values stored into the {@link #width} and {@link #height} dimension
|
|
102 |
* fields will be chosen as the 32-bit values that encompass the largest
|
|
103 |
* part of the true result as possible.
|
|
104 |
* Generally this means that the dimension will be clipped independently
|
|
105 |
* to the range of 32-bit integers except that if the location had to be
|
|
106 |
* moved to store it into its pair of 32-bit fields then the dimensions
|
|
107 |
* will be adjusted relative to the "best representation" of the location.
|
|
108 |
* If the true result had a negative dimension and was therefore
|
|
109 |
* non-existant along one or both axes, the stored dimensions will be
|
|
110 |
* negative numbers in those axes.
|
|
111 |
* If the true result had a location that could be represented within
|
|
112 |
* the range of 32-bit integers, but zero dimension along one or both
|
|
113 |
* axes, then the stored dimensions will be zero in those axes.
|
|
114 |
*
|
|
115 |
* @author Sami Shaio
|
|
116 |
* @since 1.0
|
|
117 |
*/
|
|
118 |
public class Rectangle extends Rectangle2D
|
|
119 |
implements Shape, java.io.Serializable
|
|
120 |
{
|
|
121 |
|
|
122 |
/**
|
|
123 |
* The X coordinate of the upper-left corner of the <code>Rectangle</code>.
|
|
124 |
*
|
|
125 |
* @serial
|
|
126 |
* @see #setLocation(int, int)
|
|
127 |
* @see #getLocation()
|
|
128 |
* @since 1.0
|
|
129 |
*/
|
|
130 |
public int x;
|
|
131 |
|
|
132 |
/**
|
|
133 |
* The Y coordinate of the upper-left corner of the <code>Rectangle</code>.
|
|
134 |
*
|
|
135 |
* @serial
|
|
136 |
* @see #setLocation(int, int)
|
|
137 |
* @see #getLocation()
|
|
138 |
* @since 1.0
|
|
139 |
*/
|
|
140 |
public int y;
|
|
141 |
|
|
142 |
/**
|
|
143 |
* The width of the <code>Rectangle</code>.
|
|
144 |
* @serial
|
|
145 |
* @see #setSize(int, int)
|
|
146 |
* @see #getSize()
|
|
147 |
* @since 1.0
|
|
148 |
*/
|
|
149 |
public int width;
|
|
150 |
|
|
151 |
/**
|
|
152 |
* The height of the <code>Rectangle</code>.
|
|
153 |
*
|
|
154 |
* @serial
|
|
155 |
* @see #setSize(int, int)
|
|
156 |
* @see #getSize()
|
|
157 |
* @since 1.0
|
|
158 |
*/
|
|
159 |
public int height;
|
|
160 |
|
|
161 |
/*
|
|
162 |
* JDK 1.1 serialVersionUID
|
|
163 |
*/
|
|
164 |
private static final long serialVersionUID = -4345857070255674764L;
|
|
165 |
|
|
166 |
/**
|
|
167 |
* Initialize JNI field and method IDs
|
|
168 |
*/
|
|
169 |
private static native void initIDs();
|
|
170 |
|
|
171 |
static {
|
|
172 |
/* ensure that the necessary native libraries are loaded */
|
|
173 |
Toolkit.loadLibraries();
|
|
174 |
if (!GraphicsEnvironment.isHeadless()) {
|
|
175 |
initIDs();
|
|
176 |
}
|
|
177 |
}
|
|
178 |
|
|
179 |
/**
|
|
180 |
* Constructs a new <code>Rectangle</code> whose upper-left corner
|
|
181 |
* is at (0, 0) in the coordinate space, and whose width and
|
|
182 |
* height are both zero.
|
|
183 |
*/
|
|
184 |
public Rectangle() {
|
|
185 |
this(0, 0, 0, 0);
|
|
186 |
}
|
|
187 |
|
|
188 |
/**
|
|
189 |
* Constructs a new <code>Rectangle</code>, initialized to match
|
|
190 |
* the values of the specified <code>Rectangle</code>.
|
|
191 |
* @param r the <code>Rectangle</code> from which to copy initial values
|
|
192 |
* to a newly constructed <code>Rectangle</code>
|
|
193 |
* @since 1.1
|
|
194 |
*/
|
|
195 |
public Rectangle(Rectangle r) {
|
|
196 |
this(r.x, r.y, r.width, r.height);
|
|
197 |
}
|
|
198 |
|
|
199 |
/**
|
|
200 |
* Constructs a new <code>Rectangle</code> whose upper-left corner is
|
|
201 |
* specified as
|
|
202 |
* {@code (x,y)} and whose width and height
|
|
203 |
* are specified by the arguments of the same name.
|
|
204 |
* @param x the specified X coordinate
|
|
205 |
* @param y the specified Y coordinate
|
|
206 |
* @param width the width of the <code>Rectangle</code>
|
|
207 |
* @param height the height of the <code>Rectangle</code>
|
|
208 |
* @since 1.0
|
|
209 |
*/
|
|
210 |
public Rectangle(int x, int y, int width, int height) {
|
|
211 |
this.x = x;
|
|
212 |
this.y = y;
|
|
213 |
this.width = width;
|
|
214 |
this.height = height;
|
|
215 |
}
|
|
216 |
|
|
217 |
/**
|
|
218 |
* Constructs a new <code>Rectangle</code> whose upper-left corner
|
|
219 |
* is at (0, 0) in the coordinate space, and whose width and
|
|
220 |
* height are specified by the arguments of the same name.
|
|
221 |
* @param width the width of the <code>Rectangle</code>
|
|
222 |
* @param height the height of the <code>Rectangle</code>
|
|
223 |
*/
|
|
224 |
public Rectangle(int width, int height) {
|
|
225 |
this(0, 0, width, height);
|
|
226 |
}
|
|
227 |
|
|
228 |
/**
|
|
229 |
* Constructs a new <code>Rectangle</code> whose upper-left corner is
|
|
230 |
* specified by the {@link Point} argument, and
|
|
231 |
* whose width and height are specified by the
|
|
232 |
* {@link Dimension} argument.
|
|
233 |
* @param p a <code>Point</code> that is the upper-left corner of
|
|
234 |
* the <code>Rectangle</code>
|
|
235 |
* @param d a <code>Dimension</code>, representing the
|
|
236 |
* width and height of the <code>Rectangle</code>
|
|
237 |
*/
|
|
238 |
public Rectangle(Point p, Dimension d) {
|
|
239 |
this(p.x, p.y, d.width, d.height);
|
|
240 |
}
|
|
241 |
|
|
242 |
/**
|
|
243 |
* Constructs a new <code>Rectangle</code> whose upper-left corner is the
|
|
244 |
* specified <code>Point</code>, and whose width and height are both zero.
|
|
245 |
* @param p a <code>Point</code> that is the top left corner
|
|
246 |
* of the <code>Rectangle</code>
|
|
247 |
*/
|
|
248 |
public Rectangle(Point p) {
|
|
249 |
this(p.x, p.y, 0, 0);
|
|
250 |
}
|
|
251 |
|
|
252 |
/**
|
|
253 |
* Constructs a new <code>Rectangle</code> whose top left corner is
|
|
254 |
* (0, 0) and whose width and height are specified
|
|
255 |
* by the <code>Dimension</code> argument.
|
|
256 |
* @param d a <code>Dimension</code>, specifying width and height
|
|
257 |
*/
|
|
258 |
public Rectangle(Dimension d) {
|
|
259 |
this(0, 0, d.width, d.height);
|
|
260 |
}
|
|
261 |
|
|
262 |
/**
|
|
263 |
* Returns the X coordinate of the bounding <code>Rectangle</code> in
|
|
264 |
* <code>double</code> precision.
|
|
265 |
* @return the X coordinate of the bounding <code>Rectangle</code>.
|
|
266 |
*/
|
|
267 |
public double getX() {
|
|
268 |
return x;
|
|
269 |
}
|
|
270 |
|
|
271 |
/**
|
|
272 |
* Returns the Y coordinate of the bounding <code>Rectangle</code> in
|
|
273 |
* <code>double</code> precision.
|
|
274 |
* @return the Y coordinate of the bounding <code>Rectangle</code>.
|
|
275 |
*/
|
|
276 |
public double getY() {
|
|
277 |
return y;
|
|
278 |
}
|
|
279 |
|
|
280 |
/**
|
|
281 |
* Returns the width of the bounding <code>Rectangle</code> in
|
|
282 |
* <code>double</code> precision.
|
|
283 |
* @return the width of the bounding <code>Rectangle</code>.
|
|
284 |
*/
|
|
285 |
public double getWidth() {
|
|
286 |
return width;
|
|
287 |
}
|
|
288 |
|
|
289 |
/**
|
|
290 |
* Returns the height of the bounding <code>Rectangle</code> in
|
|
291 |
* <code>double</code> precision.
|
|
292 |
* @return the height of the bounding <code>Rectangle</code>.
|
|
293 |
*/
|
|
294 |
public double getHeight() {
|
|
295 |
return height;
|
|
296 |
}
|
|
297 |
|
|
298 |
/**
|
|
299 |
* Gets the bounding <code>Rectangle</code> of this <code>Rectangle</code>.
|
|
300 |
* <p>
|
|
301 |
* This method is included for completeness, to parallel the
|
|
302 |
* <code>getBounds</code> method of
|
|
303 |
* {@link Component}.
|
|
304 |
* @return a new <code>Rectangle</code>, equal to the
|
|
305 |
* bounding <code>Rectangle</code> for this <code>Rectangle</code>.
|
|
306 |
* @see java.awt.Component#getBounds
|
|
307 |
* @see #setBounds(Rectangle)
|
|
308 |
* @see #setBounds(int, int, int, int)
|
|
309 |
* @since 1.1
|
|
310 |
*/
|
|
311 |
public Rectangle getBounds() {
|
|
312 |
return new Rectangle(x, y, width, height);
|
|
313 |
}
|
|
314 |
|
|
315 |
/**
|
|
316 |
* {@inheritDoc}
|
|
317 |
* @since 1.2
|
|
318 |
*/
|
|
319 |
public Rectangle2D getBounds2D() {
|
|
320 |
return new Rectangle(x, y, width, height);
|
|
321 |
}
|
|
322 |
|
|
323 |
/**
|
|
324 |
* Sets the bounding <code>Rectangle</code> of this <code>Rectangle</code>
|
|
325 |
* to match the specified <code>Rectangle</code>.
|
|
326 |
* <p>
|
|
327 |
* This method is included for completeness, to parallel the
|
|
328 |
* <code>setBounds</code> method of <code>Component</code>.
|
|
329 |
* @param r the specified <code>Rectangle</code>
|
|
330 |
* @see #getBounds
|
|
331 |
* @see java.awt.Component#setBounds(java.awt.Rectangle)
|
|
332 |
* @since 1.1
|
|
333 |
*/
|
|
334 |
public void setBounds(Rectangle r) {
|
|
335 |
setBounds(r.x, r.y, r.width, r.height);
|
|
336 |
}
|
|
337 |
|
|
338 |
/**
|
|
339 |
* Sets the bounding <code>Rectangle</code> of this
|
|
340 |
* <code>Rectangle</code> to the specified
|
|
341 |
* <code>x</code>, <code>y</code>, <code>width</code>,
|
|
342 |
* and <code>height</code>.
|
|
343 |
* <p>
|
|
344 |
* This method is included for completeness, to parallel the
|
|
345 |
* <code>setBounds</code> method of <code>Component</code>.
|
|
346 |
* @param x the new X coordinate for the upper-left
|
|
347 |
* corner of this <code>Rectangle</code>
|
|
348 |
* @param y the new Y coordinate for the upper-left
|
|
349 |
* corner of this <code>Rectangle</code>
|
|
350 |
* @param width the new width for this <code>Rectangle</code>
|
|
351 |
* @param height the new height for this <code>Rectangle</code>
|
|
352 |
* @see #getBounds
|
|
353 |
* @see java.awt.Component#setBounds(int, int, int, int)
|
|
354 |
* @since 1.1
|
|
355 |
*/
|
|
356 |
public void setBounds(int x, int y, int width, int height) {
|
|
357 |
reshape(x, y, width, height);
|
|
358 |
}
|
|
359 |
|
|
360 |
/**
|
|
361 |
* Sets the bounds of this {@code Rectangle} to the integer bounds
|
|
362 |
* which encompass the specified {@code x}, {@code y}, {@code width},
|
|
363 |
* and {@code height}.
|
|
364 |
* If the parameters specify a {@code Rectangle} that exceeds the
|
|
365 |
* maximum range of integers, the result will be the best
|
|
366 |
* representation of the specified {@code Rectangle} intersected
|
|
367 |
* with the maximum integer bounds.
|
|
368 |
* @param x the X coordinate of the upper-left corner of
|
|
369 |
* the specified rectangle
|
|
370 |
* @param y the Y coordinate of the upper-left corner of
|
|
371 |
* the specified rectangle
|
|
372 |
* @param width the width of the specified rectangle
|
|
373 |
* @param height the new height of the specified rectangle
|
|
374 |
*/
|
|
375 |
public void setRect(double x, double y, double width, double height) {
|
|
376 |
int newx, newy, neww, newh;
|
|
377 |
|
|
378 |
if (x > 2.0 * Integer.MAX_VALUE) {
|
|
379 |
// Too far in positive X direction to represent...
|
|
380 |
// We cannot even reach the left side of the specified
|
|
381 |
// rectangle even with both x & width set to MAX_VALUE.
|
|
382 |
// The intersection with the "maximal integer rectangle"
|
|
383 |
// is non-existant so we should use a width < 0.
|
|
384 |
// REMIND: Should we try to determine a more "meaningful"
|
|
385 |
// adjusted value for neww than just "-1"?
|
|
386 |
newx = Integer.MAX_VALUE;
|
|
387 |
neww = -1;
|
|
388 |
} else {
|
|
389 |
newx = clip(x, false);
|
|
390 |
if (width >= 0) width += x-newx;
|
|
391 |
neww = clip(width, width >= 0);
|
|
392 |
}
|
|
393 |
|
|
394 |
if (y > 2.0 * Integer.MAX_VALUE) {
|
|
395 |
// Too far in positive Y direction to represent...
|
|
396 |
newy = Integer.MAX_VALUE;
|
|
397 |
newh = -1;
|
|
398 |
} else {
|
|
399 |
newy = clip(y, false);
|
|
400 |
if (height >= 0) height += y-newy;
|
|
401 |
newh = clip(height, height >= 0);
|
|
402 |
}
|
|
403 |
|
|
404 |
reshape(newx, newy, neww, newh);
|
|
405 |
}
|
|
406 |
// Return best integer representation for v, clipped to integer
|
|
407 |
// range and floor-ed or ceiling-ed, depending on the boolean.
|
|
408 |
private static int clip(double v, boolean doceil) {
|
|
409 |
if (v <= Integer.MIN_VALUE) {
|
|
410 |
return Integer.MIN_VALUE;
|
|
411 |
}
|
|
412 |
if (v >= Integer.MAX_VALUE) {
|
|
413 |
return Integer.MAX_VALUE;
|
|
414 |
}
|
|
415 |
return (int) (doceil ? Math.ceil(v) : Math.floor(v));
|
|
416 |
}
|
|
417 |
|
|
418 |
/**
|
|
419 |
* Sets the bounding <code>Rectangle</code> of this
|
|
420 |
* <code>Rectangle</code> to the specified
|
|
421 |
* <code>x</code>, <code>y</code>, <code>width</code>,
|
|
422 |
* and <code>height</code>.
|
|
423 |
* <p>
|
|
424 |
* @param x the new X coordinate for the upper-left
|
|
425 |
* corner of this <code>Rectangle</code>
|
|
426 |
* @param y the new Y coordinate for the upper-left
|
|
427 |
* corner of this <code>Rectangle</code>
|
|
428 |
* @param width the new width for this <code>Rectangle</code>
|
|
429 |
* @param height the new height for this <code>Rectangle</code>
|
|
430 |
* @deprecated As of JDK version 1.1,
|
|
431 |
* replaced by <code>setBounds(int, int, int, int)</code>.
|
|
432 |
*/
|
|
433 |
@Deprecated
|
|
434 |
public void reshape(int x, int y, int width, int height) {
|
|
435 |
this.x = x;
|
|
436 |
this.y = y;
|
|
437 |
this.width = width;
|
|
438 |
this.height = height;
|
|
439 |
}
|
|
440 |
|
|
441 |
/**
|
|
442 |
* Returns the location of this <code>Rectangle</code>.
|
|
443 |
* <p>
|
|
444 |
* This method is included for completeness, to parallel the
|
|
445 |
* <code>getLocation</code> method of <code>Component</code>.
|
|
446 |
* @return the <code>Point</code> that is the upper-left corner of
|
|
447 |
* this <code>Rectangle</code>.
|
|
448 |
* @see java.awt.Component#getLocation
|
|
449 |
* @see #setLocation(Point)
|
|
450 |
* @see #setLocation(int, int)
|
|
451 |
* @since 1.1
|
|
452 |
*/
|
|
453 |
public Point getLocation() {
|
|
454 |
return new Point(x, y);
|
|
455 |
}
|
|
456 |
|
|
457 |
/**
|
|
458 |
* Moves this <code>Rectangle</code> to the specified location.
|
|
459 |
* <p>
|
|
460 |
* This method is included for completeness, to parallel the
|
|
461 |
* <code>setLocation</code> method of <code>Component</code>.
|
|
462 |
* @param p the <code>Point</code> specifying the new location
|
|
463 |
* for this <code>Rectangle</code>
|
|
464 |
* @see java.awt.Component#setLocation(java.awt.Point)
|
|
465 |
* @see #getLocation
|
|
466 |
* @since 1.1
|
|
467 |
*/
|
|
468 |
public void setLocation(Point p) {
|
|
469 |
setLocation(p.x, p.y);
|
|
470 |
}
|
|
471 |
|
|
472 |
/**
|
|
473 |
* Moves this <code>Rectangle</code> to the specified location.
|
|
474 |
* <p>
|
|
475 |
* This method is included for completeness, to parallel the
|
|
476 |
* <code>setLocation</code> method of <code>Component</code>.
|
|
477 |
* @param x the X coordinate of the new location
|
|
478 |
* @param y the Y coordinate of the new location
|
|
479 |
* @see #getLocation
|
|
480 |
* @see java.awt.Component#setLocation(int, int)
|
|
481 |
* @since 1.1
|
|
482 |
*/
|
|
483 |
public void setLocation(int x, int y) {
|
|
484 |
move(x, y);
|
|
485 |
}
|
|
486 |
|
|
487 |
/**
|
|
488 |
* Moves this <code>Rectangle</code> to the specified location.
|
|
489 |
* <p>
|
|
490 |
* @param x the X coordinate of the new location
|
|
491 |
* @param y the Y coordinate of the new location
|
|
492 |
* @deprecated As of JDK version 1.1,
|
|
493 |
* replaced by <code>setLocation(int, int)</code>.
|
|
494 |
*/
|
|
495 |
@Deprecated
|
|
496 |
public void move(int x, int y) {
|
|
497 |
this.x = x;
|
|
498 |
this.y = y;
|
|
499 |
}
|
|
500 |
|
|
501 |
/**
|
|
502 |
* Translates this <code>Rectangle</code> the indicated distance,
|
|
503 |
* to the right along the X coordinate axis, and
|
|
504 |
* downward along the Y coordinate axis.
|
|
505 |
* @param dx the distance to move this <code>Rectangle</code>
|
|
506 |
* along the X axis
|
|
507 |
* @param dy the distance to move this <code>Rectangle</code>
|
|
508 |
* along the Y axis
|
|
509 |
* @see java.awt.Rectangle#setLocation(int, int)
|
|
510 |
* @see java.awt.Rectangle#setLocation(java.awt.Point)
|
|
511 |
*/
|
|
512 |
public void translate(int dx, int dy) {
|
|
513 |
int oldv = this.x;
|
|
514 |
int newv = oldv + dx;
|
|
515 |
if (dx < 0) {
|
|
516 |
// moving leftward
|
|
517 |
if (newv > oldv) {
|
|
518 |
// negative overflow
|
|
519 |
// Only adjust width if it was valid (>= 0).
|
|
520 |
if (width >= 0) {
|
|
521 |
// The right edge is now conceptually at
|
|
522 |
// newv+width, but we may move newv to prevent
|
|
523 |
// overflow. But we want the right edge to
|
|
524 |
// remain at its new location in spite of the
|
|
525 |
// clipping. Think of the following adjustment
|
|
526 |
// conceptually the same as:
|
|
527 |
// width += newv; newv = MIN_VALUE; width -= newv;
|
|
528 |
width += newv - Integer.MIN_VALUE;
|
|
529 |
// width may go negative if the right edge went past
|
|
530 |
// MIN_VALUE, but it cannot overflow since it cannot
|
|
531 |
// have moved more than MIN_VALUE and any non-negative
|
|
532 |
// number + MIN_VALUE does not overflow.
|
|
533 |
}
|
|
534 |
newv = Integer.MIN_VALUE;
|
|
535 |
}
|
|
536 |
} else {
|
|
537 |
// moving rightward (or staying still)
|
|
538 |
if (newv < oldv) {
|
|
539 |
// positive overflow
|
|
540 |
if (width >= 0) {
|
|
541 |
// Conceptually the same as:
|
|
542 |
// width += newv; newv = MAX_VALUE; width -= newv;
|
|
543 |
width += newv - Integer.MAX_VALUE;
|
|
544 |
// With large widths and large displacements
|
|
545 |
// we may overflow so we need to check it.
|
|
546 |
if (width < 0) width = Integer.MAX_VALUE;
|
|
547 |
}
|
|
548 |
newv = Integer.MAX_VALUE;
|
|
549 |
}
|
|
550 |
}
|
|
551 |
this.x = newv;
|
|
552 |
|
|
553 |
oldv = this.y;
|
|
554 |
newv = oldv + dy;
|
|
555 |
if (dy < 0) {
|
|
556 |
// moving upward
|
|
557 |
if (newv > oldv) {
|
|
558 |
// negative overflow
|
|
559 |
if (height >= 0) {
|
|
560 |
height += newv - Integer.MIN_VALUE;
|
|
561 |
// See above comment about no overflow in this case
|
|
562 |
}
|
|
563 |
newv = Integer.MIN_VALUE;
|
|
564 |
}
|
|
565 |
} else {
|
|
566 |
// moving downward (or staying still)
|
|
567 |
if (newv < oldv) {
|
|
568 |
// positive overflow
|
|
569 |
if (height >= 0) {
|
|
570 |
height += newv - Integer.MAX_VALUE;
|
|
571 |
if (height < 0) height = Integer.MAX_VALUE;
|
|
572 |
}
|
|
573 |
newv = Integer.MAX_VALUE;
|
|
574 |
}
|
|
575 |
}
|
|
576 |
this.y = newv;
|
|
577 |
}
|
|
578 |
|
|
579 |
/**
|
|
580 |
* Gets the size of this <code>Rectangle</code>, represented by
|
|
581 |
* the returned <code>Dimension</code>.
|
|
582 |
* <p>
|
|
583 |
* This method is included for completeness, to parallel the
|
|
584 |
* <code>getSize</code> method of <code>Component</code>.
|
|
585 |
* @return a <code>Dimension</code>, representing the size of
|
|
586 |
* this <code>Rectangle</code>.
|
|
587 |
* @see java.awt.Component#getSize
|
|
588 |
* @see #setSize(Dimension)
|
|
589 |
* @see #setSize(int, int)
|
|
590 |
* @since 1.1
|
|
591 |
*/
|
|
592 |
public Dimension getSize() {
|
|
593 |
return new Dimension(width, height);
|
|
594 |
}
|
|
595 |
|
|
596 |
/**
|
|
597 |
* Sets the size of this <code>Rectangle</code> to match the
|
|
598 |
* specified <code>Dimension</code>.
|
|
599 |
* <p>
|
|
600 |
* This method is included for completeness, to parallel the
|
|
601 |
* <code>setSize</code> method of <code>Component</code>.
|
|
602 |
* @param d the new size for the <code>Dimension</code> object
|
|
603 |
* @see java.awt.Component#setSize(java.awt.Dimension)
|
|
604 |
* @see #getSize
|
|
605 |
* @since 1.1
|
|
606 |
*/
|
|
607 |
public void setSize(Dimension d) {
|
|
608 |
setSize(d.width, d.height);
|
|
609 |
}
|
|
610 |
|
|
611 |
/**
|
|
612 |
* Sets the size of this <code>Rectangle</code> to the specified
|
|
613 |
* width and height.
|
|
614 |
* <p>
|
|
615 |
* This method is included for completeness, to parallel the
|
|
616 |
* <code>setSize</code> method of <code>Component</code>.
|
|
617 |
* @param width the new width for this <code>Rectangle</code>
|
|
618 |
* @param height the new height for this <code>Rectangle</code>
|
|
619 |
* @see java.awt.Component#setSize(int, int)
|
|
620 |
* @see #getSize
|
|
621 |
* @since 1.1
|
|
622 |
*/
|
|
623 |
public void setSize(int width, int height) {
|
|
624 |
resize(width, height);
|
|
625 |
}
|
|
626 |
|
|
627 |
/**
|
|
628 |
* Sets the size of this <code>Rectangle</code> to the specified
|
|
629 |
* width and height.
|
|
630 |
* <p>
|
|
631 |
* @param width the new width for this <code>Rectangle</code>
|
|
632 |
* @param height the new height for this <code>Rectangle</code>
|
|
633 |
* @deprecated As of JDK version 1.1,
|
|
634 |
* replaced by <code>setSize(int, int)</code>.
|
|
635 |
*/
|
|
636 |
@Deprecated
|
|
637 |
public void resize(int width, int height) {
|
|
638 |
this.width = width;
|
|
639 |
this.height = height;
|
|
640 |
}
|
|
641 |
|
|
642 |
/**
|
|
643 |
* Checks whether or not this <code>Rectangle</code> contains the
|
|
644 |
* specified <code>Point</code>.
|
|
645 |
* @param p the <code>Point</code> to test
|
|
646 |
* @return <code>true</code> if the specified <code>Point</code>
|
|
647 |
* is inside this <code>Rectangle</code>;
|
|
648 |
* <code>false</code> otherwise.
|
|
649 |
* @since 1.1
|
|
650 |
*/
|
|
651 |
public boolean contains(Point p) {
|
|
652 |
return contains(p.x, p.y);
|
|
653 |
}
|
|
654 |
|
|
655 |
/**
|
|
656 |
* Checks whether or not this <code>Rectangle</code> contains the
|
|
657 |
* point at the specified location {@code (x,y)}.
|
|
658 |
*
|
|
659 |
* @param x the specified X coordinate
|
|
660 |
* @param y the specified Y coordinate
|
|
661 |
* @return <code>true</code> if the point
|
|
662 |
* {@code (x,y)} is inside this
|
|
663 |
* <code>Rectangle</code>;
|
|
664 |
* <code>false</code> otherwise.
|
|
665 |
* @since 1.1
|
|
666 |
*/
|
|
667 |
public boolean contains(int x, int y) {
|
|
668 |
return inside(x, y);
|
|
669 |
}
|
|
670 |
|
|
671 |
/**
|
|
672 |
* Checks whether or not this <code>Rectangle</code> entirely contains
|
|
673 |
* the specified <code>Rectangle</code>.
|
|
674 |
*
|
|
675 |
* @param r the specified <code>Rectangle</code>
|
|
676 |
* @return <code>true</code> if the <code>Rectangle</code>
|
|
677 |
* is contained entirely inside this <code>Rectangle</code>;
|
|
678 |
* <code>false</code> otherwise
|
|
679 |
* @since 1.2
|
|
680 |
*/
|
|
681 |
public boolean contains(Rectangle r) {
|
|
682 |
return contains(r.x, r.y, r.width, r.height);
|
|
683 |
}
|
|
684 |
|
|
685 |
/**
|
|
686 |
* Checks whether this <code>Rectangle</code> entirely contains
|
|
687 |
* the <code>Rectangle</code>
|
|
688 |
* at the specified location {@code (X,Y)} with the
|
|
689 |
* specified dimensions {@code (W,H)}.
|
|
690 |
* @param X the specified X coordinate
|
|
691 |
* @param Y the specified Y coordinate
|
|
692 |
* @param W the width of the <code>Rectangle</code>
|
|
693 |
* @param H the height of the <code>Rectangle</code>
|
|
694 |
* @return <code>true</code> if the <code>Rectangle</code> specified by
|
|
695 |
* {@code (X, Y, W, H)}
|
|
696 |
* is entirely enclosed inside this <code>Rectangle</code>;
|
|
697 |
* <code>false</code> otherwise.
|
|
698 |
* @since 1.1
|
|
699 |
*/
|
|
700 |
public boolean contains(int X, int Y, int W, int H) {
|
|
701 |
int w = this.width;
|
|
702 |
int h = this.height;
|
|
703 |
if ((w | h | W | H) < 0) {
|
|
704 |
// At least one of the dimensions is negative...
|
|
705 |
return false;
|
|
706 |
}
|
|
707 |
// Note: if any dimension is zero, tests below must return false...
|
|
708 |
int x = this.x;
|
|
709 |
int y = this.y;
|
|
710 |
if (X < x || Y < y) {
|
|
711 |
return false;
|
|
712 |
}
|
|
713 |
w += x;
|
|
714 |
W += X;
|
|
715 |
if (W <= X) {
|
|
716 |
// X+W overflowed or W was zero, return false if...
|
|
717 |
// either original w or W was zero or
|
|
718 |
// x+w did not overflow or
|
|
719 |
// the overflowed x+w is smaller than the overflowed X+W
|
|
720 |
if (w >= x || W > w) return false;
|
|
721 |
} else {
|
|
722 |
// X+W did not overflow and W was not zero, return false if...
|
|
723 |
// original w was zero or
|
|
724 |
// x+w did not overflow and x+w is smaller than X+W
|
|
725 |
if (w >= x && W > w) return false;
|
|
726 |
}
|
|
727 |
h += y;
|
|
728 |
H += Y;
|
|
729 |
if (H <= Y) {
|
|
730 |
if (h >= y || H > h) return false;
|
|
731 |
} else {
|
|
732 |
if (h >= y && H > h) return false;
|
|
733 |
}
|
|
734 |
return true;
|
|
735 |
}
|
|
736 |
|
|
737 |
/**
|
|
738 |
* Checks whether or not this <code>Rectangle</code> contains the
|
|
739 |
* point at the specified location {@code (X,Y)}.
|
|
740 |
*
|
|
741 |
* @param X the specified X coordinate
|
|
742 |
* @param Y the specified Y coordinate
|
|
743 |
* @return <code>true</code> if the point
|
|
744 |
* {@code (X,Y)} is inside this
|
|
745 |
* <code>Rectangle</code>;
|
|
746 |
* <code>false</code> otherwise.
|
|
747 |
* @deprecated As of JDK version 1.1,
|
|
748 |
* replaced by <code>contains(int, int)</code>.
|
|
749 |
*/
|
|
750 |
@Deprecated
|
|
751 |
public boolean inside(int X, int Y) {
|
|
752 |
int w = this.width;
|
|
753 |
int h = this.height;
|
|
754 |
if ((w | h) < 0) {
|
|
755 |
// At least one of the dimensions is negative...
|
|
756 |
return false;
|
|
757 |
}
|
|
758 |
// Note: if either dimension is zero, tests below must return false...
|
|
759 |
int x = this.x;
|
|
760 |
int y = this.y;
|
|
761 |
if (X < x || Y < y) {
|
|
762 |
return false;
|
|
763 |
}
|
|
764 |
w += x;
|
|
765 |
h += y;
|
|
766 |
// overflow || intersect
|
|
767 |
return ((w < x || w > X) &&
|
|
768 |
(h < y || h > Y));
|
|
769 |
}
|
|
770 |
|
|
771 |
/**
|
|
772 |
* Determines whether or not this <code>Rectangle</code> and the specified
|
|
773 |
* <code>Rectangle</code> intersect. Two rectangles intersect if
|
|
774 |
* their intersection is nonempty.
|
|
775 |
*
|
|
776 |
* @param r the specified <code>Rectangle</code>
|
|
777 |
* @return <code>true</code> if the specified <code>Rectangle</code>
|
|
778 |
* and this <code>Rectangle</code> intersect;
|
|
779 |
* <code>false</code> otherwise.
|
|
780 |
*/
|
|
781 |
public boolean intersects(Rectangle r) {
|
|
782 |
int tw = this.width;
|
|
783 |
int th = this.height;
|
|
784 |
int rw = r.width;
|
|
785 |
int rh = r.height;
|
|
786 |
if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) {
|
|
787 |
return false;
|
|
788 |
}
|
|
789 |
int tx = this.x;
|
|
790 |
int ty = this.y;
|
|
791 |
int rx = r.x;
|
|
792 |
int ry = r.y;
|
|
793 |
rw += rx;
|
|
794 |
rh += ry;
|
|
795 |
tw += tx;
|
|
796 |
th += ty;
|
|
797 |
// overflow || intersect
|
|
798 |
return ((rw < rx || rw > tx) &&
|
|
799 |
(rh < ry || rh > ty) &&
|
|
800 |
(tw < tx || tw > rx) &&
|
|
801 |
(th < ty || th > ry));
|
|
802 |
}
|
|
803 |
|
|
804 |
/**
|
|
805 |
* Computes the intersection of this <code>Rectangle</code> with the
|
|
806 |
* specified <code>Rectangle</code>. Returns a new <code>Rectangle</code>
|
|
807 |
* that represents the intersection of the two rectangles.
|
|
808 |
* If the two rectangles do not intersect, the result will be
|
|
809 |
* an empty rectangle.
|
|
810 |
*
|
|
811 |
* @param r the specified <code>Rectangle</code>
|
|
812 |
* @return the largest <code>Rectangle</code> contained in both the
|
|
813 |
* specified <code>Rectangle</code> and in
|
|
814 |
* this <code>Rectangle</code>; or if the rectangles
|
|
815 |
* do not intersect, an empty rectangle.
|
|
816 |
*/
|
|
817 |
public Rectangle intersection(Rectangle r) {
|
|
818 |
int tx1 = this.x;
|
|
819 |
int ty1 = this.y;
|
|
820 |
int rx1 = r.x;
|
|
821 |
int ry1 = r.y;
|
|
822 |
long tx2 = tx1; tx2 += this.width;
|
|
823 |
long ty2 = ty1; ty2 += this.height;
|
|
824 |
long rx2 = rx1; rx2 += r.width;
|
|
825 |
long ry2 = ry1; ry2 += r.height;
|
|
826 |
if (tx1 < rx1) tx1 = rx1;
|
|
827 |
if (ty1 < ry1) ty1 = ry1;
|
|
828 |
if (tx2 > rx2) tx2 = rx2;
|
|
829 |
if (ty2 > ry2) ty2 = ry2;
|
|
830 |
tx2 -= tx1;
|
|
831 |
ty2 -= ty1;
|
|
832 |
// tx2,ty2 will never overflow (they will never be
|
|
833 |
// larger than the smallest of the two source w,h)
|
|
834 |
// they might underflow, though...
|
|
835 |
if (tx2 < Integer.MIN_VALUE) tx2 = Integer.MIN_VALUE;
|
|
836 |
if (ty2 < Integer.MIN_VALUE) ty2 = Integer.MIN_VALUE;
|
|
837 |
return new Rectangle(tx1, ty1, (int) tx2, (int) ty2);
|
|
838 |
}
|
|
839 |
|
|
840 |
/**
|
|
841 |
* Computes the union of this <code>Rectangle</code> with the
|
|
842 |
* specified <code>Rectangle</code>. Returns a new
|
|
843 |
* <code>Rectangle</code> that
|
|
844 |
* represents the union of the two rectangles.
|
|
845 |
* <p>
|
|
846 |
* If either {@code Rectangle} has any dimension less than zero
|
|
847 |
* the rules for <a href=#NonExistant>non-existant</a> rectangles
|
|
848 |
* apply.
|
|
849 |
* If only one has a dimension less than zero, then the result
|
|
850 |
* will be a copy of the other {@code Rectangle}.
|
|
851 |
* If both have dimension less than zero, then the result will
|
|
852 |
* have at least one dimension less than zero.
|
|
853 |
* <p>
|
|
854 |
* If the resulting {@code Rectangle} would have a dimension
|
|
855 |
* too large to be expressed as an {@code int}, the result
|
|
856 |
* will have a dimension of {@code Integer.MAX_VALUE} along
|
|
857 |
* that dimension.
|
|
858 |
* @param r the specified <code>Rectangle</code>
|
|
859 |
* @return the smallest <code>Rectangle</code> containing both
|
|
860 |
* the specified <code>Rectangle</code> and this
|
|
861 |
* <code>Rectangle</code>.
|
|
862 |
*/
|
|
863 |
public Rectangle union(Rectangle r) {
|
|
864 |
long tx2 = this.width;
|
|
865 |
long ty2 = this.height;
|
|
866 |
if ((tx2 | ty2) < 0) {
|
|
867 |
// This rectangle has negative dimensions...
|
|
868 |
// If r has non-negative dimensions then it is the answer.
|
|
869 |
// If r is non-existant (has a negative dimension), then both
|
|
870 |
// are non-existant and we can return any non-existant rectangle
|
|
871 |
// as an answer. Thus, returning r meets that criterion.
|
|
872 |
// Either way, r is our answer.
|
|
873 |
return new Rectangle(r);
|
|
874 |
}
|
|
875 |
long rx2 = r.width;
|
|
876 |
long ry2 = r.height;
|
|
877 |
if ((rx2 | ry2) < 0) {
|
|
878 |
return new Rectangle(this);
|
|
879 |
}
|
|
880 |
int tx1 = this.x;
|
|
881 |
int ty1 = this.y;
|
|
882 |
tx2 += tx1;
|
|
883 |
ty2 += ty1;
|
|
884 |
int rx1 = r.x;
|
|
885 |
int ry1 = r.y;
|
|
886 |
rx2 += rx1;
|
|
887 |
ry2 += ry1;
|
|
888 |
if (tx1 > rx1) tx1 = rx1;
|
|
889 |
if (ty1 > ry1) ty1 = ry1;
|
|
890 |
if (tx2 < rx2) tx2 = rx2;
|
|
891 |
if (ty2 < ry2) ty2 = ry2;
|
|
892 |
tx2 -= tx1;
|
|
893 |
ty2 -= ty1;
|
|
894 |
// tx2,ty2 will never underflow since both original rectangles
|
|
895 |
// were already proven to be non-empty
|
|
896 |
// they might overflow, though...
|
|
897 |
if (tx2 > Integer.MAX_VALUE) tx2 = Integer.MAX_VALUE;
|
|
898 |
if (ty2 > Integer.MAX_VALUE) ty2 = Integer.MAX_VALUE;
|
|
899 |
return new Rectangle(tx1, ty1, (int) tx2, (int) ty2);
|
|
900 |
}
|
|
901 |
|
|
902 |
/**
|
|
903 |
* Adds a point, specified by the integer arguments {@code newx,newy}
|
|
904 |
* to the bounds of this {@code Rectangle}.
|
|
905 |
* <p>
|
|
906 |
* If this {@code Rectangle} has any dimension less than zero,
|
|
907 |
* the rules for <a href=#NonExistant>non-existant</a>
|
|
908 |
* rectangles apply.
|
|
909 |
* In that case, the new bounds of this {@code Rectangle} will
|
|
910 |
* have a location equal to the specified coordinates and
|
|
911 |
* width and height equal to zero.
|
|
912 |
* <p>
|
|
913 |
* After adding a point, a call to <code>contains</code> with the
|
|
914 |
* added point as an argument does not necessarily return
|
|
915 |
* <code>true</code>. The <code>contains</code> method does not
|
|
916 |
* return <code>true</code> for points on the right or bottom
|
|
917 |
* edges of a <code>Rectangle</code>. Therefore, if the added point
|
|
918 |
* falls on the right or bottom edge of the enlarged
|
|
919 |
* <code>Rectangle</code>, <code>contains</code> returns
|
|
920 |
* <code>false</code> for that point.
|
|
921 |
* If the specified point must be contained within the new
|
|
922 |
* {@code Rectangle}, a 1x1 rectangle should be added instead:
|
|
923 |
* <pre>
|
|
924 |
* r.add(newx, newy, 1, 1);
|
|
925 |
* </pre>
|
|
926 |
* @param newx the X coordinate of the new point
|
|
927 |
* @param newy the Y coordinate of the new point
|
|
928 |
*/
|
|
929 |
public void add(int newx, int newy) {
|
|
930 |
if ((width | height) < 0) {
|
|
931 |
this.x = newx;
|
|
932 |
this.y = newy;
|
|
933 |
this.width = this.height = 0;
|
|
934 |
return;
|
|
935 |
}
|
|
936 |
int x1 = this.x;
|
|
937 |
int y1 = this.y;
|
|
938 |
long x2 = this.width;
|
|
939 |
long y2 = this.height;
|
|
940 |
x2 += x1;
|
|
941 |
y2 += y1;
|
|
942 |
if (x1 > newx) x1 = newx;
|
|
943 |
if (y1 > newy) y1 = newy;
|
|
944 |
if (x2 < newx) x2 = newx;
|
|
945 |
if (y2 < newy) y2 = newy;
|
|
946 |
x2 -= x1;
|
|
947 |
y2 -= y1;
|
|
948 |
if (x2 > Integer.MAX_VALUE) x2 = Integer.MAX_VALUE;
|
|
949 |
if (y2 > Integer.MAX_VALUE) y2 = Integer.MAX_VALUE;
|
|
950 |
reshape(x1, y1, (int) x2, (int) y2);
|
|
951 |
}
|
|
952 |
|
|
953 |
/**
|
|
954 |
* Adds the specified {@code Point} to the bounds of this
|
|
955 |
* {@code Rectangle}.
|
|
956 |
* <p>
|
|
957 |
* If this {@code Rectangle} has any dimension less than zero,
|
|
958 |
* the rules for <a href=#NonExistant>non-existant</a>
|
|
959 |
* rectangles apply.
|
|
960 |
* In that case, the new bounds of this {@code Rectangle} will
|
|
961 |
* have a location equal to the coordinates of the specified
|
|
962 |
* {@code Point} and width and height equal to zero.
|
|
963 |
* <p>
|
|
964 |
* After adding a <code>Point</code>, a call to <code>contains</code>
|
|
965 |
* with the added <code>Point</code> as an argument does not
|
|
966 |
* necessarily return <code>true</code>. The <code>contains</code>
|
|
967 |
* method does not return <code>true</code> for points on the right
|
|
968 |
* or bottom edges of a <code>Rectangle</code>. Therefore if the added
|
|
969 |
* <code>Point</code> falls on the right or bottom edge of the
|
|
970 |
* enlarged <code>Rectangle</code>, <code>contains</code> returns
|
|
971 |
* <code>false</code> for that <code>Point</code>.
|
|
972 |
* If the specified point must be contained within the new
|
|
973 |
* {@code Rectangle}, a 1x1 rectangle should be added instead:
|
|
974 |
* <pre>
|
|
975 |
* r.add(pt.x, pt.y, 1, 1);
|
|
976 |
* </pre>
|
|
977 |
* @param pt the new <code>Point</code> to add to this
|
|
978 |
* <code>Rectangle</code>
|
|
979 |
*/
|
|
980 |
public void add(Point pt) {
|
|
981 |
add(pt.x, pt.y);
|
|
982 |
}
|
|
983 |
|
|
984 |
/**
|
|
985 |
* Adds a <code>Rectangle</code> to this <code>Rectangle</code>.
|
|
986 |
* The resulting <code>Rectangle</code> is the union of the two
|
|
987 |
* rectangles.
|
|
988 |
* <p>
|
|
989 |
* If either {@code Rectangle} has any dimension less than 0, the
|
|
990 |
* result will have the dimensions of the other {@code Rectangle}.
|
|
991 |
* If both {@code Rectangle}s have at least one dimension less
|
|
992 |
* than 0, the result will have at least one dimension less than 0.
|
|
993 |
* <p>
|
|
994 |
* If either {@code Rectangle} has one or both dimensions equal
|
|
995 |
* to 0, the result along those axes with 0 dimensions will be
|
|
996 |
* equivalent to the results obtained by adding the corresponding
|
|
997 |
* origin coordinate to the result rectangle along that axis,
|
|
998 |
* similar to the operation of the {@link #add(Point)} method,
|
|
999 |
* but contribute no further dimension beyond that.
|
|
1000 |
* <p>
|
|
1001 |
* If the resulting {@code Rectangle} would have a dimension
|
|
1002 |
* too large to be expressed as an {@code int}, the result
|
|
1003 |
* will have a dimension of {@code Integer.MAX_VALUE} along
|
|
1004 |
* that dimension.
|
|
1005 |
* @param r the specified <code>Rectangle</code>
|
|
1006 |
*/
|
|
1007 |
public void add(Rectangle r) {
|
|
1008 |
long tx2 = this.width;
|
|
1009 |
long ty2 = this.height;
|
|
1010 |
if ((tx2 | ty2) < 0) {
|
|
1011 |
reshape(r.x, r.y, r.width, r.height);
|
|
1012 |
}
|
|
1013 |
long rx2 = r.width;
|
|
1014 |
long ry2 = r.height;
|
|
1015 |
if ((rx2 | ry2) < 0) {
|
|
1016 |
return;
|
|
1017 |
}
|
|
1018 |
int tx1 = this.x;
|
|
1019 |
int ty1 = this.y;
|
|
1020 |
tx2 += tx1;
|
|
1021 |
ty2 += ty1;
|
|
1022 |
int rx1 = r.x;
|
|
1023 |
int ry1 = r.y;
|
|
1024 |
rx2 += rx1;
|
|
1025 |
ry2 += ry1;
|
|
1026 |
if (tx1 > rx1) tx1 = rx1;
|
|
1027 |
if (ty1 > ry1) ty1 = ry1;
|
|
1028 |
if (tx2 < rx2) tx2 = rx2;
|
|
1029 |
if (ty2 < ry2) ty2 = ry2;
|
|
1030 |
tx2 -= tx1;
|
|
1031 |
ty2 -= ty1;
|
|
1032 |
// tx2,ty2 will never underflow since both original
|
|
1033 |
// rectangles were non-empty
|
|
1034 |
// they might overflow, though...
|
|
1035 |
if (tx2 > Integer.MAX_VALUE) tx2 = Integer.MAX_VALUE;
|
|
1036 |
if (ty2 > Integer.MAX_VALUE) ty2 = Integer.MAX_VALUE;
|
|
1037 |
reshape(tx1, ty1, (int) tx2, (int) ty2);
|
|
1038 |
}
|
|
1039 |
|
|
1040 |
/**
|
|
1041 |
* Resizes the <code>Rectangle</code> both horizontally and vertically.
|
|
1042 |
* <p>
|
|
1043 |
* This method modifies the <code>Rectangle</code> so that it is
|
|
1044 |
* <code>h</code> units larger on both the left and right side,
|
|
1045 |
* and <code>v</code> units larger at both the top and bottom.
|
|
1046 |
* <p>
|
|
1047 |
* The new <code>Rectangle</code> has {@code (x - h, y - v)}
|
|
1048 |
* as its upper-left corner,
|
|
1049 |
* width of {@code (width + 2h)},
|
|
1050 |
* and a height of {@code (height + 2v)}.
|
|
1051 |
* <p>
|
|
1052 |
* If negative values are supplied for <code>h</code> and
|
|
1053 |
* <code>v</code>, the size of the <code>Rectangle</code>
|
|
1054 |
* decreases accordingly.
|
|
1055 |
* The {@code grow} method will check for integer overflow
|
|
1056 |
* and underflow, but does not check whether the resulting
|
|
1057 |
* values of {@code width} and {@code height} grow
|
|
1058 |
* from negative to non-negative or shrink from non-negative
|
|
1059 |
* to negative.
|
|
1060 |
* @param h the horizontal expansion
|
|
1061 |
* @param v the vertical expansion
|
|
1062 |
*/
|
|
1063 |
public void grow(int h, int v) {
|
|
1064 |
long x0 = this.x;
|
|
1065 |
long y0 = this.y;
|
|
1066 |
long x1 = this.width;
|
|
1067 |
long y1 = this.height;
|
|
1068 |
x1 += x0;
|
|
1069 |
y1 += y0;
|
|
1070 |
|
|
1071 |
x0 -= h;
|
|
1072 |
y0 -= v;
|
|
1073 |
x1 += h;
|
|
1074 |
y1 += v;
|
|
1075 |
|
|
1076 |
if (x1 < x0) {
|
|
1077 |
// Non-existant in X direction
|
|
1078 |
// Final width must remain negative so subtract x0 before
|
|
1079 |
// it is clipped so that we avoid the risk that the clipping
|
|
1080 |
// of x0 will reverse the ordering of x0 and x1.
|
|
1081 |
x1 -= x0;
|
|
1082 |
if (x1 < Integer.MIN_VALUE) x1 = Integer.MIN_VALUE;
|
|
1083 |
if (x0 < Integer.MIN_VALUE) x0 = Integer.MIN_VALUE;
|
|
1084 |
else if (x0 > Integer.MAX_VALUE) x0 = Integer.MAX_VALUE;
|
|
1085 |
} else { // (x1 >= x0)
|
|
1086 |
// Clip x0 before we subtract it from x1 in case the clipping
|
|
1087 |
// affects the representable area of the rectangle.
|
|
1088 |
if (x0 < Integer.MIN_VALUE) x0 = Integer.MIN_VALUE;
|
|
1089 |
else if (x0 > Integer.MAX_VALUE) x0 = Integer.MAX_VALUE;
|
|
1090 |
x1 -= x0;
|
|
1091 |
// The only way x1 can be negative now is if we clipped
|
|
1092 |
// x0 against MIN and x1 is less than MIN - in which case
|
|
1093 |
// we want to leave the width negative since the result
|
|
1094 |
// did not intersect the representable area.
|
|
1095 |
if (x1 < Integer.MIN_VALUE) x1 = Integer.MIN_VALUE;
|
|
1096 |
else if (x1 > Integer.MAX_VALUE) x1 = Integer.MAX_VALUE;
|
|
1097 |
}
|
|
1098 |
|
|
1099 |
if (y1 < y0) {
|
|
1100 |
// Non-existant in Y direction
|
|
1101 |
y1 -= y0;
|
|
1102 |
if (y1 < Integer.MIN_VALUE) y1 = Integer.MIN_VALUE;
|
|
1103 |
if (y0 < Integer.MIN_VALUE) y0 = Integer.MIN_VALUE;
|
|
1104 |
else if (y0 > Integer.MAX_VALUE) y0 = Integer.MAX_VALUE;
|
|
1105 |
} else { // (y1 >= y0)
|
|
1106 |
if (y0 < Integer.MIN_VALUE) y0 = Integer.MIN_VALUE;
|
|
1107 |
else if (y0 > Integer.MAX_VALUE) y0 = Integer.MAX_VALUE;
|
|
1108 |
y1 -= y0;
|
|
1109 |
if (y1 < Integer.MIN_VALUE) y1 = Integer.MIN_VALUE;
|
|
1110 |
else if (y1 > Integer.MAX_VALUE) y1 = Integer.MAX_VALUE;
|
|
1111 |
}
|
|
1112 |
|
|
1113 |
reshape((int) x0, (int) y0, (int) x1, (int) y1);
|
|
1114 |
}
|
|
1115 |
|
|
1116 |
/**
|
|
1117 |
* {@inheritDoc}
|
|
1118 |
* @since 1.2
|
|
1119 |
*/
|
|
1120 |
public boolean isEmpty() {
|
|
1121 |
return (width <= 0) || (height <= 0);
|
|
1122 |
}
|
|
1123 |
|
|
1124 |
/**
|
|
1125 |
* {@inheritDoc}
|
|
1126 |
* @since 1.2
|
|
1127 |
*/
|
|
1128 |
public int outcode(double x, double y) {
|
|
1129 |
/*
|
|
1130 |
* Note on casts to double below. If the arithmetic of
|
|
1131 |
* x+w or y+h is done in int, then we may get integer
|
|
1132 |
* overflow. By converting to double before the addition
|
|
1133 |
* we force the addition to be carried out in double to
|
|
1134 |
* avoid overflow in the comparison.
|
|
1135 |
*
|
|
1136 |
* See bug 4320890 for problems that this can cause.
|
|
1137 |
*/
|
|
1138 |
int out = 0;
|
|
1139 |
if (this.width <= 0) {
|
|
1140 |
out |= OUT_LEFT | OUT_RIGHT;
|
|
1141 |
} else if (x < this.x) {
|
|
1142 |
out |= OUT_LEFT;
|
|
1143 |
} else if (x > this.x + (double) this.width) {
|
|
1144 |
out |= OUT_RIGHT;
|
|
1145 |
}
|
|
1146 |
if (this.height <= 0) {
|
|
1147 |
out |= OUT_TOP | OUT_BOTTOM;
|
|
1148 |
} else if (y < this.y) {
|
|
1149 |
out |= OUT_TOP;
|
|
1150 |
} else if (y > this.y + (double) this.height) {
|
|
1151 |
out |= OUT_BOTTOM;
|
|
1152 |
}
|
|
1153 |
return out;
|
|
1154 |
}
|
|
1155 |
|
|
1156 |
/**
|
|
1157 |
* {@inheritDoc}
|
|
1158 |
* @since 1.2
|
|
1159 |
*/
|
|
1160 |
public Rectangle2D createIntersection(Rectangle2D r) {
|
|
1161 |
if (r instanceof Rectangle) {
|
|
1162 |
return intersection((Rectangle) r);
|
|
1163 |
}
|
|
1164 |
Rectangle2D dest = new Rectangle2D.Double();
|
|
1165 |
Rectangle2D.intersect(this, r, dest);
|
|
1166 |
return dest;
|
|
1167 |
}
|
|
1168 |
|
|
1169 |
/**
|
|
1170 |
* {@inheritDoc}
|
|
1171 |
* @since 1.2
|
|
1172 |
*/
|
|
1173 |
public Rectangle2D createUnion(Rectangle2D r) {
|
|
1174 |
if (r instanceof Rectangle) {
|
|
1175 |
return union((Rectangle) r);
|
|
1176 |
}
|
|
1177 |
Rectangle2D dest = new Rectangle2D.Double();
|
|
1178 |
Rectangle2D.union(this, r, dest);
|
|
1179 |
return dest;
|
|
1180 |
}
|
|
1181 |
|
|
1182 |
/**
|
|
1183 |
* Checks whether two rectangles are equal.
|
|
1184 |
* <p>
|
|
1185 |
* The result is <code>true</code> if and only if the argument is not
|
|
1186 |
* <code>null</code> and is a <code>Rectangle</code> object that has the
|
|
1187 |
* same upper-left corner, width, and height as
|
|
1188 |
* this <code>Rectangle</code>.
|
|
1189 |
* @param obj the <code>Object</code> to compare with
|
|
1190 |
* this <code>Rectangle</code>
|
|
1191 |
* @return <code>true</code> if the objects are equal;
|
|
1192 |
* <code>false</code> otherwise.
|
|
1193 |
*/
|
|
1194 |
public boolean equals(Object obj) {
|
|
1195 |
if (obj instanceof Rectangle) {
|
|
1196 |
Rectangle r = (Rectangle)obj;
|
|
1197 |
return ((x == r.x) &&
|
|
1198 |
(y == r.y) &&
|
|
1199 |
(width == r.width) &&
|
|
1200 |
(height == r.height));
|
|
1201 |
}
|
|
1202 |
return super.equals(obj);
|
|
1203 |
}
|
|
1204 |
|
|
1205 |
/**
|
|
1206 |
* Returns a <code>String</code> representing this
|
|
1207 |
* <code>Rectangle</code> and its values.
|
|
1208 |
* @return a <code>String</code> representing this
|
|
1209 |
* <code>Rectangle</code> object's coordinate and size values.
|
|
1210 |
*/
|
|
1211 |
public String toString() {
|
|
1212 |
return getClass().getName() + "[x=" + x + ",y=" + y + ",width=" + width + ",height=" + height + "]";
|
|
1213 |
}
|
|
1214 |
}
|