src/share/classes/java/awt/Rectangle.java

Print this page




  24  */
  25 
  26 package java.awt;
  27 
  28 import java.awt.geom.Rectangle2D;
  29 import java.beans.Transient;
  30 
  31 /**
  32  * A <code>Rectangle</code> specifies an area in a coordinate space that is
  33  * enclosed by the <code>Rectangle</code> object's upper-left point
  34  * {@code (x,y)}
  35  * in the coordinate space, its width, and its height.
  36  * <p>
  37  * A <code>Rectangle</code> object's <code>width</code> and
  38  * <code>height</code> are <code>public</code> fields. The constructors
  39  * that create a <code>Rectangle</code>, and the methods that can modify
  40  * one, do not prevent setting a negative value for width or height.
  41  * <p>
  42  * <a name="Empty">
  43  * A {@code Rectangle} whose width or height is exactly zero has location
  44  * along those axes with zero dimension, but is otherwise considered empty.
  45  * The {@link #isEmpty} method will return true for such a {@code Rectangle}.
  46  * Methods which test if an empty {@code Rectangle} contains or intersects
  47  * a point or rectangle will always return false if either dimension is zero.
  48  * Methods which combine such a {@code Rectangle} with a point or rectangle
  49  * will include the location of the {@code Rectangle} on that axis in the
  50  * result as if the {@link #add(Point)} method were being called.
  51  * </a>
  52  * <p>
  53  * <a name="NonExistant">
  54  * A {@code Rectangle} whose width or height is negative has neither
  55  * location nor dimension along those axes with negative dimensions.
  56  * Such a {@code Rectangle} is treated as non-existant along those axes.
  57  * Such a {@code Rectangle} is also empty with respect to containment
  58  * calculations and methods which test if it contains or intersects a
  59  * point or rectangle will always return false.
  60  * Methods which combine such a {@code Rectangle} with a point or rectangle
  61  * will ignore the {@code Rectangle} entirely in generating the result.
  62  * If two {@code Rectangle} objects are combined and each has a negative
  63  * dimension, the result will have at least one negative dimension.
  64  * </a>
  65  * <p>
  66  * Methods which affect only the location of a {@code Rectangle} will
  67  * operate on its location regardless of whether or not it has a negative
  68  * or zero dimension along either axis.
  69  * <p>
  70  * Note that a {@code Rectangle} constructed with the default no-argument
  71  * constructor will have dimensions of {@code 0x0} and therefore be empty.


 405 
 406         reshape(newx, newy, neww, newh);
 407     }
 408     // Return best integer representation for v, clipped to integer
 409     // range and floor-ed or ceiling-ed, depending on the boolean.
 410     private static int clip(double v, boolean doceil) {
 411         if (v <= Integer.MIN_VALUE) {
 412             return Integer.MIN_VALUE;
 413         }
 414         if (v >= Integer.MAX_VALUE) {
 415             return Integer.MAX_VALUE;
 416         }
 417         return (int) (doceil ? Math.ceil(v) : Math.floor(v));
 418     }
 419 
 420     /**
 421      * Sets the bounding <code>Rectangle</code> of this
 422      * <code>Rectangle</code> to the specified
 423      * <code>x</code>, <code>y</code>, <code>width</code>,
 424      * and <code>height</code>.
 425      * <p>
 426      * @param x the new X coordinate for the upper-left
 427      *                    corner of this <code>Rectangle</code>
 428      * @param y the new Y coordinate for the upper-left
 429      *                    corner of this <code>Rectangle</code>
 430      * @param width the new width for this <code>Rectangle</code>
 431      * @param height the new height for this <code>Rectangle</code>
 432      * @deprecated As of JDK version 1.1,
 433      * replaced by <code>setBounds(int, int, int, int)</code>.
 434      */
 435     @Deprecated
 436     public void reshape(int x, int y, int width, int height) {
 437         this.x = x;
 438         this.y = y;
 439         this.width = width;
 440         this.height = height;
 441     }
 442 
 443     /**
 444      * Returns the location of this <code>Rectangle</code>.
 445      * <p>


 471         setLocation(p.x, p.y);
 472     }
 473 
 474     /**
 475      * Moves this <code>Rectangle</code> to the specified location.
 476      * <p>
 477      * This method is included for completeness, to parallel the
 478      * <code>setLocation</code> method of <code>Component</code>.
 479      * @param x the X coordinate of the new location
 480      * @param y the Y coordinate of the new location
 481      * @see       #getLocation
 482      * @see       java.awt.Component#setLocation(int, int)
 483      * @since     1.1
 484      */
 485     public void setLocation(int x, int y) {
 486         move(x, y);
 487     }
 488 
 489     /**
 490      * Moves this <code>Rectangle</code> to the specified location.
 491      * <p>
 492      * @param x the X coordinate of the new location
 493      * @param y the Y coordinate of the new location
 494      * @deprecated As of JDK version 1.1,
 495      * replaced by <code>setLocation(int, int)</code>.
 496      */
 497     @Deprecated
 498     public void move(int x, int y) {
 499         this.x = x;
 500         this.y = y;
 501     }
 502 
 503     /**
 504      * Translates this <code>Rectangle</code> the indicated distance,
 505      * to the right along the X coordinate axis, and
 506      * downward along the Y coordinate axis.
 507      * @param dx the distance to move this <code>Rectangle</code>
 508      *                 along the X axis
 509      * @param dy the distance to move this <code>Rectangle</code>
 510      *                 along the Y axis
 511      * @see       java.awt.Rectangle#setLocation(int, int)


 612 
 613     /**
 614      * Sets the size of this <code>Rectangle</code> to the specified
 615      * width and height.
 616      * <p>
 617      * This method is included for completeness, to parallel the
 618      * <code>setSize</code> method of <code>Component</code>.
 619      * @param width the new width for this <code>Rectangle</code>
 620      * @param height the new height for this <code>Rectangle</code>
 621      * @see       java.awt.Component#setSize(int, int)
 622      * @see       #getSize
 623      * @since     1.1
 624      */
 625     public void setSize(int width, int height) {
 626         resize(width, height);
 627     }
 628 
 629     /**
 630      * Sets the size of this <code>Rectangle</code> to the specified
 631      * width and height.
 632      * <p>
 633      * @param width the new width for this <code>Rectangle</code>
 634      * @param height the new height for this <code>Rectangle</code>
 635      * @deprecated As of JDK version 1.1,
 636      * replaced by <code>setSize(int, int)</code>.
 637      */
 638     @Deprecated
 639     public void resize(int width, int height) {
 640         this.width = width;
 641         this.height = height;
 642     }
 643 
 644     /**
 645      * Checks whether or not this <code>Rectangle</code> contains the
 646      * specified <code>Point</code>.
 647      * @param p the <code>Point</code> to test
 648      * @return    <code>true</code> if the specified <code>Point</code>
 649      *            is inside this <code>Rectangle</code>;
 650      *            <code>false</code> otherwise.
 651      * @since     1.1
 652      */




  24  */
  25 
  26 package java.awt;
  27 
  28 import java.awt.geom.Rectangle2D;
  29 import java.beans.Transient;
  30 
  31 /**
  32  * A <code>Rectangle</code> specifies an area in a coordinate space that is
  33  * enclosed by the <code>Rectangle</code> object's upper-left point
  34  * {@code (x,y)}
  35  * in the coordinate space, its width, and its height.
  36  * <p>
  37  * A <code>Rectangle</code> object's <code>width</code> and
  38  * <code>height</code> are <code>public</code> fields. The constructors
  39  * that create a <code>Rectangle</code>, and the methods that can modify
  40  * one, do not prevent setting a negative value for width or height.
  41  * <p>
  42  * <a name="Empty">
  43  * A {@code Rectangle} whose width or height is exactly zero has location
  44  * along those axes with zero dimension, but is otherwise considered empty.</a>
  45  * The {@link #isEmpty} method will return true for such a {@code Rectangle}.
  46  * Methods which test if an empty {@code Rectangle} contains or intersects
  47  * a point or rectangle will always return false if either dimension is zero.
  48  * Methods which combine such a {@code Rectangle} with a point or rectangle
  49  * will include the location of the {@code Rectangle} on that axis in the
  50  * result as if the {@link #add(Point)} method were being called.

  51  * <p>
  52  * <a name="NonExistant">
  53  * A {@code Rectangle} whose width or height is negative has neither
  54  * location nor dimension along those axes with negative dimensions.
  55  * Such a {@code Rectangle} is treated as non-existant along those axes.
  56  * Such a {@code Rectangle} is also empty with respect to containment
  57  * calculations and methods which test if it contains or intersects a
  58  * point or rectangle will always return false.
  59  * Methods which combine such a {@code Rectangle} with a point or rectangle
  60  * will ignore the {@code Rectangle} entirely in generating the result.
  61  * If two {@code Rectangle} objects are combined and each has a negative
  62  * dimension, the result will have at least one negative dimension.
  63  * </a>
  64  * <p>
  65  * Methods which affect only the location of a {@code Rectangle} will
  66  * operate on its location regardless of whether or not it has a negative
  67  * or zero dimension along either axis.
  68  * <p>
  69  * Note that a {@code Rectangle} constructed with the default no-argument
  70  * constructor will have dimensions of {@code 0x0} and therefore be empty.


 404 
 405         reshape(newx, newy, neww, newh);
 406     }
 407     // Return best integer representation for v, clipped to integer
 408     // range and floor-ed or ceiling-ed, depending on the boolean.
 409     private static int clip(double v, boolean doceil) {
 410         if (v <= Integer.MIN_VALUE) {
 411             return Integer.MIN_VALUE;
 412         }
 413         if (v >= Integer.MAX_VALUE) {
 414             return Integer.MAX_VALUE;
 415         }
 416         return (int) (doceil ? Math.ceil(v) : Math.floor(v));
 417     }
 418 
 419     /**
 420      * Sets the bounding <code>Rectangle</code> of this
 421      * <code>Rectangle</code> to the specified
 422      * <code>x</code>, <code>y</code>, <code>width</code>,
 423      * and <code>height</code>.
 424      *
 425      * @param x the new X coordinate for the upper-left
 426      *                    corner of this <code>Rectangle</code>
 427      * @param y the new Y coordinate for the upper-left
 428      *                    corner of this <code>Rectangle</code>
 429      * @param width the new width for this <code>Rectangle</code>
 430      * @param height the new height for this <code>Rectangle</code>
 431      * @deprecated As of JDK version 1.1,
 432      * replaced by <code>setBounds(int, int, int, int)</code>.
 433      */
 434     @Deprecated
 435     public void reshape(int x, int y, int width, int height) {
 436         this.x = x;
 437         this.y = y;
 438         this.width = width;
 439         this.height = height;
 440     }
 441 
 442     /**
 443      * Returns the location of this <code>Rectangle</code>.
 444      * <p>


 470         setLocation(p.x, p.y);
 471     }
 472 
 473     /**
 474      * Moves this <code>Rectangle</code> to the specified location.
 475      * <p>
 476      * This method is included for completeness, to parallel the
 477      * <code>setLocation</code> method of <code>Component</code>.
 478      * @param x the X coordinate of the new location
 479      * @param y the Y coordinate of the new location
 480      * @see       #getLocation
 481      * @see       java.awt.Component#setLocation(int, int)
 482      * @since     1.1
 483      */
 484     public void setLocation(int x, int y) {
 485         move(x, y);
 486     }
 487 
 488     /**
 489      * Moves this <code>Rectangle</code> to the specified location.
 490      *
 491      * @param x the X coordinate of the new location
 492      * @param y the Y coordinate of the new location
 493      * @deprecated As of JDK version 1.1,
 494      * replaced by <code>setLocation(int, int)</code>.
 495      */
 496     @Deprecated
 497     public void move(int x, int y) {
 498         this.x = x;
 499         this.y = y;
 500     }
 501 
 502     /**
 503      * Translates this <code>Rectangle</code> the indicated distance,
 504      * to the right along the X coordinate axis, and
 505      * downward along the Y coordinate axis.
 506      * @param dx the distance to move this <code>Rectangle</code>
 507      *                 along the X axis
 508      * @param dy the distance to move this <code>Rectangle</code>
 509      *                 along the Y axis
 510      * @see       java.awt.Rectangle#setLocation(int, int)


 611 
 612     /**
 613      * Sets the size of this <code>Rectangle</code> to the specified
 614      * width and height.
 615      * <p>
 616      * This method is included for completeness, to parallel the
 617      * <code>setSize</code> method of <code>Component</code>.
 618      * @param width the new width for this <code>Rectangle</code>
 619      * @param height the new height for this <code>Rectangle</code>
 620      * @see       java.awt.Component#setSize(int, int)
 621      * @see       #getSize
 622      * @since     1.1
 623      */
 624     public void setSize(int width, int height) {
 625         resize(width, height);
 626     }
 627 
 628     /**
 629      * Sets the size of this <code>Rectangle</code> to the specified
 630      * width and height.
 631      *
 632      * @param width the new width for this <code>Rectangle</code>
 633      * @param height the new height for this <code>Rectangle</code>
 634      * @deprecated As of JDK version 1.1,
 635      * replaced by <code>setSize(int, int)</code>.
 636      */
 637     @Deprecated
 638     public void resize(int width, int height) {
 639         this.width = width;
 640         this.height = height;
 641     }
 642 
 643     /**
 644      * Checks whether or not this <code>Rectangle</code> contains the
 645      * specified <code>Point</code>.
 646      * @param p the <code>Point</code> to test
 647      * @return    <code>true</code> if the specified <code>Point</code>
 648      *            is inside this <code>Rectangle</code>;
 649      *            <code>false</code> otherwise.
 650      * @since     1.1
 651      */