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

  1. /*
  2.  * @(#)Rectangle.java    1.32 98/03/18
  3.  *
  4.  * Copyright 1995-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;
  16.  
  17. import java.awt.geom.Rectangle2D;
  18.  
  19. /**
  20.  * A rectangle specifies an area in a coordinate space that is 
  21.  * defined by the rectangle's top-left point (<i>x</i>, <i>y</i>) 
  22.  * in the coordinate space, its width, and its height. 
  23.  * <p>
  24.  * A rectangle's <code>width</code> and <code>height</code> are 
  25.  * public fields. The constructors that allow you to create a 
  26.  * rectangle, and the methods that allow you to modify one, do not 
  27.  * prevent you from setting a negative value for width or height. 
  28.  * <p>
  29.  * A rectangle whose width or height is negative is considered 
  30.  * empty, and all methods defined by the <code>Rectangle</code> class  
  31.  * behave accordingly. If the rectangle is empty, then the method 
  32.  * <code>isEmpty</code> returns <code>true</code>. No point can be 
  33.  * contained by or inside an empty rectangle, however the values of 
  34.  * <code>width</code> and <code>height</code> are still valid. An 
  35.  * empty rectangle still has a location in the coordinate space, and 
  36.  * methods that change its size or location remain valid. The 
  37.  * behavior of methods that operate on more than one rectangle is 
  38.  * undefined if any of the participating rectangles has a negative 
  39.  * <code>width</code> or <code>height</code>. These methods include 
  40.  * <code>intersects</code>, <code>intersection</code>, and 
  41.  * <code>union</code>. 
  42.  * specified in integer coordinates.
  43.  *
  44.  * @version     1.32, 03/18/98
  45.  * @author     Sami Shaio
  46.  * @since       JDK1.0
  47.  */
  48. public class Rectangle extends Rectangle2D
  49.     implements Shape, java.io.Serializable
  50. {
  51.  
  52.     /**
  53.      * The <i>x</i> coordinate of the rectangle.
  54.      */
  55.     public int x;
  56.  
  57.     /**
  58.      * The <i>y</i> coordinate of the rectangle.
  59.      */
  60.     public int y;
  61.  
  62.     /**
  63.      * The width of the rectangle.
  64.      * @since     JDK1.0.
  65.      */
  66.     public int width;
  67.  
  68.     /**
  69.      * The height of the rectangle.
  70.      */
  71.     public int height;
  72.  
  73.     /*
  74.      * JDK 1.1 serialVersionUID 
  75.      */
  76.      private static final long serialVersionUID = -4345857070255674764L;
  77.  
  78.     /**
  79.      * Constructs a new rectangle whose top-left corner is at (0, 0) 
  80.      * in the coordinate space, and whose width and height are zero. 
  81.      */
  82.     public Rectangle() {
  83.         this(0, 0, 0, 0);
  84.     }
  85.  
  86.     /**
  87.      * Constructs a new rectangle, initialized to match the values of
  88.      * the specificed rectangle.
  89.      * @param r  a rectangle from which to copy initial values.
  90.      * @since JDK1.1
  91.      */
  92.     public Rectangle(Rectangle r) {
  93.         this(r.x, r.y, r.width, r.height);
  94.     }
  95.  
  96.     /**
  97.      * Constructs a new rectangle whose top-left corner is specified as
  98.      * (<code>x</code>, <code>y</code>) and whose width and height 
  99.      * are specified by the arguments of the same name. 
  100.      * @param     x   the <i>x</i> coordinate.
  101.      * @param     y   the <i>y</i> coordinate.
  102.      * @param     width    the width of the rectangle.
  103.      * @param     height   the height of the rectangle.
  104.      */
  105.     public Rectangle(int x, int y, int width, int height) {
  106.     this.x = x;
  107.     this.y = y;
  108.     this.width = width;
  109.     this.height = height;
  110.     }
  111.  
  112.     /**
  113.      * Constructs a new rectangle whose top-left corner is at (0, 0) 
  114.      * in the coordinate space, and whose width and height are specified 
  115.      * by the arguments of the same name. 
  116.      * @param     width    the width of the rectangle.
  117.      * @param     height   the height of the rectangle.
  118.      */
  119.     public Rectangle(int width, int height) {
  120.     this(0, 0, width, height);
  121.     }
  122.  
  123.     /**
  124.      * Constructs a new rectangle whose top-left corner is specified 
  125.      * by the <code>point</code> argument, and whose width and height  
  126.      * are specified by the <code>dimension</code> argument. 
  127.      * @param     p   a point, the top-left corner of the rectangle.
  128.      * @param     d   a dimension, representing the width and height.
  129.      */
  130.     public Rectangle(Point p, Dimension d) {
  131.     this(p.x, p.y, d.width, d.height);
  132.     }
  133.     
  134.     /**
  135.      * Constructs a new rectangle whose top-left corner is the  
  136.      * specified point, and whose width and height are zero. 
  137.      * @param     p   the top left corner of the rectangle.
  138.      */
  139.     public Rectangle(Point p) {
  140.     this(p.x, p.y, 0, 0);
  141.     }
  142.     
  143.     /**
  144.      * Constructs a new rectangle whose top left corner is  
  145.      * (0, 0) and whose width and height are specified  
  146.      * by the <code>dimension</code> argument. 
  147.      * @param     d   a dimension, specifying width and height.
  148.      */
  149.     public Rectangle(Dimension d) {
  150.     this(0, 0, d.width, d.height);
  151.     }
  152.  
  153.     /**
  154.      * Returns the X coordinate of the bounding rectangle in double precision.
  155.      */
  156.     public double getX() {
  157.     return x;
  158.     }
  159.  
  160.     /**
  161.      * Returns the Y coordinate of the bounding rectangle in double precision.
  162.      */
  163.     public double getY() {
  164.     return y;
  165.     }
  166.  
  167.     /**
  168.      * Returns the width of the bounding rectangle in double precision.
  169.      */
  170.     public double getWidth() {
  171.     return width;
  172.     }
  173.  
  174.     /**
  175.      * Returns the height of the bounding rectangle in double precision.
  176.      */
  177.     public double getHeight() {
  178.     return height;
  179.     }
  180.  
  181.     /**
  182.      * Gets the bounding rectangle of this rectangle.
  183.      * <p>
  184.      * This method is included for completeness, to parallel the
  185.      * <code>getBounds</code> method of <code>Component</code>.
  186.      * @return    a new rectangle, equal to the bounding rectangle 
  187.      *                for this rectangle.
  188.      * @see       java.awt.Component#getBounds
  189.      * @since     JDK1.1
  190.      */
  191.     public Rectangle getBounds() {
  192.     return new Rectangle(x, y, width, height);
  193.     }    
  194.  
  195.     /**
  196.      * Sets the bounding rectangle of this rectangle to match 
  197.      * the specified rectangle.
  198.      * <p>
  199.      * This method is included for completeness, to parallel the
  200.      * <code>setBounds</code> method of <code>Component</code>.
  201.      * @param     r   a rectangle.
  202.      * @see       java.awt.Component#setBounds(java.awt.Rectangle)
  203.      * @since     JDK1.1
  204.      */
  205.     public void setBounds(Rectangle r) {
  206.     setBounds(r.x, r.y, r.width, r.height);
  207.     }
  208.  
  209.     /**
  210.      * Sets the bounding rectangle of this rectangle to the specified 
  211.      * values for <code>x</code>, <code>y</code>, <code>width</code>, 
  212.      * and <code>height</code>.
  213.      * <p>
  214.      * This method is included for completeness, to parallel the
  215.      * <code>setBounds</code> method of <code>Component</code>.
  216.      * @param     x       the new <i>x</i> coordinate for the top-left
  217.      *                    corner of this rectangle.
  218.      * @param     y       the new <i>y</i> coordinate for the top-left
  219.      *                    corner of this rectangle.
  220.      * @param     width   the new width for this rectangle.
  221.      * @param     height  the new height for this rectangle.
  222.      * @see       java.awt.Component#setBounds(int, int, int, int)
  223.      * @since     JDK1.1
  224.      */
  225.     public void setBounds(int x, int y, int width, int height) {
  226.         reshape(x, y, width, height);
  227.     }    
  228.  
  229.     /**
  230.      * Set the bounds of this rectangle to the specified x, y, width,
  231.      * and height.
  232.      * This method is included for completeness, to parallel the
  233.      * setBounds method of Component.
  234.      * @param width  the new width for the Dimension object
  235.      * @param height  the new height for the Dimension object
  236.      */
  237.     public void setRect(double x, double y, double width, double height) {
  238.     int x0 = (int) Math.floor(x);
  239.     int y0 = (int) Math.floor(y);
  240.     int x1 = (int) Math.ceil(x+width);
  241.     int y1 = (int) Math.ceil(y+height);
  242.     setBounds(x0, y0, x1-x0, y1-y0);
  243.     }    
  244.  
  245.     /**
  246.      * @deprecated As of JDK version 1.1,
  247.      * replaced by <code>setBounds(int, int, int, int)</code>.
  248.      */
  249.     public void reshape(int x, int y, int width, int height) {
  250.     this.x = x;
  251.     this.y = y;
  252.     this.width = width;
  253.     this.height = height;
  254.     }    
  255.  
  256.     /**
  257.      * Returns the location of this rectangle.
  258.      * <p>
  259.      * This method is included for completeness, to parallel the
  260.      * <code>getLocation</code> method of <code>Component</code>.
  261.      * @see       java.awt.Component#getLocation
  262.      * @since     JDK1.1
  263.      */
  264.     public Point getLocation() {
  265.     return new Point(x, y);
  266.     }    
  267.  
  268.     /**
  269.      * Moves the rectangle to the specified location.
  270.      * <p>
  271.      * This method is included for completeness, to parallel the
  272.      * <code>setLocation</code> method of <code>Component</code>.
  273.      * @param     p  the new location for the point.
  274.      * @see       java.awt.Component#setLocation(java.awt.Point)
  275.      * @since     JDK1.1
  276.      */
  277.     public void setLocation(Point p) {
  278.     setLocation(p.x, p.y);
  279.     }    
  280.  
  281.     /**
  282.      * Moves the rectangle to the specified location.
  283.      * <p>
  284.      * This method is included for completeness, to parallel the
  285.      * <code>setLocation</code> method of <code>Component</code>.
  286.      * @param     x  the <i>x</i> coordinate of the new location.
  287.      * @param     y  the <i>y</i> coordinate of the new location.
  288.      * @see       java.awt.Component#setLocation(int, int)
  289.      * @since     JDK1.1
  290.      */
  291.     public void setLocation(int x, int y) {
  292.     move(x, y);
  293.     }    
  294.  
  295.     /**
  296.      * @deprecated As of JDK version 1.1,
  297.      * replaced by <code>setLocation(int, int)</code>.
  298.      */
  299.     public void move(int x, int y) {
  300.     this.x = x;
  301.     this.y = y;
  302.     }    
  303.  
  304.     /**
  305.      * Translates the rectangle the indicated distance,
  306.      * to the right along the <i>x</i> coordinate axis, and 
  307.      * downward along the <i>y</i> coordinate axis.
  308.      * @param     dx   the distance to move the rectangle 
  309.      *                 along the <i>x</i> axis.
  310.      * @param     dy   the distance to move the rectangle 
  311.      *                 along the <i>y</i> axis.
  312.      * @see       java.awt.Rectangle#setLocation(int, int)
  313.      * @see       java.awt.Rectangle#setLocation(java.awt.Point)
  314.      */
  315.     public void translate(int x, int y) {
  316.     this.x += x;
  317.     this.y += y;
  318.     }    
  319.  
  320.     /**
  321.      * Gets the size (width and height) of this rectangle.
  322.      * <p>
  323.      * This method is included for completeness, to parallel the
  324.      * <code>getSize</code> method of <code>Component</code>.
  325.      * @return    a dimension, representing the size.
  326.      * @see       java.awt.Component#getSize
  327.      * @since     JDK1.1
  328.      */
  329.     public Dimension getSize() {
  330.     return new Dimension(width, height);
  331.     }    
  332.  
  333.     /**
  334.      * Sets the size of this rectangle to match the specified dimension.
  335.      * <p>
  336.      * This method is included for completeness, to parallel the
  337.      * <code>setSize</code> method of <code>Component</code>.
  338.      * @param d  the new size for the Dimension object
  339.      * @see       java.awt.Component#setSize(java.awt.Dimension)
  340.      * @since     JDK1.1
  341.      */
  342.     public void setSize(Dimension d) {
  343.     setSize(d.width, d.height);
  344.     }    
  345.  
  346.     /**
  347.      * Sets the size of this rectangle to the specified width and height.
  348.      * <p>
  349.      * This method is included for completeness, to parallel the
  350.      * <code>setSize</code> method of <code>Component</code>.
  351.      * @param     width    the new width for this rectangle object.
  352.      * @param     height   the new height for this rectangle object.
  353.      * @see       java.awt.Component#setSize(int, int)
  354.      * @since     JDK1.1
  355.      */
  356.     public void setSize(int width, int height) {
  357.         resize(width, height);
  358.     }    
  359.  
  360.     /**
  361.      * @deprecated As of JDK version 1.1,
  362.      * replaced by <code>setSize(int, int)</code>.
  363.      */
  364.     public void resize(int width, int height) {
  365.     this.width = width;
  366.     this.height = height;
  367.     }    
  368.  
  369.     /**
  370.      * Checks whether this rectangle contains the specified point.
  371.      * @param p the point (location) to test.
  372.      * @return    <code>true</code> if the point 
  373.      *            (<i>x</i>, <i>y</i>) is inside this rectangle; 
  374.      *            <code>false</code> otherwise.
  375.      * @since     JDK1.1
  376.      */
  377.     public boolean contains(Point p) {
  378.     return contains(p.x, p.y);
  379.     }
  380.  
  381.     /**
  382.      * Checks whether this rectangle contains the point
  383.      * at the specified location (<i>x</i>, <i>y</i>).
  384.      * @param     x   the <i>x</i> coordinate.
  385.      * @param     y   the <i>y</i> coordinate.
  386.      * @return    <code>true</code> if the point 
  387.      *            (<i>x</i>, <i>y</i>) is inside this rectangle; 
  388.      *            <code>false</code> otherwise.
  389.      * @since     JDK1.1
  390.      */
  391.     public boolean contains(int x, int y) {
  392.     return inside(x, y);
  393.     }
  394.  
  395.     /**
  396.      * Checks whether this rectangle entirely contains the specified
  397.      * rectangle.
  398.      * @param     r   the rectangle.
  399.      * @return    <code>true</code> if the rectangle (<i>r</i>) 
  400.      *            is contained entirely inside this rectangle; 
  401.      *            <code>false</code> otherwise.
  402.      * @since     JDK1.1
  403.      */
  404.     public boolean contains(Rectangle r) {
  405.     return contains(r.x, r.y, r.width, r.height);
  406.     }
  407.  
  408.     /**
  409.      * Checks whether this rectangle entirely contains the rectangle
  410.      * at the specified location (<i>X</i>, <i>Y</i>) with the
  411.      * specified dimensions (<i>W</i>, <i>H</i>).
  412.      * @param     X   the <i>x</i> coordinate.
  413.      * @param     Y   the <i>y</i> coordinate.
  414.      * @param     W   the <i>width</i> of the rectangle.
  415.      * @param     H   the <i>height</i> of the rectangle.
  416.      * @return    <code>true</code> if the rectangle specified by
  417.      *            (<i>X</i>, <i>Y</i>, <i>W</i>, <i>H</i>)
  418.      *            is entirely enclosed inside this rectangle; 
  419.      *            <code>false</code> otherwise.
  420.      * @since     JDK1.1
  421.      */
  422.     public boolean contains(int X, int Y, int W, int H) {
  423.     int width = this.width;
  424.     int height = this.height;
  425.     if (width <= 0 || height <= 0 || W <= 0 || H <= 0) {
  426.         return false;
  427.     }
  428.     int x = this.x;
  429.     int y = this.y;
  430.     return (X >= x &&
  431.         Y >= y &&
  432.         X + W <= x + width &&
  433.         Y + H <= y + height);
  434.     }
  435.  
  436.     /**
  437.      * @deprecated As of JDK version 1.1,
  438.      * replaced by <code>contains(int, int)</code>.
  439.      */
  440.     public boolean inside(int x, int y) {
  441.     return (x >= this.x) && ((x - this.x) < this.width) && (y >= this.y) && ((y-this.y) < this.height);
  442.     }
  443.  
  444.     /**
  445.      * Determines whether this rectangle and the specified rectangle  
  446.      * intersect. Two rectangles intersect if their intersection is 
  447.      * nonempty. 
  448.      * @param     r   a rectangle.
  449.      * @return    <code>true</code> if the specified rectangle 
  450.      *            and this rectangle insersect; 
  451.      *            <code>false</code> otherwise.
  452.      */
  453.     public boolean intersects(Rectangle r) {
  454.     return !((r.x + r.width <= x) ||
  455.          (r.y + r.height <= y) ||
  456.          (r.x >= x + width) ||
  457.          (r.y >= y + height));
  458.     }
  459.  
  460.     /**
  461.      * Computes the intersection of this rectangle with the 
  462.      * specified rectangle. Returns a new rectangle that 
  463.      * represents the intersection of the two rectangles.
  464.      * @param     r   a rectangle.
  465.      * @return    the largest rectangle contained in both the 
  466.      *            specified rectangle and in this rectangle.
  467.      */
  468.     public Rectangle intersection(Rectangle r) {
  469.     int x1 = Math.max(x, r.x);
  470.     int x2 = Math.min(x + width, r.x + r.width);
  471.     int y1 = Math.max(y, r.y);
  472.     int y2 = Math.min(y + height, r.y + r.height);
  473.     return new Rectangle(x1, y1, x2 - x1, y2 - y1);
  474.     }
  475.  
  476.     /**
  477.      * Computes the union of this rectangle with the 
  478.      * specified rectangle. Returns a new rectangle that 
  479.      * represents the union of the two rectangles.
  480.      * @param     r   a rectangle.
  481.      * @return    the smallest rectangle containing both the specified 
  482.      *            rectangle and this rectangle.
  483.      */
  484.     public Rectangle union(Rectangle r) {
  485.     int x1 = Math.min(x, r.x);
  486.     int x2 = Math.max(x + width, r.x + r.width);
  487.     int y1 = Math.min(y, r.y);
  488.     int y2 = Math.max(y + height, r.y + r.height);
  489.     return new Rectangle(x1, y1, x2 - x1, y2 - y1);
  490.     }
  491.  
  492.     /**
  493.      * Adds a point, specified by the integer arguments <code>newx</code>
  494.      * and <code>newy</code>, to this rectangle. The resulting rectangle is
  495.      * the smallest rectangle that contains both the original rectangle 
  496.      * and the specified point.
  497.      * <p>
  498.      * After adding a point, a call to <code>contains<code> with the 
  499.      * added point as an argument will not necessarily return 
  500.      * <code>true</code>. The <code>contains</code> method does not 
  501.      * return <code>true</code> for points on the right or bottom 
  502.      * edges of a rectangle. Therefore if the added point falls on 
  503.      * the left or bottom edge of the enlarged rectangle, 
  504.      * <code>contains</code> will return <code>false</code> for that point.
  505.      * 
  506.      * @param     newx   the <i>x</i> coordinate of the new point.
  507.      * @param     newy   the <i>y</i> coordinate of the new point.
  508.      */
  509.     public void add(int newx, int newy) {
  510.     int x1 = Math.min(x, newx);
  511.     int x2 = Math.max(x + width, newx);
  512.     int y1 = Math.min(y, newy);
  513.     int y2 = Math.max(y + height, newy);
  514.     x = x1;
  515.     y = y1;
  516.     width = x2 - x1;
  517.     height = y2 - y1;
  518.     }
  519.  
  520.     /**
  521.      * Adds the point <code>pt</code> to this rectangle. The resulting 
  522.      * rectangle is the smallest rectangle that contains both the 
  523.      * original rectangle and the specified point.
  524.      * <p>
  525.      * After adding a point, a call to <code>contains<code> with the 
  526.      * added point as an argument will not necessarily return 
  527.      * <code>true</code>. The <code>contains</code> method does not 
  528.      * return <code>true</code> for points on the right or bottom 
  529.      * edges of a rectangle. Therefore if the added point falls on 
  530.      * the left or bottom edge of the enlarged rectangle, 
  531.      * <code>contains</code> will return <code>false</code> for that point.
  532.      * 
  533.      * @param     pt the new point to add to the rectangle.
  534.      */
  535.     public void add(Point pt) {
  536.     add(pt.x, pt.y);
  537.     }
  538.  
  539.     /**
  540.      * Adds a rectangle to this rectangle. The resulting rectangle is
  541.      * the union of the two rectangles. 
  542.      * @param     a rectangle.
  543.      */
  544.     public void add(Rectangle r) {
  545.     int x1 = Math.min(x, r.x);
  546.     int x2 = Math.max(x + width, r.x + r.width);
  547.     int y1 = Math.min(y, r.y);
  548.     int y2 = Math.max(y + height, r.y + r.height);
  549.     x = x1;
  550.     y = y1;
  551.     width = x2 - x1;
  552.     height = y2 - y1;
  553.     }
  554.  
  555.     /**
  556.      * Grows the rectangle both horizontally and vertically.
  557.      * <p>
  558.      * This method modifies the rectangle so that it is 
  559.      * <code>h</code> units larger on both the left and right side, 
  560.      * and <code>v</code> units larger at both the top and bottom. 
  561.      * <p>
  562.      * The new rectangle has (<code>x - h</code>, 
  563.      * <code>y - v</code>) as its top-left corner, a 
  564.      * width of 
  565.      * <code>width</code> <code>+</code> <code>2h</code>, 
  566.      * and a height of 
  567.      * <code>height</code> <code>+</code> <code>2v</code>. 
  568.      * <p>
  569.      * If negative values are supplied for <code>h</code> and 
  570.      * <code>v</code>, the size of the rectangle decreases accordingly. 
  571.      * The <code>grow</code> method does not check whether the resulting 
  572.      * values of <code>width</code> and <code>height</code> are 
  573.      * non-negative. 
  574.      * @param     h   the horizontal expansion.
  575.      * @param     v   the vertical expansion.
  576.      */
  577.     public void grow(int h, int v) {
  578.     x -= h;
  579.     y -= v;
  580.     width += h * 2;
  581.     height += v * 2;
  582.     }
  583.  
  584.     /**
  585.      * Determines whether this rectangle is empty. A rectangle is empty if 
  586.      * its width or its height is less than or equal to zero. 
  587.      * @return     <code>true</code> if this rectangle is empty; 
  588.      *             <code>false</code> otherwise.
  589.      */
  590.     public boolean isEmpty() {
  591.     return (width <= 0) || (height <= 0);
  592.     }
  593.  
  594.     /**
  595.      * Determines where the specified float coordinates lie with respect
  596.      * to this Rectangle.
  597.      * This method computes a binary OR of the appropriate mask values
  598.      * indicating which sides of the rectangle the given point is
  599.      * outside of.
  600.      * @return the logical OR of all appropriate out codes
  601.      * @see #OUT_LEFT
  602.      * @see #OUT_TOP
  603.      * @see #OUT_RIGHT
  604.      * @see #OUT_BOTTOM
  605.      * @since JDK1.2
  606.      */
  607.     public int outcode(double x, double y) {
  608.     int out = 0;
  609.     if (this.width <= 0) {
  610.         out |= OUT_LEFT | OUT_RIGHT;
  611.     } else if (x < this.x) {
  612.         out |= OUT_LEFT;
  613.     } else if (x > this.x + this.width) {
  614.         out |= OUT_RIGHT;
  615.     }
  616.     if (this.height <= 0) {
  617.         out |= OUT_TOP | OUT_BOTTOM;
  618.     } else if (y < this.y) {
  619.         out |= OUT_TOP;
  620.     } else if (y > this.y + this.height) {
  621.         out |= OUT_BOTTOM;
  622.     }
  623.     return out;
  624.     }
  625.  
  626.     /**
  627.      * Return a new Rectangle2D object representing the intersection of 
  628.      * this rectangle with the given rectangle.
  629.      * @param     r   the rectangle to be intersected with
  630.      * @return    the largest rectangle contained in both the 
  631.      *            specified rectangle and in this rectangle
  632.      * @since JDK1.2
  633.      */
  634.     public Rectangle2D createIntersection(Rectangle2D r) {
  635.     if (r instanceof Rectangle) {
  636.         return intersection((Rectangle) r);
  637.     }
  638.     Rectangle2D dest = new Rectangle2D.Double();
  639.     Rectangle2D.intersect(this, r, dest);
  640.     return dest;
  641.     }
  642.  
  643.     /**
  644.      * Return a new Rectangle2D object representing the union of 
  645.      * this rectangle with the given rectangle.
  646.      * @param     r   the rectangle to be combined with
  647.      * @return    the smallest rectangle containing both the specified 
  648.      *            rectangle and this rectangle.
  649.      * @since JDK1.2
  650.      */
  651.     public Rectangle2D createUnion(Rectangle2D r) {
  652.     if (r instanceof Rectangle) {
  653.         return union((Rectangle) r);
  654.     }
  655.     Rectangle2D dest = new Rectangle2D.Double();
  656.     Rectangle2D.union(this, r, dest);
  657.     return dest;
  658.     }
  659.  
  660.     /**
  661.      * Returns the hashcode for this rectangle.
  662.      * @return     the hashcode for this rectangle.
  663.      */
  664.     public int hashCode() {
  665.     return x ^ (y*37) ^ (width*43) ^ (height*47);
  666.     }
  667.  
  668.     /**
  669.      * Checks whether two rectangles are equal.
  670.      * <p>
  671.      * The result is <kbd>true</kbd> if and only if the argument is not 
  672.      * <kbd>null</kbd> and is a <kbd>Rectangle</kbd> object that has the 
  673.      * same top-left corner, width, and height as this rectangle. 
  674.      * @param     obj   the object to compare with.
  675.      * @return    <code>true</code> if the objects are equal; 
  676.      *            <code>false</code> otherwise.
  677.      */
  678.     public boolean equals(Object obj) {
  679.     if (obj instanceof Rectangle) {
  680.         Rectangle r = (Rectangle)obj;
  681.         return (x == r.x) && (y == r.y) && (width == r.width) && (height == r.height);
  682.     }
  683.     return false;
  684.     }
  685.  
  686.     /**
  687.      * Returns a string representation of this rectangle 
  688.      * and its values.
  689.      * @return     a string representation of this rectangle.
  690.      */
  691.     public String toString() {
  692.     return getClass().getName() + "[x=" + x + ",y=" + y + ",width=" + width + ",height=" + height + "]";
  693.     }
  694. }
  695.