home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / geom / Rectangle2D.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  20.6 KB  |  782 lines

  1. /*
  2.  * @(#)Rectangle2D.java    1.11 98/03/18
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt.geom;
  16.  
  17. /**
  18.  * A rectangle defined by a location (x, y) and dimension (w x h).
  19.  * <p>
  20.  * This class is only the abstract superclass for all objects which
  21.  * store a 2D rectangle.
  22.  * The actual storage representation of the coordinates is left to
  23.  * the subclass.
  24.  *
  25.  * @version 10 Feb 1997
  26.  * @author    Jim Graham
  27.  */
  28. public abstract class Rectangle2D extends RectangularShape {
  29.     /**
  30.      * The bitmask which indicates that a point lies to the left of
  31.      * this Rectangle.
  32.      * @since JDK1.2
  33.      */
  34.     public static final int OUT_LEFT = 1;
  35.  
  36.     /**
  37.      * The bitmask which indicates that a point lies above
  38.      * this Rectangle.
  39.      * @since JDK1.2
  40.      */
  41.     public static final int OUT_TOP = 2;
  42.  
  43.     /**
  44.      * The bitmask which indicates that a point lies to the right of
  45.      * this Rectangle.
  46.      * @since JDK1.2
  47.      */
  48.     public static final int OUT_RIGHT = 4;
  49.  
  50.     /**
  51.      * The bitmask which indicates that a point lies below
  52.      * this Rectangle.
  53.      * @since JDK1.2
  54.      */
  55.     public static final int OUT_BOTTOM = 8;
  56.  
  57.     /**
  58.      * A rectangle specified in float coordinates.
  59.      * @since JDK1.2
  60.      */
  61.     public static class Float extends Rectangle2D {
  62.     /**
  63.      * The x coordinate of the rectangle.
  64.      * @since JDK1.2
  65.      */
  66.     public float x;
  67.  
  68.     /**
  69.      * The y coordinate of the rectangle.
  70.      * @since JDK1.2
  71.      */
  72.     public float y;
  73.  
  74.     /**
  75.      * The width of the rectangle.
  76.      * @since JDK1.2
  77.      */
  78.     public float width;
  79.  
  80.     /**
  81.      * The height of the rectangle.
  82.      * @since JDK1.2
  83.      */
  84.     public float height;
  85.  
  86.     /**
  87.      * Constructs a new rectangle, initialized to location (0.0, 0.0) and
  88.      * size (0.0, 0.0).
  89.      * @since JDK1.2
  90.      */
  91.     public Float() {
  92.     }
  93.  
  94.     /**
  95.      * Constructs and initializes a rectangle from the specified
  96.      * float coordinates.
  97.      * @param x the x coordinate
  98.      * @param y the y coordinate
  99.      * @param width the width of the rectangle
  100.      * @param height the height of the rectangle
  101.      * @since JDK1.2
  102.     */
  103.     public Float(float x, float y, float w, float h) {
  104.         setRect(x, y, w, h);
  105.     }
  106.  
  107.     /**
  108.      * Returns the X coordinate of the rectangle in double precision.
  109.      * @since JDK1.2
  110.      */
  111.     public double getX() {
  112.         return (double) x;
  113.     }
  114.  
  115.     /**
  116.      * Returns the Y coordinate of the rectangle in double precision.
  117.      * @since JDK1.2
  118.      */
  119.     public double getY() {
  120.         return (double) y;
  121.     }
  122.  
  123.     /**
  124.      * Returns the width of the rectangle in double precision.
  125.      * @since JDK1.2
  126.      */
  127.     public double getWidth() {
  128.         return (double) width;
  129.     }
  130.  
  131.     /**
  132.      * Returns the height of the rectangle in double precision.
  133.      * @since JDK1.2
  134.      */
  135.     public double getHeight() {
  136.         return (double) height;
  137.     }
  138.  
  139.     /**
  140.      * Determines whether the rectangle is empty.
  141.      * @since JDK1.2
  142.      */
  143.     public boolean isEmpty() {
  144.         return (width <= 0.0f) || (height <= 0.0f);
  145.     }
  146.  
  147.     /**
  148.      * Sets the location and size of this rectangle to the specified
  149.      * float values.
  150.      * @since JDK1.2
  151.      */
  152.     public void setRect(float x, float y, float w, float h) {
  153.         this.x = x;
  154.         this.y = y;
  155.         this.width = w;
  156.         this.height = h;
  157.     }
  158.  
  159.     /**
  160.      * Sets the location and size of this rectangle to the specified
  161.      * double values.
  162.      * @since JDK1.2
  163.      */
  164.     public void setRect(double x, double y, double w, double h) {
  165.         this.x = (float) x;
  166.         this.y = (float) y;
  167.         this.width = (float) w;
  168.         this.height = (float) h;
  169.     }
  170.  
  171.     /**
  172.      * Sets this rectangle to be the same as the specified Rectangle.
  173.      * @since JDK1.2
  174.      */
  175.     public void setRect(Rectangle2D r) {
  176.         this.x = (float) r.getX();
  177.         this.y = (float) r.getY();
  178.         this.width = (float) r.getWidth();
  179.         this.height = (float) r.getHeight();
  180.     }
  181.  
  182.     /**
  183.      * Determines where the specified float coordinates lie with respect
  184.      * to this Rectangle.
  185.      * This method computes a binary OR of the appropriate mask values
  186.      * indicating which sides of the rectangle the given point is
  187.      * outside of.
  188.      * @return the logical OR of all appropriate out codes
  189.      * @see Rectangle2D#OUT_LEFT
  190.      * @see Rectangle2D#OUT_TOP
  191.      * @see Rectangle2D#OUT_RIGHT
  192.      * @see Rectangle2D#OUT_BOTTOM
  193.      * @since JDK1.2
  194.      */
  195.     public int outcode(double x, double y) {
  196.         int out = 0;
  197.         if (this.width <= 0) {
  198.         out |= OUT_LEFT | OUT_RIGHT;
  199.         } else if (x < this.x) {
  200.         out |= OUT_LEFT;
  201.         } else if (x > this.x + this.width) {
  202.         out |= OUT_RIGHT;
  203.         }
  204.         if (this.height <= 0) {
  205.         out |= OUT_TOP | OUT_BOTTOM;
  206.         } else if (y < this.y) {
  207.         out |= OUT_TOP;
  208.         } else if (y > this.y + this.height) {
  209.         out |= OUT_BOTTOM;
  210.         }
  211.         return out;
  212.     }
  213.  
  214.     /**
  215.      * Return a new Rectangle2D object representing the intersection of 
  216.      * this rectangle with the given rectangle.
  217.      * @param     r   the rectangle to be intersected with
  218.      * @return    the largest rectangle contained in both the 
  219.      *            specified rectangle and in this rectangle
  220.      * @since JDK1.2
  221.      */
  222.     public Rectangle2D createIntersection(Rectangle2D r) {
  223.         Rectangle2D dest;
  224.         if (r instanceof Float) {
  225.         dest = new Rectangle2D.Float();
  226.         } else {
  227.         dest = new Rectangle2D.Double();
  228.         }
  229.         Rectangle2D.intersect(this, r, dest);
  230.         return dest;
  231.     }
  232.  
  233.     /**
  234.      * Return a new Rectangle2D object representing the union of 
  235.      * this rectangle with the given rectangle.
  236.      * @param     r   the rectangle to be combined with
  237.      * @return    the smallest rectangle containing both the specified 
  238.      *            rectangle and this rectangle.
  239.      * @since JDK1.2
  240.      */
  241.     public Rectangle2D createUnion(Rectangle2D r) {
  242.         Rectangle2D dest;
  243.         if (r instanceof Float) {
  244.         dest = new Rectangle2D.Float();
  245.         } else {
  246.         dest = new Rectangle2D.Double();
  247.         }
  248.         Rectangle2D.union(this, r, dest);
  249.         return dest;
  250.     }
  251.  
  252.     /**
  253.      * Returns the String representation of this Rectangle.
  254.      * @since JDK1.2
  255.      */
  256.     public String toString() {
  257.         return getClass().getName()
  258.         + "[x=" + x +
  259.         ",y=" + y +
  260.         ",w=" + width +
  261.         ",h=" + height + "]";
  262.     }
  263.     }
  264.  
  265.     /**
  266.      * A rectangle specified in double coordinates.
  267.      * @since JDK1.2
  268.      */
  269.     public static class Double extends Rectangle2D {
  270.     /**
  271.      * The x coordinate of the rectangle.
  272.      * @since JDK1.2
  273.      */
  274.     public double x;
  275.  
  276.     /**
  277.      * The y coordinate of the rectangle.
  278.      * @since JDK1.2
  279.      */
  280.     public double y;
  281.  
  282.     /**
  283.      * The width of the rectangle.
  284.      * @since JDK1.2
  285.      */
  286.     public double width;
  287.  
  288.     /**
  289.      * The height of the rectangle.
  290.      * @since JDK1.2
  291.      */
  292.     public double height;
  293.  
  294.     /**
  295.      * Constructs a new rectangle, initialized to location (0, 0) and
  296.      * size (0, 0).
  297.      * @since JDK1.2
  298.      */
  299.     public Double() {
  300.     }
  301.  
  302.     /**
  303.      * Constructs and initializes a rectangle from the specified
  304.      * double coordinates.
  305.      * @param x the x coordinate
  306.      * @param y the y coordinate
  307.      * @param width the width of the rectangle
  308.      * @param height the height of the rectangle
  309.      * @since JDK1.2
  310.      */
  311.     public Double(double x, double y, double w, double h) {
  312.         setRect(x, y, w, h);
  313.     }
  314.  
  315.     /**
  316.      * Returns the X coordinate of the rectangle in double precision.
  317.      * @since JDK1.2
  318.      */
  319.     public double getX() {
  320.         return x;
  321.     }
  322.  
  323.     /**
  324.      * Returns the Y coordinate of the rectangle in double precision.
  325.      * @since JDK1.2
  326.      */
  327.     public double getY() {
  328.         return y;
  329.     }
  330.  
  331.     /**
  332.      * Returns the width of the rectangle in double precision.
  333.      * @since JDK1.2
  334.      */
  335.     public double getWidth() {
  336.         return width;
  337.     }
  338.  
  339.     /**
  340.      * Returns the height of the rectangle in double precision.
  341.      * @since JDK1.2
  342.      */
  343.     public double getHeight() {
  344.         return height;
  345.     }
  346.  
  347.     /**
  348.      * Determines whether the rectangle is empty.
  349.      * @since JDK1.2
  350.      */
  351.     public boolean isEmpty() {
  352.         return (width <= 0.0) || (height <= 0.0);
  353.     }
  354.  
  355.     /**
  356.      * Sets the location and size of this rectangle to the specified
  357.      * double values.
  358.      * @since JDK1.2
  359.      */
  360.     public void setRect(double x, double y, double w, double h) {
  361.         this.x = x;
  362.         this.y = y;
  363.         this.width = w;
  364.         this.height = h;
  365.     }
  366.  
  367.     /**
  368.      * Sets this rectangle to be the same as the specified Rectangle.
  369.      * @since JDK1.2
  370.      */
  371.     public void setRect(Rectangle2D r) {
  372.         this.x = r.getX();
  373.         this.y = r.getY();
  374.         this.width = r.getWidth();
  375.         this.height = r.getHeight();
  376.     }
  377.  
  378.     /**
  379.      * Determines where the specified double coordinates lie with respect
  380.      * to this Rectangle.
  381.      * This method computes a binary OR of the appropriate mask values
  382.      * indicating which sides of the rectangle the given point is
  383.      * outside of.
  384.      * @return the logical OR of all appropriate out codes
  385.      * @see Rectangle2D#OUT_LEFT
  386.      * @see Rectangle2D#OUT_TOP
  387.      * @see Rectangle2D#OUT_RIGHT
  388.      * @see Rectangle2D#OUT_BOTTOM
  389.      * @since JDK1.2
  390.      */
  391.     public int outcode(double x, double y) {
  392.         int out = 0;
  393.         if (this.width <= 0) {
  394.         out |= OUT_LEFT | OUT_RIGHT;
  395.         } else if (x < this.x) {
  396.         out |= OUT_LEFT;
  397.         } else if (x > this.x + this.width) {
  398.         out |= OUT_RIGHT;
  399.         }
  400.         if (this.height <= 0) {
  401.         out |= OUT_TOP | OUT_BOTTOM;
  402.         } else if (y < this.y) {
  403.         out |= OUT_TOP;
  404.         } else if (y > this.y + this.height) {
  405.         out |= OUT_BOTTOM;
  406.         }
  407.         return out;
  408.     }
  409.  
  410.     /**
  411.      * Return a new Rectangle2D object representing the intersection of 
  412.      * this rectangle with the given rectangle.
  413.      * @param     r   the rectangle to be intersected with
  414.      * @return    the largest rectangle contained in both the 
  415.      *            specified rectangle and in this rectangle
  416.      * @since JDK1.2
  417.      */
  418.     public Rectangle2D createIntersection(Rectangle2D r) {
  419.         Rectangle2D dest = new Rectangle2D.Double();
  420.         Rectangle2D.intersect(this, r, dest);
  421.         return dest;
  422.     }
  423.  
  424.     /**
  425.      * Return a new Rectangle2D object representing the union of 
  426.      * this rectangle with the given rectangle.
  427.      * @param     r   the rectangle to be combined with
  428.      * @return    the smallest rectangle containing both the specified 
  429.      *            rectangle and this rectangle.
  430.      * @since JDK1.2
  431.      */
  432.     public Rectangle2D createUnion(Rectangle2D r) {
  433.         Rectangle2D dest = new Rectangle2D.Double();
  434.         Rectangle2D.union(this, r, dest);
  435.         return dest;
  436.     }
  437.  
  438.     /**
  439.      * Returns the String representation of this Rectangle.
  440.      * @since JDK1.2
  441.      */
  442.     public String toString() {
  443.         return getClass().getName()
  444.         + "[x=" + x +
  445.         ",y=" + y +
  446.         ",w=" + width +
  447.         ",h=" + height + "]";
  448.     }
  449.     }
  450.  
  451.     protected Rectangle2D() {
  452.     }
  453.  
  454.     /**
  455.      * Sets the location and size of this rectangle to the specified
  456.      * double values.
  457.      * @since JDK1.2
  458.      */
  459.     public abstract void setRect(double x, double y, double w, double h);
  460.  
  461.     /**
  462.      * Sets this rectangle to be the same as the specified Rectangle.
  463.      * @since JDK1.2
  464.      */
  465.     public void setRect(Rectangle2D r) {
  466.     setRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
  467.     }
  468.  
  469.     /**
  470.      * Tests if the given line segment intersects the interior of this
  471.      * Rectangle.
  472.      * @since JDK1.2
  473.      */
  474.     public boolean intersectsLine(double x1, double y1, double x2, double y2) {
  475.     int out1 = outcode(x1, y1);
  476.     int out2 = outcode(x2, y2);
  477.     while (true) {
  478.         if ((out1 & out2) != 0) {
  479.         return false;
  480.         }
  481.         if (out1 == 0) {
  482.         if (out2 == 0) {
  483.             // Both are inside, line intersects
  484.             return true;
  485.         }
  486.         double f = x1;
  487.         x1 = x2;
  488.         x2 = f;
  489.         f = y1;
  490.         y1 = y2;
  491.         y2 = f;
  492.         int out = out1;
  493.         out1 = out2;
  494.         out2 = out;
  495.         }
  496.         if ((out1 & (OUT_LEFT | OUT_RIGHT)) != 0) {
  497.         double x = getX();
  498.         if ((out1 & OUT_RIGHT) != 0) {
  499.             x += getWidth();
  500.         }
  501.         y1 = y1 + (x - x1) * (y2 - y1) / (x2 - x1);
  502.         x1 = x;
  503.         } else if ((out1 & (OUT_TOP | OUT_BOTTOM)) != 0) {
  504.         double y = getY();
  505.         if ((out1 & OUT_BOTTOM) != 0) {
  506.             y += getHeight();
  507.         }
  508.         x1 = x1 + (y - y1) * (x2 - x1) / (y2 - y1);
  509.         y1 = y;
  510.         }
  511.         out1 = outcode(x1, y1);
  512.     }
  513.     }
  514.  
  515.     /**
  516.      * Tests if the given line segment intersects the interior of this
  517.      * Rectangle.
  518.      * @since JDK1.2
  519.      */
  520.     public boolean intersectsLine(Line2D l) {
  521.     return intersectsLine(l.getX1(), l.getY1(), l.getX2(), l.getY2());
  522.     }
  523.  
  524.     /**
  525.      * Determines where the specified coordinates lie with respect
  526.      * to this Rectangle.
  527.      * This method computes a binary OR of the appropriate mask values
  528.      * indicating which sides of the rectangle the given point is
  529.      * outside of.
  530.      * @return the logical OR of all appropriate out codes
  531.      * @see #OUT_LEFT
  532.      * @see #OUT_TOP
  533.      * @see #OUT_RIGHT
  534.      * @see #OUT_BOTTOM
  535.      * @since JDK1.2
  536.      */
  537.     public abstract int outcode(double x, double y);
  538.  
  539.     /**
  540.      * Determines where the specified point lies with respect to this
  541.      * Rectangle.
  542.      * This method computes a binary OR of the appropriate mask values
  543.      * indicating which sides of the rectangle the given point is
  544.      * outside of.
  545.      * @return the logical OR of all appropriate out codes
  546.      * @see #OUT_LEFT
  547.      * @see #OUT_TOP
  548.      * @see #OUT_RIGHT
  549.      * @see #OUT_BOTTOM
  550.      * @since JDK1.2
  551.      */
  552.     public int outcode(Point2D p) {
  553.     return outcode(p.getX(), p.getY());
  554.     }
  555.  
  556.     /**
  557.      * Sets the location and size of the outer bounds of this shape
  558.      * to the specified rectangular values.
  559.      * @since JDK1.2
  560.      */
  561.     public void setBounds(double x, double y, double w, double h) {
  562.     setRect(x, y, w, h);
  563.     }
  564.  
  565.     /**
  566.      * Return the high precision bounding box of the shape.
  567.      * @since JDK1.2
  568.      */
  569.     public Rectangle2D getBounds2D() {
  570.     return this;
  571.     }
  572.  
  573.     /**
  574.      * Test if a given coordinate is inside the boundary of the shape.
  575.      * @since JDK1.2
  576.      */
  577.     public boolean contains(double x, double y) {
  578.     double x0 = getX();
  579.     double y0 = getY();
  580.     return (x >= x0 &&
  581.         y >= y0 &&
  582.         x < x0 + getWidth() &&
  583.         y < y0 + getHeight());
  584.     }
  585.  
  586.     /**
  587.      * Test if the interior of the Shape intersects the interior of a given
  588.      * set of rectangular coordinates.
  589.      * @since JDK1.2
  590.      */
  591.     public boolean intersects(double x, double y, double w, double h) {
  592.     if (isEmpty() || w <= 0 || h <= 0) {
  593.         return false;
  594.     }
  595.     double x0 = getX();
  596.     double y0 = getY();
  597.     return (x + w > x0 &&
  598.         y + h > y0 &&
  599.         x < x0 + getWidth() &&
  600.         y < y0 + getHeight());
  601.     }
  602.  
  603.     /**
  604.      * Test if the interior of the Shape entirely contains the given
  605.      * set of rectangular coordinates.
  606.      * @since JDK1.2
  607.      */
  608.     public boolean contains(double x, double y, double w, double h) {
  609.     if (isEmpty() || w <= 0 || h <= 0) {
  610.         return false;
  611.     }
  612.     double x0 = getX();
  613.     double y0 = getY();
  614.     return (x >= x0 &&
  615.         y >= y0 &&
  616.         (x + w) <= x0 + getWidth() &&
  617.         (y + h) <= y0 + getHeight());
  618.     }
  619.  
  620.     /**
  621.      * Return a new Rectangle2D object representing the intersection of 
  622.      * this rectangle with the given rectangle.
  623.      * @param     r   the rectangle to be intersected with
  624.      * @return    the largest rectangle contained in both the 
  625.      *            specified rectangle and in this rectangle
  626.      * @since JDK1.2
  627.      */
  628.     public abstract Rectangle2D createIntersection(Rectangle2D r);
  629.  
  630.     /**
  631.      * Intersect the pair of Rectangles and put the result into
  632.      * the indicated destination Rectangle2D object.
  633.      * @param src1 the first rectangle to be intersected
  634.      * @param src2 the second rectangle to be intersected
  635.      * @param dest the rectangle to store the results in
  636.      * @since JDK1.2
  637.      */
  638.     public static void intersect(Rectangle2D src1,
  639.                  Rectangle2D src2,
  640.                  Rectangle2D dest) {
  641.     double x1 = Math.max(src1.getLeft(), src2.getLeft());
  642.     double y1 = Math.max(src1.getTop(), src2.getTop());
  643.     double x2 = Math.min(src1.getRight(), src2.getRight());
  644.     double y2 = Math.min(src1.getBottom(), src2.getBottom());
  645.     dest.setBoundsFromDiagonal(x1, y1, x2, y2);
  646.     }
  647.       
  648.     /**
  649.      * Return a new Rectangle2D object representing the union of 
  650.      * this rectangle with the given rectangle.
  651.      * @param     r   the rectangle to be combined with
  652.      * @return    the smallest rectangle containing both the specified 
  653.      *            rectangle and this rectangle.
  654.      * @since JDK1.2
  655.      */
  656.     public abstract Rectangle2D createUnion(Rectangle2D r);
  657.  
  658.     /**
  659.      * Union the pair of Rectangles and put the result into
  660.      * the indicated destination Rectangle2D object.
  661.      * @param src1 the first rectangle to be combined
  662.      * @param src2 the second rectangle to be combined
  663.      * @param dest the rectangle to store the results in
  664.      * @since JDK1.2
  665.      */
  666.     public static void union(Rectangle2D src1,
  667.                  Rectangle2D src2,
  668.                  Rectangle2D dest) {
  669.     double x1 = Math.min(src1.getLeft(), src2.getLeft());
  670.     double y1 = Math.min(src1.getTop(), src2.getTop());
  671.     double x2 = Math.max(src1.getRight(), src2.getRight());
  672.     double y2 = Math.max(src1.getBottom(), src2.getBottom());
  673.     dest.setBoundsFromDiagonal(x1, y1, x2, y2);
  674.     }
  675.       
  676.     /**
  677.      * Adds a point, specified by the double precision arguments
  678.      * <code>newx</code> and <code>newy</code>, to this rectangle.
  679.      * The resulting rectangle is the smallest rectangle that
  680.      * contains both the original rectangle and the specified point.
  681.      * <p>
  682.      * After adding a point, a call to <code>contains<code> with the 
  683.      * added point as an argument will not necessarily return 
  684.      * <code>true</code>. The <code>contains</code> method does not 
  685.      * return <code>true</code> for points on the right or bottom 
  686.      * edges of a rectangle. Therefore if the added point falls on 
  687.      * the left or bottom edge of the enlarged rectangle, 
  688.      * <code>contains</code> will return <code>false</code> for that point.
  689.      * 
  690.      * @param     newx   the <i>x</i> coordinate of the new point.
  691.      * @param     newy   the <i>y</i> coordinate of the new point.
  692.      * @since     JDK1.0
  693.      */
  694.     public void add(double newx, double newy) {
  695.     double x1 = Math.min(getLeft(), newx);
  696.     double x2 = Math.max(getRight(), newx);
  697.     double y1 = Math.min(getTop(), newy);
  698.     double y2 = Math.max(getBottom(), newy);
  699.     setRect(x1, y1, x2 - x1, y2 - y1);
  700.     }
  701.  
  702.     /**
  703.      * Adds the Point2D object <code>pt</code> to this rectangle.
  704.      * The resulting rectangle is the smallest rectangle that
  705.      * contains both the original rectangle and the specified point.
  706.      * <p>
  707.      * After adding a point, a call to <code>contains<code> with the 
  708.      * added point as an argument will not necessarily return 
  709.      * <code>true</code>. The <code>contains</code> method does not 
  710.      * return <code>true</code> for points on the right or bottom 
  711.      * edges of a rectangle. Therefore if the added point falls on 
  712.      * the left or bottom edge of the enlarged rectangle, 
  713.      * <code>contains</code> will return <code>false</code> for that point.
  714.      * 
  715.      * @param     pt the new point to add to the rectangle.
  716.      * @since     JDK1.0
  717.      */
  718.     public void add(Point2D pt) {
  719.     add(pt.getX(), pt.getY());
  720.     }
  721.  
  722.     /**
  723.      * Adds a Rectangle2D object to this rectangle.
  724.      * The resulting rectangle is the union of the two rectangles. 
  725.      * @param     a rectangle.
  726.      * @since     JDK1.0
  727.      */
  728.     public void add(Rectangle2D r) {
  729.     double x1 = Math.min(getLeft(), r.getLeft());
  730.     double x2 = Math.max(getRight(), r.getRight());
  731.     double y1 = Math.min(getTop(), r.getTop());
  732.     double y2 = Math.max(getBottom(), r.getBottom());
  733.     setRect(x1, y1, x2 - x1, y2 - y1);
  734.     }
  735.  
  736.     /**
  737.      * Return an iteration object that defines the boundary of the
  738.      * shape.
  739.      * @param at the AffineTransform by which to transform the points
  740.      * @since JDK1.2
  741.      */
  742.     public PathIterator getPathIterator(AffineTransform at) {
  743.     return new RectIterator(this, at);
  744.     }
  745.  
  746.     /**
  747.      * Return an iteration object that defines the boundary of the
  748.      * flattened shape.
  749.      * @param at the AffineTransform by which to transform the points
  750.      * @param flatness the maximum approximation error desired for the
  751.      * flattened path
  752.      * @since JDK1.2
  753.      */
  754.     public PathIterator getPathIterator(AffineTransform at, double flatness) {
  755.     return new RectIterator(this, at);
  756.     }
  757.  
  758.     /**
  759.      * Determines whether two rectangles are equal. Two instances of
  760.      * <code>Rectangle2D</code> are equal if their location and size
  761.      * are the same.
  762.      * @param      obj   an object to be compared with this rectangle.
  763.      * @return     <code>true</code> if the object to be compared is
  764.      *                     an instance of <code>Rectangle2D</code> and has
  765.      *                     the same values; <code>false</code> otherwise.
  766.      * @since JDK1.2
  767.      */
  768.     public boolean equals(Object obj) {
  769.     if (obj == this) {
  770.         return true;
  771.     }
  772.     if (obj != null && obj instanceof Rectangle2D) {
  773.         Rectangle2D r2d = (Rectangle2D) obj;
  774.         return ((getX() == r2d.getX()) &&
  775.             (getY() == r2d.getY()) &&
  776.             (getWidth() == r2d.getWidth()) &&
  777.             (getHeight() == r2d.getHeight()));
  778.     }
  779.     return super.equals(obj);
  780.     }
  781. }
  782.