Java Platform 1.2
Beta 4

Class java.awt.Rectangle

java.lang.Object
  |
  +--java.awt.geom.RectangularShape
        |
        +--java.awt.geom.Rectangle2D
              |
              +--java.awt.Rectangle

public class Rectangle
extends Rectangle2D
implements Shape, Serializable
A rectangle specifies an area in a coordinate space that is defined by the rectangle's top-left point (xy) in the coordinate space, its width, and its height.

A rectangle's width and height are public fields. The constructors that allow you to create a rectangle, and the methods that allow you to modify one, do not prevent you from setting a negative value for width or height.

A rectangle whose width or height is negative is considered empty, and all methods defined by the Rectangle class behave accordingly. If the rectangle is empty, then the method isEmpty returns true. No point can be contained by or inside an empty rectangle, however the values of width and height are still valid. An empty rectangle still has a location in the coordinate space, and methods that change its size or location remain valid. The behavior of methods that operate on more than one rectangle is undefined if any of the participating rectangles has a negative width or height. These methods include intersects, intersection, and union. specified in integer coordinates.

Since:
JDK1.0
See Also:
Serialized Form

Inner classes inherited from class java.awt.geom.Rectangle2D
Rectangle2D.Double , Rectangle2D.Float
 
Field Summary
 int height
          The height of the rectangle.
 int width
          The width of the rectangle.
 int x
          The x coordinate of the rectangle.
 int y
          The y coordinate of the rectangle.
 
Fields inherited from class java.awt.geom.Rectangle2D
OUT_BOTTOM , OUT_LEFT , OUT_RIGHT , OUT_TOP
 
Constructor Summary
Rectangle()
          Constructs a new rectangle whose top-left corner is at (0, 0) in the coordinate space, and whose width and height are zero.
Rectangle(Dimension d)
          Constructs a new rectangle whose top left corner is (0, 0) and whose width and height are specified by the dimension argument.
Rectangle(int x, int y, int width, int height)
          Constructs a new rectangle whose top-left corner is specified as (xy) and whose width and height are specified by the arguments of the same name.
Rectangle(int width, int height)
          Constructs a new rectangle whose top-left corner is at (0, 0) in the coordinate space, and whose width and height are specified by the arguments of the same name.
Rectangle(Point p, Dimension d)
          Constructs a new rectangle whose top-left corner is specified by the point argument, and whose width and height are specified by the dimension argument.
Rectangle(Point p)
          Constructs a new rectangle whose top-left corner is the specified point, and whose width and height are zero.
Rectangle(Rectangle r)
          Constructs a new rectangle, initialized to match the values of the specificed rectangle.
 
Method Summary
 void add(int newx, int newy)
          Adds a point, specified by the integer arguments newx and newy, to this rectangle.
 void add(Point pt)
          Adds the point pt to this rectangle.
 void add(Rectangle r)
          Adds a rectangle to this rectangle.
 boolean contains(int X, int Y, int W, int H)
          Checks whether this rectangle entirely contains the rectangle at the specified location (XY) with the specified dimensions (WH).
 boolean contains(int x, int y)
          Checks whether this rectangle contains the point at the specified location (xy).
 boolean contains(Point p)
          Checks whether this rectangle contains the specified point.
 boolean contains(Rectangle r)
          Checks whether this rectangle entirely contains the specified rectangle.
 Rectangle2D createIntersection(Rectangle2D r)
          Return a new Rectangle2D object representing the intersection of this rectangle with the given rectangle.
 Rectangle2D createUnion(Rectangle2D r)
          Return a new Rectangle2D object representing the union of this rectangle with the given rectangle.
 boolean equals(Object obj)
          Checks whether two rectangles are equal.
 Rectangle getBounds()
          Gets the bounding rectangle of this rectangle.
 double getHeight()
          Returns the height of the bounding rectangle in double precision.
 Point getLocation()
          Returns the location of this rectangle.
 Dimension getSize()
          Gets the size (width and height) of this rectangle.
 double getWidth()
          Returns the width of the bounding rectangle in double precision.
 double getX()
          Returns the X coordinate of the bounding rectangle in double precision.
 double getY()
          Returns the Y coordinate of the bounding rectangle in double precision.
 void grow(int h, int v)
          Grows the rectangle both horizontally and vertically.
 int hashCode()
          Returns the hashcode for this rectangle.
 boolean inside(int x, int y)
          Deprecated. As of JDK version 1.1, replaced by contains(int, int).
 Rectangle intersection(Rectangle r)
          Computes the intersection of this rectangle with the specified rectangle.
 boolean intersects(Rectangle r)
          Determines whether this rectangle and the specified rectangle intersect.
 boolean isEmpty()
          Determines whether this rectangle is empty.
 void move(int x, int y)
          Deprecated. As of JDK version 1.1, replaced by setLocation(int, int).
 int outcode(double x, double y)
          Determines where the specified float coordinates lie with respect to this Rectangle.
 void reshape(int x, int y, int width, int height)
          Deprecated. As of JDK version 1.1, replaced by setBounds(int, int, int, int).
 void resize(int width, int height)
          Deprecated. As of JDK version 1.1, replaced by setSize(int, int).
 void setBounds(int x, int y, int width, int height)
          Sets the bounding rectangle of this rectangle to the specified values for x, y, width, and height.
 void setBounds(Rectangle r)
          Sets the bounding rectangle of this rectangle to match the specified rectangle.
 void setLocation(int x, int y)
          Moves the rectangle to the specified location.
 void setLocation(Point p)
          Moves the rectangle to the specified location.
 void setRect(double x, double y, double width, double height)
          Set the bounds of this rectangle to the specified x, y, width, and height.
 void setSize(Dimension d)
          Sets the size of this rectangle to match the specified dimension.
 void setSize(int width, int height)
          Sets the size of this rectangle to the specified width and height.
 String toString()
          Returns a string representation of this rectangle and its values.
 void translate(int x, int y)
          Translates the rectangle the indicated distance, to the right along the x coordinate axis, and downward along the y coordinate axis.
 Rectangle union(Rectangle r)
          Computes the union of this rectangle with the specified rectangle.
 
Methods inherited from class java.awt.geom.Rectangle2D
add , add , add , contains , contains , getBounds2D , getPathIterator , getPathIterator , intersect , intersects , intersectsLine , intersectsLine , outcode , setFrame , setRect , union
 
Methods inherited from class java.awt.geom.RectangularShape
clone , contains , contains , getCenterX , getCenterY , getFrame , getMaxX , getMaxY , getMinX , getMinY , intersects , setFrame , setFrame , setFrameFromCenter , setFrameFromCenter , setFrameFromDiagonal , setFrameFromDiagonal
 
Methods inherited from class java.lang.Object
finalize , getClass , notify , notifyAll , wait , wait , wait
 

Field Detail

x

public int x
The x coordinate of the rectangle.

y

public int y
The y coordinate of the rectangle.

width

public int width
The width of the rectangle.
Since:
JDK1.0.

height

public int height
The height of the rectangle.
Constructor Detail

Rectangle

public Rectangle()
Constructs a new rectangle whose top-left corner is at (0, 0) in the coordinate space, and whose width and height are zero.

Rectangle

public Rectangle(Rectangle r)
Constructs a new rectangle, initialized to match the values of the specificed rectangle.
Parameters:
r - a rectangle from which to copy initial values.
Since:
JDK1.1

Rectangle

public Rectangle(int x,
                 int y,
                 int width,
                 int height)
Constructs a new rectangle whose top-left corner is specified as (xy) and whose width and height are specified by the arguments of the same name.
Parameters:
x - the x coordinate.
y - the y coordinate.
width - the width of the rectangle.
height - the height of the rectangle.

Rectangle

public Rectangle(int width,
                 int height)
Constructs a new rectangle whose top-left corner is at (0, 0) in the coordinate space, and whose width and height are specified by the arguments of the same name.
Parameters:
width - the width of the rectangle.
height - the height of the rectangle.

Rectangle

public Rectangle(Point p,
                 Dimension d)
Constructs a new rectangle whose top-left corner is specified by the point argument, and whose width and height are specified by the dimension argument.
Parameters:
p - a point, the top-left corner of the rectangle.
d - a dimension, representing the width and height.

Rectangle

public Rectangle(Point p)
Constructs a new rectangle whose top-left corner is the specified point, and whose width and height are zero.
Parameters:
p - the top left corner of the rectangle.

Rectangle

public Rectangle(Dimension d)
Constructs a new rectangle whose top left corner is (0, 0) and whose width and height are specified by the dimension argument.
Parameters:
d - a dimension, specifying width and height.
Method Detail

getX

public double getX()
Returns the X coordinate of the bounding rectangle in double precision.
Overrides:
getX in class RectangularShape

getY

public double getY()
Returns the Y coordinate of the bounding rectangle in double precision.
Overrides:
getY in class RectangularShape

getWidth

public double getWidth()
Returns the width of the bounding rectangle in double precision.
Overrides:
getWidth in class RectangularShape

getHeight

public double getHeight()
Returns the height of the bounding rectangle in double precision.
Overrides:
getHeight in class RectangularShape

getBounds

public Rectangle getBounds()
Gets the bounding rectangle of this rectangle.

This method is included for completeness, to parallel the getBounds method of Component.

Specified by:
getBounds in interface Shape
Returns:
a new rectangle, equal to the bounding rectangle for this rectangle.
Overrides:
getBounds in class RectangularShape
Since:
JDK1.1
See Also:
Component.getBounds()

setBounds

public void setBounds(Rectangle r)
Sets the bounding rectangle of this rectangle to match the specified rectangle.

This method is included for completeness, to parallel the setBounds method of Component.

Parameters:
r - a rectangle.
Since:
JDK1.1
See Also:
Component.setBounds(java.awt.Rectangle)

setBounds

public void setBounds(int x,
                      int y,
                      int width,
                      int height)
Sets the bounding rectangle of this rectangle to the specified values for x, y, width, and height.

This method is included for completeness, to parallel the setBounds method of Component.

Parameters:
x - the new x coordinate for the top-left corner of this rectangle.
y - the new y coordinate for the top-left corner of this rectangle.
width - the new width for this rectangle.
height - the new height for this rectangle.
Since:
JDK1.1
See Also:
Component.setBounds(int, int, int, int)

setRect

public void setRect(double x,
                    double y,
                    double width,
                    double height)
Set the bounds of this rectangle to the specified x, y, width, and height. This method is included for completeness, to parallel the setBounds method of Component.
Parameters:
width - the new width for the Dimension object
height - the new height for the Dimension object
Overrides:
setRect in class Rectangle2D

reshape

public void reshape(int x,
                    int y,
                    int width,
                    int height)
Deprecated. As of JDK version 1.1, replaced by setBounds(int, int, int, int).

getLocation

public Point getLocation()
Returns the location of this rectangle.

This method is included for completeness, to parallel the getLocation method of Component.

Since:
JDK1.1
See Also:
Component.getLocation()

setLocation

public void setLocation(Point p)
Moves the rectangle to the specified location.

This method is included for completeness, to parallel the setLocation method of Component.

Parameters:
p - the new location for the point.
Since:
JDK1.1
See Also:
Component.setLocation(java.awt.Point)

setLocation

public void setLocation(int x,
                        int y)
Moves the rectangle to the specified location.

This method is included for completeness, to parallel the setLocation method of Component.

Parameters:
x - the x coordinate of the new location.
y - the y coordinate of the new location.
Since:
JDK1.1
See Also:
Component.setLocation(int, int)

move

public void move(int x,
                 int y)
Deprecated. As of JDK version 1.1, replaced by setLocation(int, int).

translate

public void translate(int x,
                      int y)
Translates the rectangle the indicated distance, to the right along the x coordinate axis, and downward along the y coordinate axis.
Parameters:
dx - the distance to move the rectangle along the x axis.
dy - the distance to move the rectangle along the y axis.
See Also:
setLocation(int, int), setLocation(java.awt.Point)

getSize

public Dimension getSize()
Gets the size (width and height) of this rectangle.

This method is included for completeness, to parallel the getSize method of Component.

Returns:
a dimension, representing the size.
Since:
JDK1.1
See Also:
Component.getSize()

setSize

public void setSize(Dimension d)
Sets the size of this rectangle to match the specified dimension.

This method is included for completeness, to parallel the setSize method of Component.

Parameters:
d - the new size for the Dimension object
Since:
JDK1.1
See Also:
Component.setSize(java.awt.Dimension)

setSize

public void setSize(int width,
                    int height)
Sets the size of this rectangle to the specified width and height.

This method is included for completeness, to parallel the setSize method of Component.

Parameters:
width - the new width for this rectangle object.
height - the new height for this rectangle object.
Since:
JDK1.1
See Also:
Component.setSize(int, int)

resize

public void resize(int width,
                   int height)
Deprecated. As of JDK version 1.1, replaced by setSize(int, int).

contains

public boolean contains(Point p)
Checks whether this rectangle contains the specified point.
Parameters:
p - the point (location) to test.
Returns:
true if the point (xy) is inside this rectangle; false otherwise.
Since:
JDK1.1

contains

public boolean contains(int x,
                        int y)
Checks whether this rectangle contains the point at the specified location (xy).
Parameters:
x - the x coordinate.
y - the y coordinate.
Returns:
true if the point (xy) is inside this rectangle; false otherwise.
Since:
JDK1.1

contains

public boolean contains(Rectangle r)
Checks whether this rectangle entirely contains the specified rectangle.
Parameters:
r - the rectangle.
Returns:
true if the rectangle (r) is contained entirely inside this rectangle; false otherwise.
Since:
JDK1.1

contains

public boolean contains(int X,
                        int Y,
                        int W,
                        int H)
Checks whether this rectangle entirely contains the rectangle at the specified location (XY) with the specified dimensions (WH).
Parameters:
X - the x coordinate.
Y - the y coordinate.
W - the width of the rectangle.
H - the height of the rectangle.
Returns:
true if the rectangle specified by (XYWH) is entirely enclosed inside this rectangle; false otherwise.
Since:
JDK1.1

inside

public boolean inside(int x,
                      int y)
Deprecated. As of JDK version 1.1, replaced by contains(int, int).

intersects

public boolean intersects(Rectangle r)
Determines whether this rectangle and the specified rectangle intersect. Two rectangles intersect if their intersection is nonempty.
Parameters:
r - a rectangle.
Returns:
true if the specified rectangle and this rectangle insersect; false otherwise.

intersection

public Rectangle intersection(Rectangle r)
Computes the intersection of this rectangle with the specified rectangle. Returns a new rectangle that represents the intersection of the two rectangles.
Parameters:
r - a rectangle.
Returns:
the largest rectangle contained in both the specified rectangle and in this rectangle.

union

public Rectangle union(Rectangle r)
Computes the union of this rectangle with the specified rectangle. Returns a new rectangle that represents the union of the two rectangles.
Parameters:
r - a rectangle.
Returns:
the smallest rectangle containing both the specified rectangle and this rectangle.

add

public void add(int newx,
                int newy)
Adds a point, specified by the integer arguments newx and newy, to this rectangle. The resulting rectangle is the smallest rectangle that contains both the original rectangle and the specified point.

After adding a point, a call to contains with the added point as an argument will not necessarily return true. The contains method does not return true for points on the right or bottom edges of a rectangle. Therefore if the added point falls on the right or bottom edge of the enlarged rectangle, contains will return false for that point.

Parameters:
newx - the x coordinate of the new point.
newy - the y coordinate of the new point.

add

public void add(Point pt)
Adds the point pt to this rectangle. The resulting rectangle is the smallest rectangle that contains both the original rectangle and the specified point.

After adding a point, a call to contains with the added point as an argument will not necessarily return true. The contains method does not return true for points on the right or bottom edges of a rectangle. Therefore if the added point falls on the right or bottom edge of the enlarged rectangle, contains will return false for that point.

Parameters:
pt - the new point to add to the rectangle.

add

public void add(Rectangle r)
Adds a rectangle to this rectangle. The resulting rectangle is the union of the two rectangles.
Parameters:
a - rectangle.

grow

public void grow(int h,
                 int v)
Grows the rectangle both horizontally and vertically.

This method modifies the rectangle so that it is h units larger on both the left and right side, and v units larger at both the top and bottom.

The new rectangle has (x - h, y - v) as its top-left corner, a width of width + 2h, and a height of height + 2v.

If negative values are supplied for h and v, the size of the rectangle decreases accordingly. The grow method does not check whether the resulting values of width and height are non-negative.

Parameters:
h - the horizontal expansion.
v - the vertical expansion.

isEmpty

public boolean isEmpty()
Determines whether this rectangle is empty. A rectangle is empty if its width or its height is less than or equal to zero.
Returns:
true if this rectangle is empty; false otherwise.
Overrides:
isEmpty in class RectangularShape

outcode

public int outcode(double x,
                   double y)
Determines where the specified float coordinates lie with respect to this Rectangle. This method computes a binary OR of the appropriate mask values indicating which sides of the rectangle the given point is outside of.
Returns:
the logical OR of all appropriate out codes
Overrides:
outcode in class Rectangle2D
Since:
JDK1.2
See Also:
Rectangle2D.OUT_LEFT, Rectangle2D.OUT_TOP, Rectangle2D.OUT_RIGHT, Rectangle2D.OUT_BOTTOM

createIntersection

public Rectangle2D createIntersection(Rectangle2D r)
Return a new Rectangle2D object representing the intersection of this rectangle with the given rectangle.
Parameters:
r - the rectangle to be intersected with
Returns:
the largest rectangle contained in both the specified rectangle and in this rectangle
Overrides:
createIntersection in class Rectangle2D
Since:
JDK1.2

createUnion

public Rectangle2D createUnion(Rectangle2D r)
Return a new Rectangle2D object representing the union of this rectangle with the given rectangle.
Parameters:
r - the rectangle to be combined with
Returns:
the smallest rectangle containing both the specified rectangle and this rectangle.
Overrides:
createUnion in class Rectangle2D
Since:
JDK1.2

hashCode

public int hashCode()
Returns the hashcode for this rectangle.
Returns:
the hashcode for this rectangle.
Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)
Checks whether two rectangles are equal.

The result is true if and only if the argument is not null and is a Rectangle object that has the same top-left corner, width, and height as this rectangle.

Parameters:
obj - the object to compare with.
Returns:
true if the objects are equal; false otherwise.
Overrides:
equals in class Rectangle2D

toString

public String toString()
Returns a string representation of this rectangle and its values.
Returns:
a string representation of this rectangle.
Overrides:
toString in class Object

Java Platform 1.2
Beta 4

Submit a bug or feature
Submit comments/suggestions about new javadoc look
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1998 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.
This documentation was generated with a post-Beta4 version of Javadoc.