A rectangle specifies an area in a coordinate space that is
defined by the rectangle's top-left point (x, y)
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.
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.
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.
Constructs a new rectangle, initialized to match the values of
the specificed rectangle.
Parameters:
r - a rectangle from which to copy initial values.
Rectangle
public Rectangle(int x,
int y,
int width,
int height)
Constructs a new rectangle whose top-left corner is specified as
(x, y) 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.
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.
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.
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 left or bottom edge of the enlarged rectangle,
contains will return false for that point.
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.
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.
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.