KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > Rectangle


1 /*
2  * @(#)Rectangle.java 1.70 04/05/18
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt;
9
10 import java.awt.geom.Rectangle2D JavaDoc;
11
12 /**
13  * A <code>Rectangle</code> specifies an area in a coordinate space that is
14  * enclosed by the <code>Rectangle</code> object's top-left point
15  * (<i>x</i>,&nbsp;<i>y</i>)
16  * in the coordinate space, its width, and its height.
17  * <p>
18  * A <code>Rectangle</code> object's <code>width</code> and
19  * <code>height</code> are <code>public</code> fields. The constructors
20  * that create a <code>Rectangle</code>, and the methods that can modify
21  * one, do not prevent setting a negative value for width or height.
22  * <p>
23  * A <code>Rectangle</code> whose width or height is negative is considered
24  * empty. If the <code>Rectangle</code> is empty, then the
25  * <code>isEmpty</code> method returns <code>true</code>. No point can be
26  * contained by or inside an empty <code>Rectangle</code>. The
27  * values of <code>width</code> and <code>height</code>, however, are still
28  * valid. An empty <code>Rectangle</code> still has a location in the
29  * coordinate space, and methods that change its size or location remain
30  * valid. The behavior of methods that operate on more than one
31  * <code>Rectangle</code> is undefined if any of the participating
32  * <code>Rectangle</code> objects has a negative
33  * <code>width</code> or <code>height</code>. These methods include
34  * <code>intersects</code>, <code>intersection</code>, and
35  * <code>union</code>.
36  *
37  * @version 1.70, 05/18/04
38  * @author Sami Shaio
39  * @since JDK1.0
40  */

41 public class Rectangle extends Rectangle2D JavaDoc
42     implements Shape JavaDoc, java.io.Serializable JavaDoc
43 {
44
45     /**
46      * The <i>x</i> coordinate of the <code>Rectangle</code>.
47      *
48      * @serial
49      * @see #setLocation(int, int)
50      * @see #getLocation()
51      */

52     public int x;
53
54     /**
55      * The <i>y</i> coordinate of the <code>Rectangle</code>.
56      *
57      * @serial
58      * @see #setLocation(int, int)
59      * @see #getLocation()
60      */

61     public int y;
62
63     /**
64      * The width of the <code>Rectangle</code>.
65      * @serial
66      * @see #setSize(int, int)
67      * @see #getSize()
68      * @since JDK1.0.
69      */

70     public int width;
71
72     /**
73      * The height of the <code>Rectangle</code>.
74      *
75      * @serial
76      * @see #setSize(int, int)
77      * @see #getSize()
78      */

79     public int height;
80
81     /*
82      * JDK 1.1 serialVersionUID
83      */

84      private static final long serialVersionUID = -4345857070255674764L;
85        
86     /**
87      * Initialize JNI field and method IDs
88      */

89     private static native void initIDs();
90     
91     static {
92         /* ensure that the necessary native libraries are loaded */
93         Toolkit.loadLibraries();
94         if (!GraphicsEnvironment.isHeadless()) {
95             initIDs();
96         }
97     }
98
99     /**
100      * Constructs a new <code>Rectangle</code> whose top-left corner
101      * is at (0,&nbsp;0) in the coordinate space, and whose width and
102      * height are both zero.
103      */

104     public Rectangle() {
105         this(0, 0, 0, 0);
106     }
107
108     /**
109      * Constructs a new <code>Rectangle</code>, initialized to match
110      * the values of the specified <code>Rectangle</code>.
111      * @param r the <code>Rectangle</code> from which to copy initial values
112      * to a newly constructed <code>Rectangle</code>
113      * @since JDK1.1
114      */

115     public Rectangle(Rectangle JavaDoc r) {
116         this(r.x, r.y, r.width, r.height);
117     }
118
119     /**
120      * Constructs a new <code>Rectangle</code> whose top-left corner is
121      * specified as
122      * (<code>x</code>,&nbsp;<code>y</code>) and whose width and height
123      * are specified by the arguments of the same name.
124      * @param x the specified x coordinate
125      * @param y the specified y coordinate
126      * @param width the width of the <code>Rectangle</code>
127      * @param height the height of the <code>Rectangle</code>
128      */

129     public Rectangle(int x, int y, int width, int height) {
130     this.x = x;
131     this.y = y;
132     this.width = width;
133     this.height = height;
134     }
135
136     /**
137      * Constructs a new <code>Rectangle</code> whose top-left corner
138      * is at (0,&nbsp;0) in the coordinate space, and whose width and
139      * height are specified by the arguments of the same name.
140      * @param width the width of the <code>Rectangle</code>
141      * @param height the height of the <code>Rectangle</code>
142      */

143     public Rectangle(int width, int height) {
144     this(0, 0, width, height);
145     }
146
147     /**
148      * Constructs a new <code>Rectangle</code> whose top-left corner is
149      * specified by the {@link Point} argument, and
150      * whose width and height are specified by the
151      * {@link Dimension} argument.
152      * @param p a <code>Point</code> that is the top-left corner of
153      * the <code>Rectangle</code>
154      * @param d a <code>Dimension</code>, representing the
155      * width and height of the <code>Rectangle</code>
156      */

157     public Rectangle(Point JavaDoc p, Dimension JavaDoc d) {
158     this(p.x, p.y, d.width, d.height);
159     }
160     
161     /**
162      * Constructs a new <code>Rectangle</code> whose top-left corner is the
163      * specified <code>Point</code>, and whose width and height are both zero.
164      * @param p a <code>Point</code> that is the top left corner
165      * of the <code>Rectangle</code>
166      */

167     public Rectangle(Point JavaDoc p) {
168     this(p.x, p.y, 0, 0);
169     }
170     
171     /**
172      * Constructs a new <code>Rectangle</code> whose top left corner is
173      * (0,&nbsp;0) and whose width and height are specified
174      * by the <code>Dimension</code> argument.
175      * @param d a <code>Dimension</code>, specifying width and height
176      */

177     public Rectangle(Dimension JavaDoc d) {
178     this(0, 0, d.width, d.height);
179     }
180
181     /**
182      * Returns the X coordinate of the bounding <code>Rectangle</code> in
183      * <code>double</code> precision.
184      * @return the x coordinate of the bounding <code>Rectangle</code>.
185      */

186     public double getX() {
187     return x;
188     }
189
190     /**
191      * Returns the Y coordinate of the bounding <code>Rectangle</code> in
192      * <code>double</code> precision.
193      * @return the y coordinate of the bounding <code>Rectangle</code>.
194      */

195     public double getY() {
196     return y;
197     }
198
199     /**
200      * Returns the width of the bounding <code>Rectangle</code> in
201      * <code>double</code> precision.
202      * @return the width of the bounding <code>Rectangle</code>.
203      */

204     public double getWidth() {
205     return width;
206     }
207
208     /**
209      * Returns the height of the bounding <code>Rectangle</code> in
210      * <code>double</code> precision.
211      * @return the height of the bounding <code>Rectangle</code>.
212      */

213     public double getHeight() {
214     return height;
215     }
216
217     /**
218      * Gets the bounding <code>Rectangle</code> of this <code>Rectangle</code>.
219      * <p>
220      * This method is included for completeness, to parallel the
221      * <code>getBounds</code> method of
222      * {@link Component}.
223      * @return a new <code>Rectangle</code>, equal to the
224      * bounding <code>Rectangle</code> for this <code>Rectangle</code>.
225      * @see java.awt.Component#getBounds
226      * @see #setBounds(Rectangle)
227      * @see #setBounds(int, int, int, int)
228      * @since JDK1.1
229      */

230     public Rectangle JavaDoc getBounds() {
231     return new Rectangle JavaDoc(x, y, width, height);
232     }
233
234     /**
235      * Return the high precision bounding box of this rectangle.
236      * @since 1.2
237      */

238     public Rectangle2D JavaDoc getBounds2D() {
239     return new Rectangle JavaDoc(x, y, width, height);
240     }
241
242     /**
243      * Sets the bounding <code>Rectangle</code> of this <code>Rectangle</code>
244      * to match the specified <code>Rectangle</code>.
245      * <p>
246      * This method is included for completeness, to parallel the
247      * <code>setBounds</code> method of <code>Component</code>.
248      * @param r the specified <code>Rectangle</code>
249      * @see #getBounds
250      * @see java.awt.Component#setBounds(java.awt.Rectangle)
251      * @since JDK1.1
252      */

253     public void setBounds(Rectangle JavaDoc r) {
254     setBounds(r.x, r.y, r.width, r.height);
255     }
256
257     /**
258      * Sets the bounding <code>Rectangle</code> of this
259      * <code>Rectangle</code> to the specified
260      * <code>x</code>, <code>y</code>, <code>width</code>,
261      * and <code>height</code>.
262      * <p>
263      * This method is included for completeness, to parallel the
264      * <code>setBounds</code> method of <code>Component</code>.
265      * @param x the new x coordinate for the top-left
266      * corner of this <code>Rectangle</code>
267      * @param y the new y coordinate for the top-left
268      * corner of this <code>Rectangle</code>
269      * @param width the new width for this <code>Rectangle</code>
270      * @param height the new height for this <code>Rectangle</code>
271      * @see #getBounds
272      * @see java.awt.Component#setBounds(int, int, int, int)
273      * @since JDK1.1
274      */

275     public void setBounds(int x, int y, int width, int height) {
276         reshape(x, y, width, height);
277     }
278
279     /**
280      * Sets the bounds of this <code>Rectangle</code> to the specified
281      * <code>x</code>, <code>y</code>, <code>width</code>,
282      * and <code>height</code>.
283      * This method is included for completeness, to parallel the
284      * <code>setBounds</code> method of <code>Component</code>.
285      * @param x the x coordinate of the upper-left corner of
286      * the specified rectangle
287      * @param y the y coordinate of the upper-left corner of
288      * the specified rectangle
289      * @param width the new width for the <code>Dimension</code> object
290      * @param height the new height for the <code>Dimension</code> object
291      */

292     public void setRect(double x, double y, double width, double height) {
293     int x0 = (int) Math.floor(x);
294     int y0 = (int) Math.floor(y);
295     int x1 = (int) Math.ceil(x+width);
296     int y1 = (int) Math.ceil(y+height);
297     setBounds(x0, y0, x1-x0, y1-y0);
298     }
299
300     /**
301      * Sets the bounding <code>Rectangle</code> of this
302      * <code>Rectangle</code> to the specified
303      * <code>x</code>, <code>y</code>, <code>width</code>,
304      * and <code>height</code>.
305      * <p>
306      * @param x the new x coordinate for the top-left
307      * corner of this <code>Rectangle</code>
308      * @param y the new y coordinate for the top-left
309      * corner of this <code>Rectangle</code>
310      * @param width the new width for this <code>Rectangle</code>
311      * @param height the new height for this <code>Rectangle</code>
312      * @deprecated As of JDK version 1.1,
313      * replaced by <code>setBounds(int, int, int, int)</code>.
314      */

315     @Deprecated JavaDoc
316     public void reshape(int x, int y, int width, int height) {
317     this.x = x;
318     this.y = y;
319     this.width = width;
320     this.height = height;
321     }
322
323     /**
324      * Returns the location of this <code>Rectangle</code>.
325      * <p>
326      * This method is included for completeness, to parallel the
327      * <code>getLocation</code> method of <code>Component</code>.
328      * @return the <code>Point</code> that is the top-left corner of
329      * this <code>Rectangle</code>.
330      * @see java.awt.Component#getLocation
331      * @see #setLocation(Point)
332      * @see #setLocation(int, int)
333      * @since JDK1.1
334      */

335     public Point JavaDoc getLocation() {
336     return new Point JavaDoc(x, y);
337     }
338
339     /**
340      * Moves this <code>Rectangle</code> to the specified location.
341      * <p>
342      * This method is included for completeness, to parallel the
343      * <code>setLocation</code> method of <code>Component</code>.
344      * @param p the <code>Point</code> specifying the new location
345      * for this <code>Rectangle</code>
346      * @see java.awt.Component#setLocation(java.awt.Point)
347      * @see #getLocation
348      * @since JDK1.1
349      */

350     public void setLocation(Point JavaDoc p) {
351     setLocation(p.x, p.y);
352     }
353
354     /**
355      * Moves this <code>Rectangle</code> to the specified location.
356      * <p>
357      * This method is included for completeness, to parallel the
358      * <code>setLocation</code> method of <code>Component</code>.
359      * @param x the x coordinate of the new location
360      * @param y the y coordinate of the new location
361      * @see #getLocation
362      * @see java.awt.Component#setLocation(int, int)
363      * @since JDK1.1
364      */

365     public void setLocation(int x, int y) {
366     move(x, y);
367     }
368
369     /**
370      * Moves this <code>Rectangle</code> to the specified location.
371      * <p>
372      * @param x the x coordinate of the new location
373      * @param y the y coordinate of the new location
374      * @deprecated As of JDK version 1.1,
375      * replaced by <code>setLocation(int, int)</code>.
376      */

377     @Deprecated JavaDoc
378     public void move(int x, int y) {
379     this.x = x;
380     this.y = y;
381     }
382
383     /**
384      * Translates this <code>Rectangle</code> the indicated distance,
385      * to the right along the x coordinate axis, and
386      * downward along the y coordinate axis.
387      * @param x the distance to move this <code>Rectangle</code>
388      * along the x axis
389      * @param y the distance to move this <code>Rectangle</code>
390      * along the y axis
391      * @see java.awt.Rectangle#setLocation(int, int)
392      * @see java.awt.Rectangle#setLocation(java.awt.Point)
393      */

394     public void translate(int x, int y) {
395     this.x += x;
396     this.y += y;
397     }
398
399     /**
400      * Gets the size of this <code>Rectangle</code>, represented by
401      * the returned <code>Dimension</code>.
402      * <p>
403      * This method is included for completeness, to parallel the
404      * <code>getSize</code> method of <code>Component</code>.
405      * @return a <code>Dimension</code>, representing the size of
406      * this <code>Rectangle</code>.
407      * @see java.awt.Component#getSize
408      * @see #setSize(Dimension)
409      * @see #setSize(int, int)
410      * @since JDK1.1
411      */

412     public Dimension JavaDoc getSize() {
413     return new Dimension JavaDoc(width, height);
414     }
415
416     /**
417      * Sets the size of this <code>Rectangle</code> to match the
418      * specified <code>Dimension</code>.
419      * <p>
420      * This method is included for completeness, to parallel the
421      * <code>setSize</code> method of <code>Component</code>.
422      * @param d the new size for the <code>Dimension</code> object
423      * @see java.awt.Component#setSize(java.awt.Dimension)
424      * @see #getSize
425      * @since JDK1.1
426      */

427     public void setSize(Dimension JavaDoc d) {
428     setSize(d.width, d.height);
429     }
430
431     /**
432      * Sets the size of this <code>Rectangle</code> to the specified
433      * width and height.
434      * <p>
435      * This method is included for completeness, to parallel the
436      * <code>setSize</code> method of <code>Component</code>.
437      * @param width the new width for this <code>Rectangle</code>
438      * @param height the new height for this <code>Rectangle</code>
439      * @see java.awt.Component#setSize(int, int)
440      * @see #getSize
441      * @since JDK1.1
442      */

443     public void setSize(int width, int height) {
444         resize(width, height);
445     }
446
447     /**
448      * Sets the size of this <code>Rectangle</code> to the specified
449      * width and height.
450      * <p>
451      * @param width the new width for this <code>Rectangle</code>
452      * @param height the new height for this <code>Rectangle</code>
453      * @deprecated As of JDK version 1.1,
454      * replaced by <code>setSize(int, int)</code>.
455      */

456     @Deprecated JavaDoc
457     public void resize(int width, int height) {
458     this.width = width;
459     this.height = height;
460     }
461
462     /**
463      * Checks whether or not this <code>Rectangle</code> contains the
464      * specified <code>Point</code>.
465      * @param p the <code>Point</code> to test
466      * @return <code>true</code> if the <code>Point</code>
467      * (<i>x</i>,&nbsp;<i>y</i>) is inside this
468      * <code>Rectangle</code>;
469      * <code>false</code> otherwise.
470      * @since JDK1.1
471      */

472     public boolean contains(Point JavaDoc p) {
473     return contains(p.x, p.y);
474     }
475
476     /**
477      * Checks whether or not this <code>Rectangle</code> contains the
478      * point at the specified location
479      * (<i>x</i>,&nbsp;<i>y</i>).
480      * @param x the specified x coordinate
481      * @param y the specified y coordinate
482      * @return <code>true</code> if the point
483      * (<i>x</i>,&nbsp;<i>y</i>) is inside this
484      * <code>Rectangle</code>;
485      * <code>false</code> otherwise.
486      * @since JDK1.1
487      */

488     public boolean contains(int x, int y) {
489     return inside(x, y);
490     }
491
492     /**
493      * Checks whether or not this <code>Rectangle</code> entirely contains
494      * the specified <code>Rectangle</code>.
495      *
496      * @param r the specified <code>Rectangle</code>
497      * @return <code>true</code> if the <code>Rectangle</code>
498      * is contained entirely inside this <code>Rectangle</code>;
499      * <code>false</code> otherwise
500      * @since JDK1.2
501      */

502     public boolean contains(Rectangle JavaDoc r) {
503     return contains(r.x, r.y, r.width, r.height);
504     }
505
506     /**
507      * Checks whether this <code>Rectangle</code> entirely contains
508      * the <code>Rectangle</code>
509      * at the specified location (<i>X</i>,&nbsp;<i>Y</i>) with the
510      * specified dimensions (<i>W</i>,&nbsp;<i>H</i>).
511      * @param X the specified x coordinate
512      * @param Y the specified y coordinate
513      * @param W the width of the <code>Rectangle</code>
514      * @param H the height of the <code>Rectangle</code>
515      * @return <code>true</code> if the <code>Rectangle</code> specified by
516      * (<i>X</i>,&nbsp;<i>Y</i>,&nbsp;<i>W</i>,&nbsp;<i>H</i>)
517      * is entirely enclosed inside this <code>Rectangle</code>;
518      * <code>false</code> otherwise.
519      * @since JDK1.1
520      */

521     public boolean contains(int X, int Y, int W, int H) {
522     int w = this.width;
523     int h = this.height;
524     if ((w | h | W | H) < 0) {
525         // At least one of the dimensions is negative...
526
return false;
527     }
528     // Note: if any dimension is zero, tests below must return false...
529
int x = this.x;
530     int y = this.y;
531     if (X < x || Y < y) {
532         return false;
533     }
534     w += x;
535     W += X;
536     if (W <= X) {
537         // X+W overflowed or W was zero, return false if...
538
// either original w or W was zero or
539
// x+w did not overflow or
540
// the overflowed x+w is smaller than the overflowed X+W
541
if (w >= x || W > w) return false;
542     } else {
543         // X+W did not overflow and W was not zero, return false if...
544
// original w was zero or
545
// x+w did not overflow and x+w is smaller than X+W
546
if (w >= x && W > w) return false;
547     }
548     h += y;
549     H += Y;
550     if (H <= Y) {
551         if (h >= y || H > h) return false;
552     } else {
553         if (h >= y && H > h) return false;
554     }
555     return true;
556     }
557
558     /**
559      * Checks whether or not this <code>Rectangle</code> contains the
560      * point at the specified location
561      * (<i>X</i>,&nbsp;<i>Y</i>).
562      * @param X the specified x coordinate
563      * @param Y the specified y coordinate
564      * @return <code>true</code> if the point
565      * (<i>X</i>,&nbsp;<i>Y</i>) is inside this
566      * <code>Rectangle</code>;
567      * <code>false</code> otherwise.
568      * @deprecated As of JDK version 1.1,
569      * replaced by <code>contains(int, int)</code>.
570      */

571     @Deprecated JavaDoc
572     public boolean inside(int X, int Y) {
573     int w = this.width;
574     int h = this.height;
575     if ((w | h) < 0) {
576         // At least one of the dimensions is negative...
577
return false;
578     }
579     // Note: if either dimension is zero, tests below must return false...
580
int x = this.x;
581     int y = this.y;
582     if (X < x || Y < y) {
583         return false;
584     }
585     w += x;
586     h += y;
587     // overflow || intersect
588
return ((w < x || w > X) &&
589         (h < y || h > Y));
590     }
591
592     /**
593      * Determines whether or not this <code>Rectangle</code> and the specified
594      * <code>Rectangle</code> intersect. Two rectangles intersect if
595      * their intersection is nonempty.
596      *
597      * @param r the specified <code>Rectangle</code>
598      * @return <code>true</code> if the specified <code>Rectangle</code>
599      * and this <code>Rectangle</code> intersect;
600      * <code>false</code> otherwise.
601      */

602     public boolean intersects(Rectangle JavaDoc r) {
603     int tw = this.width;
604     int th = this.height;
605     int rw = r.width;
606     int rh = r.height;
607     if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) {
608         return false;
609     }
610     int tx = this.x;
611     int ty = this.y;
612     int rx = r.x;
613     int ry = r.y;
614     rw += rx;
615     rh += ry;
616     tw += tx;
617     th += ty;
618     // overflow || intersect
619
return ((rw < rx || rw > tx) &&
620         (rh < ry || rh > ty) &&
621         (tw < tx || tw > rx) &&
622         (th < ty || th > ry));
623     }
624
625     /**
626      * Computes the intersection of this <code>Rectangle</code> with the
627      * specified <code>Rectangle</code>. Returns a new <code>Rectangle</code>
628      * that represents the intersection of the two rectangles.
629      * If the two rectangles do not intersect, the result will be
630      * an empty rectangle.
631      *
632      * @param r the specified <code>Rectangle</code>
633      * @return the largest <code>Rectangle</code> contained in both the
634      * specified <code>Rectangle</code> and in
635      * this <code>Rectangle</code>; or if the rectangles
636      * do not intersect, an empty rectangle.
637      */

638     public Rectangle JavaDoc intersection(Rectangle JavaDoc r) {
639     int tx1 = this.x;
640     int ty1 = this.y;
641     int rx1 = r.x;
642     int ry1 = r.y;
643     long tx2 = tx1; tx2 += this.width;
644     long ty2 = ty1; ty2 += this.height;
645     long rx2 = rx1; rx2 += r.width;
646     long ry2 = ry1; ry2 += r.height;
647     if (tx1 < rx1) tx1 = rx1;
648     if (ty1 < ry1) ty1 = ry1;
649     if (tx2 > rx2) tx2 = rx2;
650     if (ty2 > ry2) ty2 = ry2;
651     tx2 -= tx1;
652     ty2 -= ty1;
653     // tx2,ty2 will never overflow (they will never be
654
// larger than the smallest of the two source w,h)
655
// they might underflow, though...
656
if (tx2 < Integer.MIN_VALUE) tx2 = Integer.MIN_VALUE;
657     if (ty2 < Integer.MIN_VALUE) ty2 = Integer.MIN_VALUE;
658     return new Rectangle JavaDoc(tx1, ty1, (int) tx2, (int) ty2);
659     }
660
661     /**
662      * Computes the union of this <code>Rectangle</code> with the
663      * specified <code>Rectangle</code>. Returns a new
664      * <code>Rectangle</code> that
665      * represents the union of the two rectangles
666      * @param r the specified <code>Rectangle</code>
667      * @return the smallest <code>Rectangle</code> containing both
668      * the specified <code>Rectangle</code> and this
669      * <code>Rectangle</code>.
670      */

671     public Rectangle JavaDoc union(Rectangle JavaDoc r) {
672     int x1 = Math.min(x, r.x);
673     int x2 = Math.max(x + width, r.x + r.width);
674     int y1 = Math.min(y, r.y);
675     int y2 = Math.max(y + height, r.y + r.height);
676     return new Rectangle JavaDoc(x1, y1, x2 - x1, y2 - y1);
677     }
678
679     /**
680      * Adds a point, specified by the integer arguments <code>newx</code>
681      * and <code>newy</code>, to this <code>Rectangle</code>. The
682      * resulting <code>Rectangle</code> is
683      * the smallest <code>Rectangle</code> that contains both the
684      * original <code>Rectangle</code> and the specified point.
685      * <p>
686      * After adding a point, a call to <code>contains</code> with the
687      * added point as an argument does not necessarily return
688      * <code>true</code>. The <code>contains</code> method does not
689      * return <code>true</code> for points on the right or bottom
690      * edges of a <code>Rectangle</code>. Therefore, if the added point
691      * falls on the right or bottom edge of the enlarged
692      * <code>Rectangle</code>, <code>contains</code> returns
693      * <code>false</code> for that point.
694      * @param newx the x coordinate of the new point
695      * @param newy the y coordinate of the new point
696      */

697     public void add(int newx, int newy) {
698     int x1 = Math.min(x, newx);
699     int x2 = Math.max(x + width, newx);
700     int y1 = Math.min(y, newy);
701     int y2 = Math.max(y + height, newy);
702     x = x1;
703     y = y1;
704     width = x2 - x1;
705     height = y2 - y1;
706     }
707
708     /**
709      * Adds the specified <code>Point</code> to this
710      * <code>Rectangle</code>. The resulting <code>Rectangle</code>
711      * is the smallest <code>Rectangle</code> that contains both the
712      * original <code>Rectangle</code> and the specified
713      * <code>Point</code>.
714      * <p>
715      * After adding a <code>Point</code>, a call to <code>contains</code>
716      * with the added <code>Point</code> as an argument does not
717      * necessarily return <code>true</code>. The <code>contains</code>
718      * method does not return <code>true</code> for points on the right
719      * or bottom edges of a <code>Rectangle</code>. Therefore if the added
720      * <code>Point</code> falls on the right or bottom edge of the
721      * enlarged <code>Rectangle</code>, <code>contains</code> returns
722      * <code>false</code> for that <code>Point</code>.
723      * @param pt the new <code>Point</code> to add to this
724      * <code>Rectangle</code>
725      */

726     public void add(Point JavaDoc pt) {
727     add(pt.x, pt.y);
728     }
729
730     /**
731      * Adds a <code>Rectangle</code> to this <code>Rectangle</code>.
732      * The resulting <code>Rectangle</code> is the union of the two
733      * rectangles.
734      * @param r the specified <code>Rectangle</code>
735      */

736     public void add(Rectangle JavaDoc r) {
737     int x1 = Math.min(x, r.x);
738     int x2 = Math.max(x + width, r.x + r.width);
739     int y1 = Math.min(y, r.y);
740     int y2 = Math.max(y + height, r.y + r.height);
741     x = x1;
742     y = y1;
743     width = x2 - x1;
744     height = y2 - y1;
745     }
746
747     /**
748      * Resizes the <code>Rectangle</code> both horizontally and vertically.
749      * <p>
750      * This method modifies the <code>Rectangle</code> so that it is
751      * <code>h</code> units larger on both the left and right side,
752      * and <code>v</code> units larger at both the top and bottom.
753      * <p>
754      * The new <code>Rectangle</code> has (<code>x&nbsp;-&nbsp;h</code>,
755      * <code>y&nbsp;-&nbsp;v</code>) as its top-left corner, a
756      * width of
757      * <code>width</code>&nbsp;<code>+</code>&nbsp;<code>2h</code>,
758      * and a height of
759      * <code>height</code>&nbsp;<code>+</code>&nbsp;<code>2v</code>.
760      * <p>
761      * If negative values are supplied for <code>h</code> and
762      * <code>v</code>, the size of the <code>Rectangle</code>
763      * decreases accordingly.
764      * The <code>grow</code> method does not check whether the resulting
765      * values of <code>width</code> and <code>height</code> are
766      * non-negative.
767      * @param h the horizontal expansion
768      * @param v the vertical expansion
769      */

770     public void grow(int h, int v) {
771     x -= h;
772     y -= v;
773     width += h * 2;
774     height += v * 2;
775     }
776
777     /**
778      * Determines whether or not this <code>Rectangle</code> is empty. A
779      * <code>Rectangle</code> is empty if its width or its height is less
780      * than or equal to zero.
781      * @return <code>true</code> if this <code>Rectangle</code> is empty;
782      * <code>false</code> otherwise.
783      */

784     public boolean isEmpty() {
785     return (width <= 0) || (height <= 0);
786     }
787
788     /**
789      * Determines where the specified coordinates lie with respect
790      * to this <code>Rectangle</code>.
791      * This method computes a binary OR of the appropriate mask values
792      * indicating, for each side of this <code>Rectangle</code>,
793      * whether or not the specified coordinates are on the same side of the
794      * edge as the rest of this <code>Rectangle</code>.
795      * @param x the specified x coordinate
796      * @param y the specified y coordinate
797      * @return the logical OR of all appropriate out codes.
798      * @see #OUT_LEFT
799      * @see #OUT_TOP
800      * @see #OUT_RIGHT
801      * @see #OUT_BOTTOM
802      * @since 1.2
803      */

804     public int outcode(double x, double y) {
805     /*
806      * Note on casts to double below. If the arithmetic of
807      * x+w or y+h is done in int, then we may get integer
808      * overflow. By converting to double before the addition
809      * we force the addition to be carried out in double to
810      * avoid overflow in the comparison.
811      *
812      * See bug 4320890 for problems that this can cause.
813      */

814     int out = 0;
815     if (this.width <= 0) {
816         out |= OUT_LEFT | OUT_RIGHT;
817     } else if (x < this.x) {
818         out |= OUT_LEFT;
819     } else if (x > this.x + (double) this.width) {
820         out |= OUT_RIGHT;
821     }
822     if (this.height <= 0) {
823         out |= OUT_TOP | OUT_BOTTOM;
824     } else if (y < this.y) {
825         out |= OUT_TOP;
826     } else if (y > this.y + (double) this.height) {
827         out |= OUT_BOTTOM;
828     }
829     return out;
830     }
831
832     /**
833      * Returns a new {@link Rectangle2D} object
834      * representing the intersection of this <code>Rectangle</code> with the
835      * specified <code>Rectangle2D</code>.
836      * @param r the <code>Rectangle2D</code> to be intersected
837      * with this <code>Rectangle</code>
838      * @return the largest <code>Rectangle2D</code> contained in both the
839      * specified <code>Rectangle2D</code> and in
840      * this <code>Rectangle</code>.
841      * @since 1.2
842      */

843     public Rectangle2D JavaDoc createIntersection(Rectangle2D JavaDoc r) {
844     if (r instanceof Rectangle JavaDoc) {
845         return intersection((Rectangle JavaDoc) r);
846     }
847     Rectangle2D JavaDoc dest = new Rectangle2D.Double JavaDoc();
848     Rectangle2D.intersect(this, r, dest);
849     return dest;
850     }
851
852     /**
853      * Returns a new <code>Rectangle2D</code> object representing the
854      * union of this <code>Rectangle</code> with the specified
855      * <code>Rectangle2D</code>.
856      * @param r the <code>Rectangle2D</code> to be combined with
857      * this <code>Rectangle</code>
858      * @return the smallest <code>Rectangle2D</code> containing
859      * both the specified <code>Rectangle2D</code> and this
860      * <code>Rectangle</code>.
861      * @since 1.2
862      */

863     public Rectangle2D JavaDoc createUnion(Rectangle2D JavaDoc r) {
864     if (r instanceof Rectangle JavaDoc) {
865         return union((Rectangle JavaDoc) r);
866     }
867     Rectangle2D JavaDoc dest = new Rectangle2D.Double JavaDoc();
868     Rectangle2D.union(this, r, dest);
869     return dest;
870     }
871
872     /**
873      * Checks whether two rectangles are equal.
874      * <p>
875      * The result is <code>true</code> if and only if the argument is not
876      * <code>null</code> and is a <code>Rectangle</code> object that has the
877      * same top-left corner, width, and height as this <code>Rectangle</code>.
878      * @param obj the <code>Object</code> to compare with
879      * this <code>Rectangle</code>
880      * @return <code>true</code> if the objects are equal;
881      * <code>false</code> otherwise.
882      */

883     public boolean equals(Object JavaDoc obj) {
884     if (obj instanceof Rectangle JavaDoc) {
885         Rectangle JavaDoc r = (Rectangle JavaDoc)obj;
886         return ((x == r.x) &&
887             (y == r.y) &&
888             (width == r.width) &&
889             (height == r.height));
890     }
891     return super.equals(obj);
892     }
893
894     /**
895      * Returns a <code>String</code> representing this
896      * <code>Rectangle</code> and its values.
897      * @return a <code>String</code> representing this
898      * <code>Rectangle</code> object's coordinate and size values.
899      */

900     public String JavaDoc toString() {
901     return getClass().getName() + "[x=" + x + ",y=" + y + ",width=" + width + ",height=" + height + "]";
902     }
903 }
904
Popular Tags