001 /*
002 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.awt.geom;
027
028 import java.io.Serializable;
029
030 /**
031 * The <code>Rectangle2D</code> class describes a rectangle
032 * defined by a location {@code (x,y)} and dimension
033 * {@code (w x h)}.
034 * <p>
035 * This class is only the abstract superclass for all objects that
036 * store a 2D rectangle.
037 * The actual storage representation of the coordinates is left to
038 * the subclass.
039 *
040 * @version 1.38, 05/05/07
041 * @author Jim Graham
042 * @since 1.2
043 */
044 public abstract class Rectangle2D extends RectangularShape {
045 /**
046 * The bitmask that indicates that a point lies to the left of
047 * this <code>Rectangle2D</code>.
048 * @since 1.2
049 */
050 public static final int OUT_LEFT = 1;
051
052 /**
053 * The bitmask that indicates that a point lies above
054 * this <code>Rectangle2D</code>.
055 * @since 1.2
056 */
057 public static final int OUT_TOP = 2;
058
059 /**
060 * The bitmask that indicates that a point lies to the right of
061 * this <code>Rectangle2D</code>.
062 * @since 1.2
063 */
064 public static final int OUT_RIGHT = 4;
065
066 /**
067 * The bitmask that indicates that a point lies below
068 * this <code>Rectangle2D</code>.
069 * @since 1.2
070 */
071 public static final int OUT_BOTTOM = 8;
072
073 /**
074 * The <code>Float</code> class defines a rectangle specified in float
075 * coordinates.
076 * @since 1.2
077 */
078 public static class Float extends Rectangle2D implements
079 Serializable {
080 /**
081 * The X coordinate of this <code>Rectangle2D</code>.
082 * @since 1.2
083 * @serial
084 */
085 public float x;
086
087 /**
088 * The Y coordinate of this <code>Rectangle2D</code>.
089 * @since 1.2
090 * @serial
091 */
092 public float y;
093
094 /**
095 * The width of this <code>Rectangle2D</code>.
096 * @since 1.2
097 * @serial
098 */
099 public float width;
100
101 /**
102 * The height of this <code>Rectangle2D</code>.
103 * @since 1.2
104 * @serial
105 */
106 public float height;
107
108 /**
109 * Constructs a new <code>Rectangle2D</code>, initialized to
110 * location (0.0, 0.0) and size (0.0, 0.0).
111 * @since 1.2
112 */
113 public Float() {
114 }
115
116 /**
117 * Constructs and initializes a <code>Rectangle2D</code>
118 * from the specified <code>float</code> coordinates.
119 *
120 * @param x the X coordinate of the upper-left corner
121 * of the newly constructed <code>Rectangle2D</code>
122 * @param y the Y coordinate of the upper-left corner
123 * of the newly constructed <code>Rectangle2D</code>
124 * @param w the width of the newly constructed
125 * <code>Rectangle2D</code>
126 * @param h the height of the newly constructed
127 * <code>Rectangle2D</code>
128 * @since 1.2
129 */
130 public Float(float x, float y, float w, float h) {
131 setRect(x, y, w, h);
132 }
133
134 /**
135 * {@inheritDoc}
136 * @since 1.2
137 */
138 public double getX() {
139 return (double) x;
140 }
141
142 /**
143 * {@inheritDoc}
144 * @since 1.2
145 */
146 public double getY() {
147 return (double) y;
148 }
149
150 /**
151 * {@inheritDoc}
152 * @since 1.2
153 */
154 public double getWidth() {
155 return (double) width;
156 }
157
158 /**
159 * {@inheritDoc}
160 * @since 1.2
161 */
162 public double getHeight() {
163 return (double) height;
164 }
165
166 /**
167 * {@inheritDoc}
168 * @since 1.2
169 */
170 public boolean isEmpty() {
171 return (width <= 0.0f) || (height <= 0.0f);
172 }
173
174 /**
175 * Sets the location and size of this <code>Rectangle2D</code>
176 * to the specified <code>float</code> values.
177 *
178 * @param x the X coordinate of the upper-left corner
179 * of this <code>Rectangle2D</code>
180 * @param y the Y coordinate of the upper-left corner
181 * of this <code>Rectangle2D</code>
182 * @param w the width of this <code>Rectangle2D</code>
183 * @param h the height of this <code>Rectangle2D</code>
184 * @since 1.2
185 */
186 public void setRect(float x, float y, float w, float h) {
187 this .x = x;
188 this .y = y;
189 this .width = w;
190 this .height = h;
191 }
192
193 /**
194 * {@inheritDoc}
195 * @since 1.2
196 */
197 public void setRect(double x, double y, double w, double h) {
198 this .x = (float) x;
199 this .y = (float) y;
200 this .width = (float) w;
201 this .height = (float) h;
202 }
203
204 /**
205 * {@inheritDoc}
206 * @since 1.2
207 */
208 public void setRect(Rectangle2D r) {
209 this .x = (float) r.getX();
210 this .y = (float) r.getY();
211 this .width = (float) r.getWidth();
212 this .height = (float) r.getHeight();
213 }
214
215 /**
216 * {@inheritDoc}
217 * @since 1.2
218 */
219 public int outcode(double x, double y) {
220 /*
221 * Note on casts to double below. If the arithmetic of
222 * x+w or y+h is done in float, then some bits may be
223 * lost if the binary exponents of x/y and w/h are not
224 * similar. By converting to double before the addition
225 * we force the addition to be carried out in double to
226 * avoid rounding error in the comparison.
227 *
228 * See bug 4320890 for problems that this inaccuracy causes.
229 */
230 int out = 0;
231 if (this .width <= 0) {
232 out |= OUT_LEFT | OUT_RIGHT;
233 } else if (x < this .x) {
234 out |= OUT_LEFT;
235 } else if (x > this .x + (double) this .width) {
236 out |= OUT_RIGHT;
237 }
238 if (this .height <= 0) {
239 out |= OUT_TOP | OUT_BOTTOM;
240 } else if (y < this .y) {
241 out |= OUT_TOP;
242 } else if (y > this .y + (double) this .height) {
243 out |= OUT_BOTTOM;
244 }
245 return out;
246 }
247
248 /**
249 * {@inheritDoc}
250 * @since 1.2
251 */
252 public Rectangle2D getBounds2D() {
253 return new Float(x, y, width, height);
254 }
255
256 /**
257 * {@inheritDoc}
258 * @since 1.2
259 */
260 public Rectangle2D createIntersection(Rectangle2D r) {
261 Rectangle2D dest;
262 if (r instanceof Float) {
263 dest = new Rectangle2D.Float();
264 } else {
265 dest = new Rectangle2D.Double();
266 }
267 Rectangle2D.intersect(this , r, dest);
268 return dest;
269 }
270
271 /**
272 * {@inheritDoc}
273 * @since 1.2
274 */
275 public Rectangle2D createUnion(Rectangle2D r) {
276 Rectangle2D dest;
277 if (r instanceof Float) {
278 dest = new Rectangle2D.Float();
279 } else {
280 dest = new Rectangle2D.Double();
281 }
282 Rectangle2D.union(this , r, dest);
283 return dest;
284 }
285
286 /**
287 * Returns the <code>String</code> representation of this
288 * <code>Rectangle2D</code>.
289 * @return a <code>String</code> representing this
290 * <code>Rectangle2D</code>.
291 * @since 1.2
292 */
293 public String toString() {
294 return getClass().getName() + "[x=" + x + ",y=" + y + ",w="
295 + width + ",h=" + height + "]";
296 }
297
298 /*
299 * JDK 1.6 serialVersionUID
300 */
301 private static final long serialVersionUID = 3798716824173675777L;
302 }
303
304 /**
305 * The <code>Double</code> class defines a rectangle specified in
306 * double coordinates.
307 * @since 1.2
308 */
309 public static class Double extends Rectangle2D implements
310 Serializable {
311 /**
312 * The X coordinate of this <code>Rectangle2D</code>.
313 * @since 1.2
314 * @serial
315 */
316 public double x;
317
318 /**
319 * The Y coordinate of this <code>Rectangle2D</code>.
320 * @since 1.2
321 * @serial
322 */
323 public double y;
324
325 /**
326 * The width of this <code>Rectangle2D</code>.
327 * @since 1.2
328 * @serial
329 */
330 public double width;
331
332 /**
333 * The height of this <code>Rectangle2D</code>.
334 * @since 1.2
335 * @serial
336 */
337 public double height;
338
339 /**
340 * Constructs a new <code>Rectangle2D</code>, initialized to
341 * location (0, 0) and size (0, 0).
342 * @since 1.2
343 */
344 public Double() {
345 }
346
347 /**
348 * Constructs and initializes a <code>Rectangle2D</code>
349 * from the specified <code>double</code> coordinates.
350 *
351 * @param x the X coordinate of the upper-left corner
352 * of the newly constructed <code>Rectangle2D</code>
353 * @param y the Y coordinate of the upper-left corner
354 * of the newly constructed <code>Rectangle2D</code>
355 * @param w the width of the newly constructed
356 * <code>Rectangle2D</code>
357 * @param h the height of the newly constructed
358 * <code>Rectangle2D</code>
359 * @since 1.2
360 */
361 public Double(double x, double y, double w, double h) {
362 setRect(x, y, w, h);
363 }
364
365 /**
366 * {@inheritDoc}
367 * @since 1.2
368 */
369 public double getX() {
370 return x;
371 }
372
373 /**
374 * {@inheritDoc}
375 * @since 1.2
376 */
377 public double getY() {
378 return y;
379 }
380
381 /**
382 * {@inheritDoc}
383 * @since 1.2
384 */
385 public double getWidth() {
386 return width;
387 }
388
389 /**
390 * {@inheritDoc}
391 * @since 1.2
392 */
393 public double getHeight() {
394 return height;
395 }
396
397 /**
398 * {@inheritDoc}
399 * @since 1.2
400 */
401 public boolean isEmpty() {
402 return (width <= 0.0) || (height <= 0.0);
403 }
404
405 /**
406 * {@inheritDoc}
407 * @since 1.2
408 */
409 public void setRect(double x, double y, double w, double h) {
410 this .x = x;
411 this .y = y;
412 this .width = w;
413 this .height = h;
414 }
415
416 /**
417 * {@inheritDoc}
418 * @since 1.2
419 */
420 public void setRect(Rectangle2D r) {
421 this .x = r.getX();
422 this .y = r.getY();
423 this .width = r.getWidth();
424 this .height = r.getHeight();
425 }
426
427 /**
428 * {@inheritDoc}
429 * @since 1.2
430 */
431 public int outcode(double x, double y) {
432 int out = 0;
433 if (this .width <= 0) {
434 out |= OUT_LEFT | OUT_RIGHT;
435 } else if (x < this .x) {
436 out |= OUT_LEFT;
437 } else if (x > this .x + this .width) {
438 out |= OUT_RIGHT;
439 }
440 if (this .height <= 0) {
441 out |= OUT_TOP | OUT_BOTTOM;
442 } else if (y < this .y) {
443 out |= OUT_TOP;
444 } else if (y > this .y + this .height) {
445 out |= OUT_BOTTOM;
446 }
447 return out;
448 }
449
450 /**
451 * {@inheritDoc}
452 * @since 1.2
453 */
454 public Rectangle2D getBounds2D() {
455 return new Double(x, y, width, height);
456 }
457
458 /**
459 * {@inheritDoc}
460 * @since 1.2
461 */
462 public Rectangle2D createIntersection(Rectangle2D r) {
463 Rectangle2D dest = new Rectangle2D.Double();
464 Rectangle2D.intersect(this , r, dest);
465 return dest;
466 }
467
468 /**
469 * {@inheritDoc}
470 * @since 1.2
471 */
472 public Rectangle2D createUnion(Rectangle2D r) {
473 Rectangle2D dest = new Rectangle2D.Double();
474 Rectangle2D.union(this , r, dest);
475 return dest;
476 }
477
478 /**
479 * Returns the <code>String</code> representation of this
480 * <code>Rectangle2D</code>.
481 * @return a <code>String</code> representing this
482 * <code>Rectangle2D</code>.
483 * @since 1.2
484 */
485 public String toString() {
486 return getClass().getName() + "[x=" + x + ",y=" + y + ",w="
487 + width + ",h=" + height + "]";
488 }
489
490 /*
491 * JDK 1.6 serialVersionUID
492 */
493 private static final long serialVersionUID = 7771313791441850493L;
494 }
495
496 /**
497 * This is an abstract class that cannot be instantiated directly.
498 * Type-specific implementation subclasses are available for
499 * instantiation and provide a number of formats for storing
500 * the information necessary to satisfy the various accessor
501 * methods below.
502 *
503 * @see java.awt.geom.Rectangle2D.Float
504 * @see java.awt.geom.Rectangle2D.Double
505 * @see java.awt.Rectangle
506 * @since 1.2
507 */
508 protected Rectangle2D() {
509 }
510
511 /**
512 * Sets the location and size of this <code>Rectangle2D</code>
513 * to the specified <code>double</code> values.
514 *
515 * @param x the X coordinate of the upper-left corner
516 * of this <code>Rectangle2D</code>
517 * @param y the Y coordinate of the upper-left corner
518 * of this <code>Rectangle2D</code>
519 * @param w the width of this <code>Rectangle2D</code>
520 * @param h the height of this <code>Rectangle2D</code>
521 * @since 1.2
522 */
523 public abstract void setRect(double x, double y, double w, double h);
524
525 /**
526 * Sets this <code>Rectangle2D</code> to be the same as the specified
527 * <code>Rectangle2D</code>.
528 * @param r the specified <code>Rectangle2D</code>
529 * @since 1.2
530 */
531 public void setRect(Rectangle2D r) {
532 setRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
533 }
534
535 /**
536 * Tests if the specified line segment intersects the interior of this
537 * <code>Rectangle2D</code>.
538 *
539 * @param x1 the X coordinate of the start point of the specified
540 * line segment
541 * @param y1 the Y coordinate of the start point of the specified
542 * line segment
543 * @param x2 the X coordinate of the end point of the specified
544 * line segment
545 * @param y2 the Y coordinate of the end point of the specified
546 * line segment
547 * @return <code>true</code> if the specified line segment intersects
548 * the interior of this <code>Rectangle2D</code>; <code>false</code>
549 * otherwise.
550 * @since 1.2
551 */
552 public boolean intersectsLine(double x1, double y1, double x2,
553 double y2) {
554 int out1, out2;
555 if ((out2 = outcode(x2, y2)) == 0) {
556 return true;
557 }
558 while ((out1 = outcode(x1, y1)) != 0) {
559 if ((out1 & out2) != 0) {
560 return false;
561 }
562 if ((out1 & (OUT_LEFT | OUT_RIGHT)) != 0) {
563 double x = getX();
564 if ((out1 & OUT_RIGHT) != 0) {
565 x += getWidth();
566 }
567 y1 = y1 + (x - x1) * (y2 - y1) / (x2 - x1);
568 x1 = x;
569 } else {
570 double y = getY();
571 if ((out1 & OUT_BOTTOM) != 0) {
572 y += getHeight();
573 }
574 x1 = x1 + (y - y1) * (x2 - x1) / (y2 - y1);
575 y1 = y;
576 }
577 }
578 return true;
579 }
580
581 /**
582 * Tests if the specified line segment intersects the interior of this
583 * <code>Rectangle2D</code>.
584 * @param l the specified {@link Line2D} to test for intersection
585 * with the interior of this <code>Rectangle2D</code>
586 * @return <code>true</code> if the specified <code>Line2D</code>
587 * intersects the interior of this <code>Rectangle2D</code>;
588 * <code>false</code> otherwise.
589 * @since 1.2
590 */
591 public boolean intersectsLine(Line2D l) {
592 return intersectsLine(l.getX1(), l.getY1(), l.getX2(), l
593 .getY2());
594 }
595
596 /**
597 * Determines where the specified coordinates lie with respect
598 * to this <code>Rectangle2D</code>.
599 * This method computes a binary OR of the appropriate mask values
600 * indicating, for each side of this <code>Rectangle2D</code>,
601 * whether or not the specified coordinates are on the same side
602 * of the edge as the rest of this <code>Rectangle2D</code>.
603 * @param x the specified X coordinate
604 * @param y the specified Y coordinate
605 * @return the logical OR of all appropriate out codes.
606 * @see #OUT_LEFT
607 * @see #OUT_TOP
608 * @see #OUT_RIGHT
609 * @see #OUT_BOTTOM
610 * @since 1.2
611 */
612 public abstract int outcode(double x, double y);
613
614 /**
615 * Determines where the specified {@link Point2D} lies with
616 * respect to this <code>Rectangle2D</code>.
617 * This method computes a binary OR of the appropriate mask values
618 * indicating, for each side of this <code>Rectangle2D</code>,
619 * whether or not the specified <code>Point2D</code> is on the same
620 * side of the edge as the rest of this <code>Rectangle2D</code>.
621 * @param p the specified <code>Point2D</code>
622 * @return the logical OR of all appropriate out codes.
623 * @see #OUT_LEFT
624 * @see #OUT_TOP
625 * @see #OUT_RIGHT
626 * @see #OUT_BOTTOM
627 * @since 1.2
628 */
629 public int outcode(Point2D p) {
630 return outcode(p.getX(), p.getY());
631 }
632
633 /**
634 * Sets the location and size of the outer bounds of this
635 * <code>Rectangle2D</code> to the specified rectangular values.
636 *
637 * @param x the X coordinate of the upper-left corner
638 * of this <code>Rectangle2D</code>
639 * @param y the Y coordinate of the upper-left corner
640 * of this <code>Rectangle2D</code>
641 * @param w the width of this <code>Rectangle2D</code>
642 * @param h the height of this <code>Rectangle2D</code>
643 * @since 1.2
644 */
645 public void setFrame(double x, double y, double w, double h) {
646 setRect(x, y, w, h);
647 }
648
649 /**
650 * {@inheritDoc}
651 * @since 1.2
652 */
653 public Rectangle2D getBounds2D() {
654 return (Rectangle2D) clone();
655 }
656
657 /**
658 * {@inheritDoc}
659 * @since 1.2
660 */
661 public boolean contains(double x, double y) {
662 double x0 = getX();
663 double y0 = getY();
664 return (x >= x0 && y >= y0 && x < x0 + getWidth() && y < y0
665 + getHeight());
666 }
667
668 /**
669 * {@inheritDoc}
670 * @since 1.2
671 */
672 public boolean intersects(double x, double y, double w, double h) {
673 if (isEmpty() || w <= 0 || h <= 0) {
674 return false;
675 }
676 double x0 = getX();
677 double y0 = getY();
678 return (x + w > x0 && y + h > y0 && x < x0 + getWidth() && y < y0
679 + getHeight());
680 }
681
682 /**
683 * {@inheritDoc}
684 * @since 1.2
685 */
686 public boolean contains(double x, double y, double w, double h) {
687 if (isEmpty() || w <= 0 || h <= 0) {
688 return false;
689 }
690 double x0 = getX();
691 double y0 = getY();
692 return (x >= x0 && y >= y0 && (x + w) <= x0 + getWidth() && (y + h) <= y0
693 + getHeight());
694 }
695
696 /**
697 * Returns a new <code>Rectangle2D</code> object representing the
698 * intersection of this <code>Rectangle2D</code> with the specified
699 * <code>Rectangle2D</code>.
700 * @param r the <code>Rectangle2D</code> to be intersected with
701 * this <code>Rectangle2D</code>
702 * @return the largest <code>Rectangle2D</code> contained in both
703 * the specified <code>Rectangle2D</code> and in this
704 * <code>Rectangle2D</code>.
705 * @since 1.2
706 */
707 public abstract Rectangle2D createIntersection(Rectangle2D r);
708
709 /**
710 * Intersects the pair of specified source <code>Rectangle2D</code>
711 * objects and puts the result into the specified destination
712 * <code>Rectangle2D</code> object. One of the source rectangles
713 * can also be the destination to avoid creating a third Rectangle2D
714 * object, but in this case the original points of this source
715 * rectangle will be overwritten by this method.
716 * @param src1 the first of a pair of <code>Rectangle2D</code>
717 * objects to be intersected with each other
718 * @param src2 the second of a pair of <code>Rectangle2D</code>
719 * objects to be intersected with each other
720 * @param dest the <code>Rectangle2D</code> that holds the
721 * results of the intersection of <code>src1</code> and
722 * <code>src2</code>
723 * @since 1.2
724 */
725 public static void intersect(Rectangle2D src1, Rectangle2D src2,
726 Rectangle2D dest) {
727 double x1 = Math.max(src1.getMinX(), src2.getMinX());
728 double y1 = Math.max(src1.getMinY(), src2.getMinY());
729 double x2 = Math.min(src1.getMaxX(), src2.getMaxX());
730 double y2 = Math.min(src1.getMaxY(), src2.getMaxY());
731 dest.setFrame(x1, y1, x2 - x1, y2 - y1);
732 }
733
734 /**
735 * Returns a new <code>Rectangle2D</code> object representing the
736 * union of this <code>Rectangle2D</code> with the specified
737 * <code>Rectangle2D</code>.
738 * @param r the <code>Rectangle2D</code> to be combined with
739 * this <code>Rectangle2D</code>
740 * @return the smallest <code>Rectangle2D</code> containing both
741 * the specified <code>Rectangle2D</code> and this
742 * <code>Rectangle2D</code>.
743 * @since 1.2
744 */
745 public abstract Rectangle2D createUnion(Rectangle2D r);
746
747 /**
748 * Unions the pair of source <code>Rectangle2D</code> objects
749 * and puts the result into the specified destination
750 * <code>Rectangle2D</code> object. One of the source rectangles
751 * can also be the destination to avoid creating a third Rectangle2D
752 * object, but in this case the original points of this source
753 * rectangle will be overwritten by this method.
754 * @param src1 the first of a pair of <code>Rectangle2D</code>
755 * objects to be combined with each other
756 * @param src2 the second of a pair of <code>Rectangle2D</code>
757 * objects to be combined with each other
758 * @param dest the <code>Rectangle2D</code> that holds the
759 * results of the union of <code>src1</code> and
760 * <code>src2</code>
761 * @since 1.2
762 */
763 public static void union(Rectangle2D src1, Rectangle2D src2,
764 Rectangle2D dest) {
765 double x1 = Math.min(src1.getMinX(), src2.getMinX());
766 double y1 = Math.min(src1.getMinY(), src2.getMinY());
767 double x2 = Math.max(src1.getMaxX(), src2.getMaxX());
768 double y2 = Math.max(src1.getMaxY(), src2.getMaxY());
769 dest.setFrameFromDiagonal(x1, y1, x2, y2);
770 }
771
772 /**
773 * Adds a point, specified by the double precision arguments
774 * <code>newx</code> and <code>newy</code>, to this
775 * <code>Rectangle2D</code>. The resulting <code>Rectangle2D</code>
776 * is the smallest <code>Rectangle2D</code> that
777 * contains both the original <code>Rectangle2D</code> and the
778 * specified point.
779 * <p>
780 * After adding a point, a call to <code>contains</code> with the
781 * added point as an argument does not necessarily return
782 * <code>true</code>. The <code>contains</code> method does not
783 * return <code>true</code> for points on the right or bottom
784 * edges of a rectangle. Therefore, if the added point falls on
785 * the left or bottom edge of the enlarged rectangle,
786 * <code>contains</code> returns <code>false</code> for that point.
787 * @param newx the X coordinate of the new point
788 * @param newy the Y coordinate of the new point
789 * @since 1.2
790 */
791 public void add(double newx, double newy) {
792 double x1 = Math.min(getMinX(), newx);
793 double x2 = Math.max(getMaxX(), newx);
794 double y1 = Math.min(getMinY(), newy);
795 double y2 = Math.max(getMaxY(), newy);
796 setRect(x1, y1, x2 - x1, y2 - y1);
797 }
798
799 /**
800 * Adds the <code>Point2D</code> object <code>pt</code> to this
801 * <code>Rectangle2D</code>.
802 * The resulting <code>Rectangle2D</code> is the smallest
803 * <code>Rectangle2D</code> that contains both the original
804 * <code>Rectangle2D</code> and the specified <code>Point2D</code>.
805 * <p>
806 * After adding a point, a call to <code>contains</code> with the
807 * added point as an argument does not necessarily return
808 * <code>true</code>. The <code>contains</code>
809 * method does not return <code>true</code> for points on the right
810 * or bottom edges of a rectangle. Therefore, if the added point falls
811 * on the left or bottom edge of the enlarged rectangle,
812 * <code>contains</code> returns <code>false</code> for that point.
813 * @param pt the new <code>Point2D</code> to add to this
814 * <code>Rectangle2D</code>.
815 * @since 1.2
816 */
817 public void add(Point2D pt) {
818 add(pt.getX(), pt.getY());
819 }
820
821 /**
822 * Adds a <code>Rectangle2D</code> object to this
823 * <code>Rectangle2D</code>. The resulting <code>Rectangle2D</code>
824 * is the union of the two <code>Rectangle2D</code> objects.
825 * @param r the <code>Rectangle2D</code> to add to this
826 * <code>Rectangle2D</code>.
827 * @since 1.2
828 */
829 public void add(Rectangle2D r) {
830 double x1 = Math.min(getMinX(), r.getMinX());
831 double x2 = Math.max(getMaxX(), r.getMaxX());
832 double y1 = Math.min(getMinY(), r.getMinY());
833 double y2 = Math.max(getMaxY(), r.getMaxY());
834 setRect(x1, y1, x2 - x1, y2 - y1);
835 }
836
837 /**
838 * Returns an iteration object that defines the boundary of this
839 * <code>Rectangle2D</code>.
840 * The iterator for this class is multi-threaded safe, which means
841 * that this <code>Rectangle2D</code> class guarantees that
842 * modifications to the geometry of this <code>Rectangle2D</code>
843 * object do not affect any iterations of that geometry that
844 * are already in process.
845 * @param at an optional <code>AffineTransform</code> to be applied to
846 * the coordinates as they are returned in the iteration, or
847 * <code>null</code> if untransformed coordinates are desired
848 * @return the <code>PathIterator</code> object that returns the
849 * geometry of the outline of this
850 * <code>Rectangle2D</code>, one segment at a time.
851 * @since 1.2
852 */
853 public PathIterator getPathIterator(AffineTransform at) {
854 return new RectIterator(this , at);
855 }
856
857 /**
858 * Returns an iteration object that defines the boundary of the
859 * flattened <code>Rectangle2D</code>. Since rectangles are already
860 * flat, the <code>flatness</code> parameter is ignored.
861 * The iterator for this class is multi-threaded safe, which means
862 * that this <code>Rectangle2D</code> class guarantees that
863 * modifications to the geometry of this <code>Rectangle2D</code>
864 * object do not affect any iterations of that geometry that
865 * are already in process.
866 * @param at an optional <code>AffineTransform</code> to be applied to
867 * the coordinates as they are returned in the iteration, or
868 * <code>null</code> if untransformed coordinates are desired
869 * @param flatness the maximum distance that the line segments used to
870 * approximate the curved segments are allowed to deviate from any
871 * point on the original curve. Since rectangles are already flat,
872 * the <code>flatness</code> parameter is ignored.
873 * @return the <code>PathIterator</code> object that returns the
874 * geometry of the outline of this
875 * <code>Rectangle2D</code>, one segment at a time.
876 * @since 1.2
877 */
878 public PathIterator getPathIterator(AffineTransform at,
879 double flatness) {
880 return new RectIterator(this , at);
881 }
882
883 /**
884 * Returns the hashcode for this <code>Rectangle2D</code>.
885 * @return the hashcode for this <code>Rectangle2D</code>.
886 * @since 1.2
887 */
888 public int hashCode() {
889 long bits = java.lang.Double.doubleToLongBits(getX());
890 bits += java.lang.Double.doubleToLongBits(getY()) * 37;
891 bits += java.lang.Double.doubleToLongBits(getWidth()) * 43;
892 bits += java.lang.Double.doubleToLongBits(getHeight()) * 47;
893 return (((int) bits) ^ ((int) (bits >> 32)));
894 }
895
896 /**
897 * Determines whether or not the specified <code>Object</code> is
898 * equal to this <code>Rectangle2D</code>. The specified
899 * <code>Object</code> is equal to this <code>Rectangle2D</code>
900 * if it is an instance of <code>Rectangle2D</code> and if its
901 * location and size are the same as this <code>Rectangle2D</code>.
902 * @param obj an <code>Object</code> to be compared with this
903 * <code>Rectangle2D</code>.
904 * @return <code>true</code> if <code>obj</code> is an instance
905 * of <code>Rectangle2D</code> and has
906 * the same values; <code>false</code> otherwise.
907 * @since 1.2
908 */
909 public boolean equals(Object obj) {
910 if (obj == this ) {
911 return true;
912 }
913 if (obj instanceof Rectangle2D) {
914 Rectangle2D r2d = (Rectangle2D) obj;
915 return ((getX() == r2d.getX()) && (getY() == r2d.getY())
916 && (getWidth() == r2d.getWidth()) && (getHeight() == r2d
917 .getHeight()));
918 }
919 return false;
920 }
921 }
|