KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > geom > Rectangle2D


1 /*
2  * @(#)Rectangle2D.java 1.29 03/12/19
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.geom;
9
10 /**
11  * The <code>Rectangle2D</code> class describes a rectangle
12  * defined by a location (x,&nbsp;y) and dimension
13  * (w&nbsp;x&nbsp;h).
14  * <p>
15  * This class is only the abstract superclass for all objects that
16  * store a 2D rectangle.
17  * The actual storage representation of the coordinates is left to
18  * the subclass.
19  *
20  * @version 1.29, 12/19/03
21  * @author Jim Graham
22  */

23 public abstract class Rectangle2D extends RectangularShape JavaDoc {
24     /**
25      * The bitmask that indicates that a point lies to the left of
26      * this <code>Rectangle2D</code>.
27      * @since 1.2
28      */

29     public static final int OUT_LEFT = 1;
30
31     /**
32      * The bitmask that indicates that a point lies above
33      * this <code>Rectangle2D</code>.
34      * @since 1.2
35      */

36     public static final int OUT_TOP = 2;
37
38     /**
39      * The bitmask that indicates that a point lies to the right of
40      * this <code>Rectangle2D</code>.
41      * @since 1.2
42      */

43     public static final int OUT_RIGHT = 4;
44
45     /**
46      * The bitmask that indicates that a point lies below
47      * this <code>Rectangle2D</code>.
48      * @since 1.2
49      */

50     public static final int OUT_BOTTOM = 8;
51
52     /**
53      * The <code>Float</code> class defines a rectangle specified in float
54      * coordinates.
55      * @since 1.2
56      */

57     public static class Float extends Rectangle2D JavaDoc {
58     /**
59      * The x coordinate of this <code>Rectangle2D</code>.
60      * @since 1.2
61      */

62     public float x;
63
64     /**
65      * The y coordinate of this <code>Rectangle2D</code>.
66      * @since 1.2
67      */

68     public float y;
69
70     /**
71      * The width of this <code>Rectangle2D</code>.
72      * @since 1.2
73      */

74     public float width;
75
76     /**
77      * The height of this <code>Rectangle2D</code>.
78      * @since 1.2
79      */

80     public float height;
81
82     /**
83      * Constructs a new <code>Rectangle2D</code>, initialized to
84          * location (0.0,&nbsp;0.0) and size (0.0,&nbsp;0.0).
85      * @since 1.2
86      */

87     public Float() {
88     }
89
90     /**
91      * Constructs and initializes a <code>Rectangle2D</code>
92          * from the specified float coordinates.
93      * @param x,&nbsp;y the coordinates of the
94          * upper left corner of the newly constructed
95          * <code>Rectangle2D</code>
96      * @param w the width of the newly constructed
97          * <code>Rectangle2D</code>
98      * @param h the height of the newly constructed
99          * <code>Rectangle2D</code>
100      * @since 1.2
101     */

102     public Float(float x, float y, float w, float h) {
103         setRect(x, y, w, h);
104     }
105
106     /**
107      * Returns the X coordinate of this <code>Rectangle2D</code>
108          * in double precision.
109          * @return the X coordinate of this <code>Rectangle2D</code>.
110      * @since 1.2
111      */

112     public double getX() {
113         return (double) x;
114     }
115
116     /**
117      * Returns the Y coordinate of this <code>Rectangle2D</code>
118          * in double precision.
119          * @return the Y coordinate of this <code>Rectangle2D</code>.
120      * @since 1.2
121      */

122     public double getY() {
123         return (double) y;
124     }
125
126     /**
127      * Returns the width of this <code>Rectangle2D</code>
128          * in double precision.
129          * @return the width of this <code>Rectangle2D</code>.
130          * @since 1.2
131      */

132     public double getWidth() {
133         return (double) width;
134     }
135
136     /**
137          * Returns the height of this <code>Rectangle2D</code>
138          * in double precision.
139          * @return the height of this <code>Rectangle2D</code>.
140      * @since 1.2
141      */

142     public double getHeight() {
143         return (double) height;
144     }
145
146     /**
147      * Determines whether or not this <code>Rectangle2D</code>
148          * is empty.
149          * @return <code>true</code> if this <code>Rectangle2D</code>
150          * is empty; <code>false</code> otherwise.
151      * @since 1.2
152      */

153     public boolean isEmpty() {
154         return (width <= 0.0f) || (height <= 0.0f);
155     }
156
157     /**
158      * Sets the location and size of this <code>Rectangle2D</code>
159          * to the specified float values.
160          * @param x,&nbsp;y the coordinates to which to set the
161          * location of the upper left corner of this
162          * <code>Rectangle2D</code>
163          * @param w the value to use to set the width of this
164          * <code>Rectangle2D</code>
165          * @param h the value to use to set the height of this
166          * <code>Rectangle2D</code>
167      * @since 1.2
168      */

169     public void setRect(float x, float y, float w, float h) {
170         this.x = x;
171         this.y = y;
172         this.width = w;
173         this.height = h;
174     }
175
176     /**
177      * Sets the location and size of this <code>Rectangle2D</code>
178          * to the specified double values.
179          * @param x,&nbsp;y the coordinates to which to set the
180          * location of the upper left corner of this
181          * <code>Rectangle2D</code>
182          * @param w the value to use to set the width of this
183          * <code>Rectangle2D</code>
184          * @param h the value to use to set the height of this
185          * <code>Rectangle2D</code>
186      * @since 1.2
187      */

188     public void setRect(double x, double y, double w, double h) {
189         this.x = (float) x;
190         this.y = (float) y;
191         this.width = (float) w;
192         this.height = (float) h;
193     }
194
195     /**
196      * Sets this <code>Rectangle2D</code> to be the same as the
197          * specified <code>Rectangle2D</code>.
198          * @param r the specified <code>Rectangle2D</code>
199      * @since 1.2
200      */

201     public void setRect(Rectangle2D JavaDoc r) {
202         this.x = (float) r.getX();
203         this.y = (float) r.getY();
204         this.width = (float) r.getWidth();
205         this.height = (float) r.getHeight();
206     }
207
208     /**
209      * Determines where the specified float coordinates lie with respect
210      * to this <code>Rectangle2D</code>.
211      * This method computes a binary OR of the appropriate mask values
212      * indicating, for each side of this <code>Rectangle2D</code>,
213          * whether or not the specified coordinates are on the same side
214          * of the edge as the rest of this <code>Rectangle2D</code>.
215          * @param x,&nbsp;y the specified coordinates
216          * @return the logical OR of all appropriate out codes.
217      * @see Rectangle2D#OUT_LEFT
218      * @see Rectangle2D#OUT_TOP
219      * @see Rectangle2D#OUT_RIGHT
220      * @see Rectangle2D#OUT_BOTTOM
221      * @since 1.2
222      */

223     public int outcode(double x, double y) {
224         /*
225          * Note on casts to double below. If the arithmetic of
226          * x+w or y+h is done in float, then some bits may be
227          * lost if the binary exponents of x/y and w/h are not
228          * similar. By converting to double before the addition
229          * we force the addition to be carried out in double to
230          * avoid rounding error in the comparison.
231          *
232          * See bug 4320890 for problems that this inaccuracy causes.
233          */

234         int out = 0;
235         if (this.width <= 0) {
236         out |= OUT_LEFT | OUT_RIGHT;
237         } else if (x < this.x) {
238         out |= OUT_LEFT;
239         } else if (x > this.x + (double) this.width) {
240         out |= OUT_RIGHT;
241         }
242         if (this.height <= 0) {
243         out |= OUT_TOP | OUT_BOTTOM;
244         } else if (y < this.y) {
245         out |= OUT_TOP;
246         } else if (y > this.y + (double) this.height) {
247         out |= OUT_BOTTOM;
248         }
249         return out;
250     }
251
252     /**
253      * Returns the high precision bounding box of this
254          * <code>Rectangle2D</code>.
255          * @return the bounding box of this <code>Rectangle2D</code>.
256      * @since 1.2
257      */

258     public Rectangle2D JavaDoc getBounds2D() {
259         return new Float JavaDoc(x, y, width, height);
260     }
261
262     /**
263      * Returns a new <code>Rectangle2D</code> object
264          * representing the intersection of
265      * this <code>Rectangle2D</code> with the specified
266          * <code>Rectangle2D</code>.
267      * @param r the <code>Rectangle2D</code> that is
268          * intersected with this <code>Rectangle2D</code>
269      * @return the largest <code>Rectangle2D</code>
270          * contained in both the specified
271          * <code>Rectangle2D</code> and in this
272          * <code>Rectangle2D</code>.
273      * @since 1.2
274      */

275     public Rectangle2D JavaDoc createIntersection(Rectangle2D JavaDoc r) {
276         Rectangle2D JavaDoc dest;
277         if (r instanceof Float JavaDoc) {
278         dest = new Rectangle2D.Float JavaDoc();
279         } else {
280         dest = new Rectangle2D.Double JavaDoc();
281         }
282         Rectangle2D.intersect(this, r, dest);
283         return dest;
284     }
285
286     /**
287      * Returns a new <code>Rectangle2D</code> object
288          * representing the union of this <code>Rectangle2D</code>
289          * with the specified <code>Rectangle2D</code>.
290      * @param r the <code>Rectangle2D</code> to be combined with
291          * this <code>Rectangle2D</code>
292      * @return the smallest <code>Rectangle2D</code> containing
293          * both the specified <code>Rectangle2D</code> and this
294          * <code>Rectangle2D</code>.
295      * @since 1.2
296      */

297     public Rectangle2D JavaDoc createUnion(Rectangle2D JavaDoc r) {
298         Rectangle2D JavaDoc dest;
299         if (r instanceof Float JavaDoc) {
300         dest = new Rectangle2D.Float JavaDoc();
301         } else {
302         dest = new Rectangle2D.Double JavaDoc();
303         }
304         Rectangle2D.union(this, r, dest);
305         return dest;
306     }
307
308     /**
309      * Returns the <code>String</code> representation of this
310          * <code>Rectangle2D</code>.
311          * @return a <code>String</code> representing this
312          * <code>Rectangle2D</code>.
313      * @since 1.2
314      */

315     public String JavaDoc toString() {
316         return getClass().getName()
317         + "[x=" + x +
318         ",y=" + y +
319         ",w=" + width +
320         ",h=" + height + "]";
321     }
322     }
323
324     /**
325      * The <code>Double</code> class defines a rectangle specified in
326      * double coordinates.
327      * @since 1.2
328      */

329     public static class Double extends Rectangle2D JavaDoc {
330     /**
331      * The x coordinate of this <code>Rectangle2D</code>.
332      * @since 1.2
333      */

334     public double x;
335
336     /**
337      * The y coordinate of this <code>Rectangle2D</code>.
338      * @since 1.2
339      */

340     public double y;
341
342     /**
343      * The width of this <code>Rectangle2D</code>.
344      * @since 1.2
345      */

346     public double width;
347
348     /**
349      * The height of this <code>Rectangle2D</code>.
350      * @since 1.2
351      */

352     public double height;
353
354     /**
355      * Constructs a new <code>Rectangle2D</code>, initialized to
356          * location (0,&nbsp;0) and size (0,&nbsp;0).
357      * @since 1.2
358      */

359     public Double() {
360     }
361
362     /**
363      * Constructs and initializes a <code>Rectangle2D</code>
364          * from the specified double coordinates.
365      * @param x,&nbsp;y the coordinates of the upper left corner
366          * of the newly constructed <code>Rectangle2D</code>
367      * @param w the width of the
368          * newly constructed <code>Rectangle2D</code>
369      * @param h the height of the
370          * newly constructed <code>Rectangle2D</code>
371      * @since 1.2
372      */

373     public Double(double x, double y, double w, double h) {
374         setRect(x, y, w, h);
375     }
376
377     /**
378      * Returns the X coordinate of this <code>Rectangle2D</code> in
379          * double precision.
380          * @return the X coordinate of this <code>Rectangle2D</code>.
381      * @since 1.2
382      */

383     public double getX() {
384         return x;
385     }
386
387     /**
388      * Returns the Y coordinate of this <code>Rectangle2D</code> in
389          * double precision.
390          * @return the Y coordinate of this <code>Rectangle2D</code>.
391      * @since 1.2
392      */

393     public double getY() {
394         return y;
395     }
396
397     /**
398      * Returns the width of this <code>Rectangle2D</code> in
399          * double precision.
400          * @return the width of this <code>Rectangle2D</code>.
401      * @since 1.2
402      */

403     public double getWidth() {
404         return width;
405     }
406
407     /**
408      * Returns the height of this <code>Rectangle2D</code> in
409          * double precision.
410          * @return the height of this <code>Rectangle2D</code>.
411      * @since 1.2
412      */

413     public double getHeight() {
414         return height;
415     }
416
417     /**
418      * Determines whether or not this <code>Rectangle2D</code>
419          * is empty.
420          * @return <code>true</code> if this <code>Rectangle2D</code>
421          * is empty; <code>false</code> otherwise.
422      * @since 1.2
423      */

424     public boolean isEmpty() {
425         return (width <= 0.0) || (height <= 0.0);
426     }
427
428     /**
429      * Sets the location and size of this <code>Rectangle2D</code>
430          * to the specified double values.
431          * @param x,&nbsp;y the coordinates to which to set the
432          * upper left corner of this <code>Rectangle2D</code>
433          * @param w the value to use to set the width of this
434          * <code>Rectangle2D</code>
435          * @param h the value to use to set the height of this
436          * <code>Rectangle2D</code>
437      * @since 1.2
438      */

439     public void setRect(double x, double y, double w, double h) {
440         this.x = x;
441         this.y = y;
442         this.width = w;
443         this.height = h;
444     }
445
446     /**
447      * Sets this <code>Rectangle2D</code> to be the same as the
448          * specified <code>Rectangle2D</code>.
449          * @param r the specified <code>Rectangle2D</code>
450      * @since 1.2
451      */

452     public void setRect(Rectangle2D JavaDoc r) {
453         this.x = r.getX();
454         this.y = r.getY();
455         this.width = r.getWidth();
456         this.height = r.getHeight();
457     }
458
459     /**
460      * Determines where the specified double coordinates lie with respect
461      * to this <code>Rectangle2D</code>.
462          * This method computes a binary OR of the appropriate mask values
463          * indicating, for each side of this <code>Rectangle2D</code>,
464          * whether or not the specified coordinates are on the same side
465          * of the edge as the rest of this <code>Rectangle2D</code>.
466          * @param x,&nbsp;y the specified coordinates
467          * @return the logical OR of all appropriate out codes.
468          * @see Rectangle2D#OUT_LEFT
469      * @see Rectangle2D#OUT_TOP
470      * @see Rectangle2D#OUT_RIGHT
471      * @see Rectangle2D#OUT_BOTTOM
472      * @since 1.2
473      */

474     public int outcode(double x, double y) {
475         int out = 0;
476         if (this.width <= 0) {
477         out |= OUT_LEFT | OUT_RIGHT;
478         } else if (x < this.x) {
479         out |= OUT_LEFT;
480         } else if (x > this.x + this.width) {
481         out |= OUT_RIGHT;
482         }
483         if (this.height <= 0) {
484         out |= OUT_TOP | OUT_BOTTOM;
485         } else if (y < this.y) {
486         out |= OUT_TOP;
487         } else if (y > this.y + this.height) {
488         out |= OUT_BOTTOM;
489         }
490         return out;
491     }
492
493     /**
494      * Returns the high precision bounding box of this
495          * <code>Rectangle2D</code>.
496          * @return the bounding box of this <code>Rectangle2D</code>.
497      * @since 1.2
498      */

499     public Rectangle2D JavaDoc getBounds2D() {
500         return new Double JavaDoc(x, y, width, height);
501     }
502
503     /**
504      * Returns a new <code>Rectangle2D</code> object representing
505          * the intersection of this <code>Rectangle2D</code> with the
506          * specified <code>Rectangle2D</code>.
507      * @param r the <code>Rectangle2D</code> to be intersected
508          * with this <code>Rectangle2D</code>
509      * @return the largest <code>Rectangle2D</code> contained in
510          * both the specified <code>Rectangle2D</code> and in this
511          * <code>Rectangle2D</code>.
512      * @since 1.2
513      */

514     public Rectangle2D JavaDoc createIntersection(Rectangle2D JavaDoc r) {
515         Rectangle2D JavaDoc dest = new Rectangle2D.Double JavaDoc();
516         Rectangle2D.intersect(this, r, dest);
517         return dest;
518     }
519
520     /**
521      * Returns a new <code>Rectangle2D</code> object representing
522          * the union of this <code>Rectangle2D</code> with the
523          * specified <code>Rectangle2D</code>.
524      * @param r the <code>Rectangle2D</code> to be combined with
525          * this <code>Rectangle2D</code>
526      * @return the smallest <code>Rectangle2D</code> containing
527          * both the specified <code>Rectangle2D</code> and this
528          * <code>Rectangle2D</code>.
529      * @since 1.2
530      */

531     public Rectangle2D JavaDoc createUnion(Rectangle2D JavaDoc r) {
532         Rectangle2D JavaDoc dest = new Rectangle2D.Double JavaDoc();
533         Rectangle2D.union(this, r, dest);
534         return dest;
535     }
536
537     /**
538      * Returns the <code>String</code> representation of this
539          * <code>Rectangle2D</code>.
540          * @return a <code>String</code> representing this
541          * <code>Rectangle2D</code>.
542      * @since 1.2
543      */

544     public String JavaDoc toString() {
545         return getClass().getName()
546         + "[x=" + x +
547         ",y=" + y +
548         ",w=" + width +
549         ",h=" + height + "]";
550     }
551     }
552
553     /**
554      * This is an abstract class that cannot be instantiated directly.
555      * Type-specific implementation subclasses are available for
556      * instantiation and provide a number of formats for storing
557      * the information necessary to satisfy the various accessor
558      * methods below.
559      *
560      * @see java.awt.geom.Rectangle2D.Float
561      * @see java.awt.geom.Rectangle2D.Double
562      * @see java.awt.Rectangle
563      */

564     protected Rectangle2D() {
565     }
566
567     /**
568      * Sets the location and size of this <code>Rectangle2D</code>
569      * to the specified double values.
570      * @param x,&nbsp;y the coordinates to which to set the
571      * location of the upper left corner of this
572      * <code>Rectangle2D</code>
573      * @param w the value to use to set the width of this
574      * <code>Rectangle2D</code>
575      * @param h the value to use to set the height of this
576      * <code>Rectangle2D</code>
577      * @since 1.2
578      */

579     public abstract void setRect(double x, double y, double w, double h);
580
581     /**
582      * Sets this <code>Rectangle2D</code> to be the same as the specified
583      * <code>Rectangle2D</code>.
584      * @param r the specified <code>Rectangle2D</code>
585      * @since 1.2
586      */

587     public void setRect(Rectangle2D JavaDoc r) {
588     setRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
589     }
590
591     /**
592      * Tests if the specified line segment intersects the interior of this
593      * <code>Rectangle2D</code>.
594      * @param x1,&nbsp;y1 the first endpoint of the specified
595      * line segment
596      * @param x2,&nbsp;y2 the second endpoint of the specified
597      * line segment
598      * @return <code>true</code> if the specified line segment intersects
599      * the interior of this <code>Rectangle2D</code>; <code>false</code>
600      * otherwise.
601      * @since 1.2
602      */

603     public boolean intersectsLine(double x1, double y1, double x2, double y2) {
604     int out1, out2;
605     if ((out2 = outcode(x2, y2)) == 0) {
606         return true;
607     }
608     while ((out1 = outcode(x1, y1)) != 0) {
609         if ((out1 & out2) != 0) {
610         return false;
611         }
612         if ((out1 & (OUT_LEFT | OUT_RIGHT)) != 0) {
613         double x = getX();
614         if ((out1 & OUT_RIGHT) != 0) {
615             x += getWidth();
616         }
617         y1 = y1 + (x - x1) * (y2 - y1) / (x2 - x1);
618         x1 = x;
619         } else {
620         double y = getY();
621         if ((out1 & OUT_BOTTOM) != 0) {
622             y += getHeight();
623         }
624         x1 = x1 + (y - y1) * (x2 - x1) / (y2 - y1);
625         y1 = y;
626         }
627     }
628     return true;
629     }
630
631     /**
632      * Tests if the specified line segment intersects the interior of this
633      * <code>Rectangle2D</code>.
634      * @param l the specified {@link Line2D} to test for intersection
635      * with the interior of this <code>Rectangle2D</code>
636      * @return <code>true</code> if the specified <code>Line2D</code>
637      * intersects the interior of this <code>Rectangle2D</code>;
638      * <code>false</code> otherwise.
639      * @since 1.2
640      */

641     public boolean intersectsLine(Line2D JavaDoc l) {
642     return intersectsLine(l.getX1(), l.getY1(), l.getX2(), l.getY2());
643     }
644
645     /**
646      * Determines where the specified coordinates lie with respect
647      * to this <code>Rectangle2D</code>.
648      * This method computes a binary OR of the appropriate mask values
649      * indicating, for each side of this <code>Rectangle2D</code>,
650      * whether or not the specified coordinates are on the same side
651      * of the edge as the rest of this <code>Rectangle2D</code>.
652      * @param x,&nbsp;y the specified coordinates
653      * @return the logical OR of all appropriate out codes.
654      * @see #OUT_LEFT
655      * @see #OUT_TOP
656      * @see #OUT_RIGHT
657      * @see #OUT_BOTTOM
658      * @since 1.2
659      */

660     public abstract int outcode(double x, double y);
661
662     /**
663      * Determines where the specified {@link Point2D} lies with
664      * respect to this <code>Rectangle2D</code>.
665      * This method computes a binary OR of the appropriate mask values
666      * indicating, for each side of this <code>Rectangle2D</code>,
667      * whether or not the specified <code>Point2D</code> is on the same
668      * side of the edge as the rest of this <code>Rectangle2D</code>.
669      * @param p the specified <code>Point2D</code>
670      * @return the logical OR of all appropriate out codes.
671      * @see #OUT_LEFT
672      * @see #OUT_TOP
673      * @see #OUT_RIGHT
674      * @see #OUT_BOTTOM
675      * @since 1.2
676      */

677     public int outcode(Point2D JavaDoc p) {
678     return outcode(p.getX(), p.getY());
679     }
680
681     /**
682      * Sets the location and size of the outer bounds of this
683      * <code>Rectangle2D</code> to the specified rectangular values.
684      * @param x,&nbsp;y the coordinates to which to set the
685      * location of the upper left corner of the outer bounds of
686      * this <code>Rectangle2D</code>
687      * @param w the value to use to set the width of the outer
688      * bounds of this <code>Rectangle2D</code>
689      * @param h the value to use to set the height of the outer
690      * bounds of this <code>Rectangle2D</code>
691      * @since 1.2
692      */

693     public void setFrame(double x, double y, double w, double h) {
694     setRect(x, y, w, h);
695     }
696
697     /**
698      * Returns the high precision bounding box of this
699      * <code>Rectangle2D</code>.
700      * @return the bounding box of this <code>Rectangle2D</code>.
701      * @since 1.2
702      */

703     public Rectangle2D JavaDoc getBounds2D() {
704     return (Rectangle2D JavaDoc) clone();
705     }
706
707     /**
708      * Tests if a specified coordinate is inside the boundary of this
709      * <code>Rectangle2D</code>.
710      * @param x,&nbsp;y the coordinates to test
711      * @return <code>true</code> if the specified coordinates are
712      * inside the boundary of this <code>Rectangle2D</code>;
713      * <code>false</code> otherwise.
714      * @since 1.2
715      */

716     public boolean contains(double x, double y) {
717     double x0 = getX();
718     double y0 = getY();
719     return (x >= x0 &&
720         y >= y0 &&
721         x < x0 + getWidth() &&
722         y < y0 + getHeight());
723     }
724
725     /**
726      * Tests if the interior of this <code>Rectangle2D</code>
727      * intersects the interior of a specified set of rectangular
728      * coordinates.
729      * @param x,&nbsp;y the coordinates of the upper left corner
730      * of the specified set of rectangular coordinates
731      * @param w the width of the specified set of rectangular
732      * coordinates
733      * @param h the height of the specified set of rectangular
734      * coordinates
735      * @return <code>true</code> if this <code>Rectangle2D</code>
736      * intersects the interior of a specified set of rectangular
737      * coordinates; <code>false</code> otherwise.
738      * @since 1.2
739      */

740     public boolean intersects(double x, double y, double w, double h) {
741     if (isEmpty() || w <= 0 || h <= 0) {
742         return false;
743     }
744     double x0 = getX();
745     double y0 = getY();
746     return (x + w > x0 &&
747         y + h > y0 &&
748         x < x0 + getWidth() &&
749         y < y0 + getHeight());
750     }
751
752     /**
753      * Tests if the interior of this <code>Rectangle2D</code> entirely
754      * contains the specified set of rectangular coordinates.
755      * @param x,&nbsp;y the coordinates of the upper left corner
756      * of the specified set of rectangular coordinates
757      * @param w the width of the specified set of rectangular
758      * coordinates
759      * @param h the height of the specified set of rectangular
760      * coordinates
761      * @return <code>true</code> if this <code>Rectangle2D</code>
762      * entirely contains specified set of rectangular
763      * coordinates; <code>false</code> otherwise.
764      * @since 1.2
765      */

766     public boolean contains(double x, double y, double w, double h) {
767     if (isEmpty() || w <= 0 || h <= 0) {
768         return false;
769     }
770     double x0 = getX();
771     double y0 = getY();
772     return (x >= x0 &&
773         y >= y0 &&
774         (x + w) <= x0 + getWidth() &&
775         (y + h) <= y0 + getHeight());
776     }
777
778     /**
779      * Returns a new <code>Rectangle2D</code> object representing the
780      * intersection of this <code>Rectangle2D</code> with the specified
781      * <code>Rectangle2D</code>.
782      * @param r the <code>Rectangle2D</code> to be intersected with
783      * this <code>Rectangle2D</code>
784      * @return the largest <code>Rectangle2D</code> contained in both
785      * the specified <code>Rectangle2D</code> and in this
786      * <code>Rectangle2D</code>.
787      * @since 1.2
788      */

789     public abstract Rectangle2D JavaDoc createIntersection(Rectangle2D JavaDoc r);
790
791     /**
792      * Intersects the pair of specified source <code>Rectangle2D</code>
793      * objects and puts the result into the specified destination
794      * <code>Rectangle2D</code> object. One of the source rectangles
795      * can also be the destination to avoid creating a third Rectangle2D
796      * object, but in this case the original points of this source
797      * rectangle will be overwritten by this method.
798      * @param src1 the first of a pair of <code>Rectangle2D</code>
799      * objects to be intersected with each other
800      * @param src2 the second of a pair of <code>Rectangle2D</code>
801      * objects to be intersected with each other
802      * @param dest the <code>Rectangle2D</code> that holds the
803      * results of the intersection of <code>src1</code> and
804      * <code>src2</code>
805      * @since 1.2
806      */

807     public static void intersect(Rectangle2D JavaDoc src1,
808                  Rectangle2D JavaDoc src2,
809                  Rectangle2D JavaDoc dest) {
810     double x1 = Math.max(src1.getMinX(), src2.getMinX());
811     double y1 = Math.max(src1.getMinY(), src2.getMinY());
812     double x2 = Math.min(src1.getMaxX(), src2.getMaxX());
813     double y2 = Math.min(src1.getMaxY(), src2.getMaxY());
814     dest.setFrame(x1, y1, x2-x1, y2-y1);
815     }
816       
817     /**
818      * Returns a new <code>Rectangle2D</code> object representing the
819      * union of this <code>Rectangle2D</code> with the specified
820      * <code>Rectangle2D</code>.
821      * @param r the <code>Rectangle2D</code> to be combined with
822      * this <code>Rectangle2D</code>
823      * @return the smallest <code>Rectangle2D</code> containing both
824      * the specified <code>Rectangle2D</code> and this
825      * <code>Rectangle2D</code>.
826      * @since 1.2
827      */

828     public abstract Rectangle2D JavaDoc createUnion(Rectangle2D JavaDoc r);
829
830     /**
831      * Unions the pair of source <code>Rectangle2D</code> objects
832      * and puts the result into the specified destination
833      * <code>Rectangle2D</code> object. One of the source rectangles
834      * can also be the destination to avoid creating a third Rectangle2D
835      * object, but in this case the original points of this source
836      * rectangle will be overwritten by this method.
837      * @param src1 the first of a pair of <code>Rectangle2D</code>
838      * objects to be combined with each other
839      * @param src2 the second of a pair of <code>Rectangle2D</code>
840      * objects to be combined with each other
841      * @param dest the <code>Rectangle2D</code> that holds the
842      * results of the union of <code>src1</code> and
843      * <code>src2</code>
844      * @since 1.2
845      */

846     public static void union(Rectangle2D JavaDoc src1,
847                  Rectangle2D JavaDoc src2,
848                  Rectangle2D JavaDoc dest) {
849     double x1 = Math.min(src1.getMinX(), src2.getMinX());
850     double y1 = Math.min(src1.getMinY(), src2.getMinY());
851     double x2 = Math.max(src1.getMaxX(), src2.getMaxX());
852     double y2 = Math.max(src1.getMaxY(), src2.getMaxY());
853     dest.setFrameFromDiagonal(x1, y1, x2, y2);
854     }
855       
856     /**
857      * Adds a point, specified by the double precision arguments
858      * <code>newx</code> and <code>newy</code>, to this
859      * <code>Rectangle2D</code>. The resulting <code>Rectangle2D</code>
860      * is the smallest <code>Rectangle2D</code> that
861      * contains both the original <code>Rectangle2D</code> and the
862      * specified point.
863      * <p>
864      * After adding a point, a call to <code>contains</code> with the
865      * added point as an argument does not necessarily return
866      * <code>true</code>. The <code>contains</code> method does not
867      * return <code>true</code> for points on the right or bottom
868      * edges of a rectangle. Therefore, if the added point falls on
869      * the left or bottom edge of the enlarged rectangle,
870      * <code>contains</code> returns <code>false</code> for that point.
871      * @param newx,&nbsp;newy the coordinates of the new point
872      * @since JDK1.0
873      */

874     public void add(double newx, double newy) {
875     double x1 = Math.min(getMinX(), newx);
876     double x2 = Math.max(getMaxX(), newx);
877     double y1 = Math.min(getMinY(), newy);
878     double y2 = Math.max(getMaxY(), newy);
879     setRect(x1, y1, x2 - x1, y2 - y1);
880     }
881
882     /**
883      * Adds the <code>Point2D</code> object <code>pt</code> to this
884      * <code>Rectangle2D</code>.
885      * The resulting <code>Rectangle2D</code> is the smallest
886      * <code>Rectangle2D</code> that contains both the original
887      * <code>Rectangle2D</code> and the specified <code>Point2D</code>.
888      * <p>
889      * After adding a point, a call to <code>contains</code> with the
890      * added point as an argument does not necessarily return
891      * <code>true</code>. The <code>contains</code>
892      * method does not return <code>true</code> for points on the right
893      * or bottom edges of a rectangle. Therefore, if the added point falls
894      * on the left or bottom edge of the enlarged rectangle,
895      * <code>contains</code> returns <code>false</code> for that point.
896      * @param pt the new <code>Point2D</code> to add to this
897      * <code>Rectangle2D</code>.
898      * @since JDK1.0
899      */

900     public void add(Point2D JavaDoc pt) {
901     add(pt.getX(), pt.getY());
902     }
903
904     /**
905      * Adds a <code>Rectangle2D</code> object to this
906      * <code>Rectangle2D</code>. The resulting <code>Rectangle2D</code>
907      * is the union of the two <code>Rectangle2D</code> objects.
908      * @param r the <code>Rectangle2D</code> to add to this
909      * <code>Rectangle2D</code>.
910      * @since JDK1.0
911      */

912     public void add(Rectangle2D JavaDoc r) {
913     double x1 = Math.min(getMinX(), r.getMinX());
914     double x2 = Math.max(getMaxX(), r.getMaxX());
915     double y1 = Math.min(getMinY(), r.getMinY());
916     double y2 = Math.max(getMaxY(), r.getMaxY());
917     setRect(x1, y1, x2 - x1, y2 - y1);
918     }
919
920     /**
921      * Returns an iteration object that defines the boundary of this
922      * <code>Rectangle2D</code>.
923      * The iterator for this class is multi-threaded safe, which means
924      * that this <code>Rectangle2D</code> class guarantees that
925      * modifications to the geometry of this <code>Rectangle2D</code>
926      * object do not affect any iterations of that geometry that
927      * are already in process.
928      * @param at an optional <code>AffineTransform</code> to be applied to
929      * the coordinates as they are returned in the iteration, or
930      * <code>null</code> if untransformed coordinates are desired
931      * @return the <code>PathIterator</code> object that returns the
932      * geometry of the outline of this
933      * <code>Rectangle2D</code>, one segment at a time.
934      * @since 1.2
935      */

936     public PathIterator JavaDoc getPathIterator(AffineTransform JavaDoc at) {
937     return new RectIterator JavaDoc(this, at);
938     }
939
940     /**
941      * Returns an iteration object that defines the boundary of the
942      * flattened <code>Rectangle2D</code>. Since rectangles are already
943      * flat, the <code>flatness</code> parameter is ignored.
944      * The iterator for this class is multi-threaded safe, which means
945      * that this <code>Rectangle2D</code> class guarantees that
946      * modifications to the geometry of this <code>Rectangle2D</code>
947      * object do not affect any iterations of that geometry that
948      * are already in process.
949      * @param at an optional <code>AffineTransform</code> to be applied to
950      * the coordinates as they are returned in the iteration, or
951      * <code>null</code> if untransformed coordinates are desired
952      * @param flatness the maximum distance that the line segments used to
953      * approximate the curved segments are allowed to deviate from any
954      * point on the original curve. Since rectangles are already flat,
955      * the <code>flatness</code> parameter is ignored.
956      * @return the <code>PathIterator</code> object that returns the
957      * geometry of the outline of this
958      * <code>Rectangle2D</code>, one segment at a time.
959      * @since 1.2
960      */

961     public PathIterator JavaDoc getPathIterator(AffineTransform JavaDoc at, double flatness) {
962     return new RectIterator JavaDoc(this, at);
963     }
964
965     /**
966      * Returns the hashcode for this <code>Rectangle2D</code>.
967      * @return the hashcode for this <code>Rectangle2D</code>.
968      */

969     public int hashCode() {
970     long bits = java.lang.Double.doubleToLongBits(getX());
971     bits += java.lang.Double.doubleToLongBits(getY()) * 37;
972     bits += java.lang.Double.doubleToLongBits(getWidth()) * 43;
973     bits += java.lang.Double.doubleToLongBits(getHeight()) * 47;
974     return (((int) bits) ^ ((int) (bits >> 32)));
975     }
976
977     /**
978      * Determines whether or not the specified <code>Object</code> is
979      * equal to this <code>Rectangle2D</code>. The specified
980      * <code>Object</code> is equal to this <code>Rectangle2D</code>
981      * if it is an instance of <code>Rectangle2D</code> and if its
982      * location and size are the same as this <code>Rectangle2D</code>.
983      * @param obj an <code>Object</code> to be compared with this
984      * <code>Rectangle2D</code>.
985      * @return <code>true</code> if <code>obj</code> is an instance
986      * of <code>Rectangle2D</code> and has
987      * the same values; <code>false</code> otherwise.
988      * @since 1.2
989      */

990     public boolean equals(Object JavaDoc obj) {
991     if (obj == this) {
992         return true;
993     }
994     if (obj instanceof Rectangle2D JavaDoc) {
995         Rectangle2D JavaDoc r2d = (Rectangle2D JavaDoc) obj;
996         return ((getX() == r2d.getX()) &&
997             (getY() == r2d.getY()) &&
998             (getWidth() == r2d.getWidth()) &&
999             (getHeight() == r2d.getHeight()));
1000    }
1001    return false;
1002    }
1003}
1004
Popular Tags