All Packages Class Hierarchy This Package Previous Next Index
java.lang.Object  +java.awt.Rectangle
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
.
dimension
argument.
x
, y
) and whose width and height
are specified by the arguments of the same name.
point
argument, and whose width and height
are specified by the dimension
argument.
newx
and newy
, to this rectangle.
pt
to this rectangle.
x
, y
, width
,
and height
.
public int x
public int y
public int width
public int height
public Rectangle()
public Rectangle(Rectangle r)
public Rectangle(int x, int y, int width, int height)
x
, y
) and whose width and height
are specified by the arguments of the same name.
public Rectangle(int width, int height)
public Rectangle(Point p, Dimension d)
point
argument, and whose width and height
are specified by the dimension
argument.
public Rectangle(Point p)
public Rectangle(Dimension d)
dimension
argument.
public Rectangle getBounds()
This method is included for completeness, to parallel the
getBounds
method of Component
.
public void setBounds(Rectangle r)
This method is included for completeness, to parallel the
setBounds
method of Component
.
public void setBounds(int x, int y, int width, int height)
x
, y
, width
,
and height
.
This method is included for completeness, to parallel the
setBounds
method of Component
.
public void reshape(int x, int y, int width, int height)
setBounds(int, int, int, int)
.
public Point getLocation()
This method is included for completeness, to parallel the
getLocation
method of Component
.
public void setLocation(Point p)
This method is included for completeness, to parallel the
setLocation
method of Component
.
public void setLocation(int x, int y)
This method is included for completeness, to parallel the
setLocation
method of Component
.
public void move(int x, int y)
setLocation(int, int)
.
public void translate(int x, int y)
public Dimension getSize()
This method is included for completeness, to parallel the
getSize
method of Component
.
public void setSize(Dimension d)
This method is included for completeness, to parallel the
setSize
method of Component
.
public void setSize(int width, int height)
This method is included for completeness, to parallel the
setSize
method of Component
.
public void resize(int width, int height)
setSize(int, int)
.
public boolean contains(Point p)
true
if the point
(x, y) is inside this rectangle;
false
otherwise.
public boolean contains(int x, int y)
true
if the point
(x, y) is inside this rectangle;
false
otherwise.
public boolean inside(int x, int y)
contains(int, int)
.
public boolean intersects(Rectangle r)
true
if the specified rectangle
and this rectangle insersect;
false
otherwise.
public Rectangle intersection(Rectangle r)
public Rectangle union(Rectangle r)
public void add(int newx, int newy)
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 left 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 left 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 topleft 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
nonnegative.
 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.
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 topleft 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 Object
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
All Packages Class Hierarchy This Package Previous Next Index
Submit a bug or feature