Java Reference
In-Depth Information
You can also use several methods to combine
Rectangle
objects and to extend a
Rectangle
object to
enclose a point. The effects of each of these methods are shown in
Figure 17-8
.
The rectangle that results from each operation is shown with dashed line boundaries. The details of the
operations provided by the methods illustrated in
Figure 17-8
are as follows:
•
Rectangle intersection(Rectangle r)
: Returns a
Rectangle
object that is the intersection of
the current object and the argument. If the two rectangles do not intersect, the
Rectangle
object
returned is at position (0,0), and the
width
and
height
members are zero, so the rectangle is
empty.
•
Rectangle union(Rectangle r)
: Returns the smallest
Rectangle
object enclosing both the cur-
rent Rectangle object and the Rectangle object r, passed as an argument.
•
void add(Rectangle r)
: Expands the current
Rectangle
object to enclose the argument
Rect-
angle
.
•
void add(Point p)
: Expands the current
Rectangle
object to enclose the
Point
object
p
. The
result is the smallest rectangle that encloses the original rectangle and the point.
•
void add(int x, int y)
: Expands the current
Rectangle
object to enclose the point at (
x
,
y
).
•
void grow(int h, int v)
: Enlarges the current
Rectangle
object by moving the boundary out
from the center by
h
horizontally and
v
vertically.
You can also test and compare
Rectangle
objects in various ways with the following methods:
•
boolean isEmpty()
: Returns
true
if the
width
and
height
members of the current
Rectangle
object are zero and returns
false
otherwise.
•
boolean equals(Object rect)
: Returns
true
if the
Rectangle
object passed as an argument is
equal to the current
Rectangle
object, and
false
otherwise. The two rectangles are equal if they
are at the same position and have the same width and height. If the argument is not a
Rectangle
object,
false
is returned.
•
boolean intersects(Rectangle rect)
: Returns
true
if the current
Rectangle
object intersects
rect
and returns
false
otherwise.
•
boolean contains(Point p)
: Returns
true
if the current
Rectangle
object encloses
p
and re-
turns
false
otherwise.