Class Rectangle
- All Implemented Interfaces:
Serializable
,ReadableDimension
,ReadablePoint
,ReadableRectangle
,WritableDimension
,WritablePoint
,WritableRectangle
- Version:
- $Revision$ $Id$
- Author:
- $Author$
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstructor for Rectangle.Rectangle
(int x, int y, int w, int h) Constructor for Rectangle.Constructor for Rectangle.Constructor for Rectangle. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(int newx, int newy) Adds a point, specified by the integer argumentsnewx
andnewy
, to thisRectangle
.void
add
(ReadablePoint pt) Adds the specifiedPoint
to thisRectangle
.void
Adds aRectangle
to thisRectangle
.boolean
contains
(int X, int Y) Checks whether or not thisRectangle
contains the point at the specified location (x, y).boolean
contains
(int X, int Y, int W, int H) Checks whether thisRectangle
entirely contains theRectangle
at the specified location (X, Y) with the specified dimensions (W, H).boolean
Checks whether or not thisRectangle
contains the specifiedPoint
.boolean
Checks whether or not thisRectangle
entirely contains the specifiedRectangle
.boolean
Checks whether two rectangles are equal.void
getBounds
(WritableRectangle dest) Copy this readable rectangle's bounds into a destination Rectangleint
Gets the height.void
getLocation
(WritablePoint dest) Copy this ReadablePoint into a destination Pointvoid
getSize
(WritableDimension dest) Copy this ReadableDimension into a destination Dimensionint
getWidth()
Gets the width.int
getX()
Gets the x.int
getY()
Gets the y.void
grow
(int h, int v) Resizes theRectangle
both horizontally and vertically.intersection
(ReadableRectangle r, Rectangle dest) Computes the intersection of thisRectangle
with the specifiedRectangle
.boolean
Determines whether or not thisRectangle
and the specifiedRectangle
intersect.boolean
isEmpty()
Determines whether or not thisRectangle
is empty.void
setBounds
(int x, int y, int w, int h) Sets the bounds of the rectanglevoid
Sets the bounds of the rectanglevoid
Sets the bounds of the rectanglevoid
setHeight
(int height) Sets the height.void
setLocation
(int x, int y) void
void
setSize
(int w, int h) void
void
setWidth
(int width) Sets the width.void
setX
(int x) Sets the x.void
setY
(int y) Sets the y.toString()
Debuggingvoid
translate
(int x, int y) Translate the rectangle by an amount.void
translate
(ReadablePoint point) Translate the rectangle by an amount.union
(ReadableRectangle r, WritableRectangle dest) Computes the union of thisRectangle
with the specifiedRectangle
.void
untranslate
(ReadablePoint point) Un-translate the rectangle by an amount.
-
Constructor Details
-
Rectangle
public Rectangle()Constructor for Rectangle. -
Rectangle
public Rectangle(int x, int y, int w, int h) Constructor for Rectangle. -
Rectangle
Constructor for Rectangle. -
Rectangle
Constructor for Rectangle.
-
-
Method Details
-
setLocation
public void setLocation(int x, int y) - Specified by:
setLocation
in interfaceWritablePoint
-
setLocation
- Specified by:
setLocation
in interfaceWritablePoint
-
setSize
public void setSize(int w, int h) - Specified by:
setSize
in interfaceWritableDimension
-
setSize
- Specified by:
setSize
in interfaceWritableDimension
-
setBounds
public void setBounds(int x, int y, int w, int h) Description copied from interface:WritableRectangle
Sets the bounds of the rectangle- Specified by:
setBounds
in interfaceWritableRectangle
- Parameters:
x
- Position of rectangle on x axisy
- Position of rectangle on y axisw
- Width of rectangleh
- Height of rectangle
-
setBounds
Description copied from interface:WritableRectangle
Sets the bounds of the rectangle- Specified by:
setBounds
in interfaceWritableRectangle
- Parameters:
p
-d
-
-
setBounds
Description copied from interface:WritableRectangle
Sets the bounds of the rectangle- Specified by:
setBounds
in interfaceWritableRectangle
- Parameters:
r
-
-
getBounds
Description copied from interface:ReadableRectangle
Copy this readable rectangle's bounds into a destination Rectangle- Specified by:
getBounds
in interfaceReadableRectangle
- Parameters:
dest
- The destination Rectangle, or null, to create a new Rectangle
-
getLocation
Description copied from interface:ReadablePoint
Copy this ReadablePoint into a destination Point- Specified by:
getLocation
in interfaceReadablePoint
- Parameters:
dest
- The destination Point, or null, to create a new Point
-
getSize
Description copied from interface:ReadableDimension
Copy this ReadableDimension into a destination Dimension- Specified by:
getSize
in interfaceReadableDimension
- Parameters:
dest
- The destination
-
translate
public void translate(int x, int y) Translate the rectangle by an amount.- Parameters:
x
- The translation amount on the x axisy
- The translation amount on the y axis
-
translate
Translate the rectangle by an amount.- Parameters:
point
- The translation amount
-
untranslate
Un-translate the rectangle by an amount.- Parameters:
point
- The translation amount
-
contains
Checks whether or not thisRectangle
contains the specifiedPoint
.- Parameters:
p
- thePoint
to test- Returns:
true
if thePoint
(x, y) is inside thisRectangle
;false
otherwise.
-
contains
public boolean contains(int X, int Y) Checks whether or not thisRectangle
contains the point at the specified location (x, y).- Parameters:
X
- the specified x coordinateY
- the specified y coordinate- Returns:
true
if the point (x, y) is inside thisRectangle
;false
otherwise.
-
contains
Checks whether or not thisRectangle
entirely contains the specifiedRectangle
.- Parameters:
r
- the specifiedRectangle
- Returns:
true
if theRectangle
is contained entirely inside thisRectangle
;false
otherwise.
-
contains
public boolean contains(int X, int Y, int W, int H) Checks whether thisRectangle
entirely contains theRectangle
at the specified location (X, Y) with the specified dimensions (W, H).- Parameters:
X
- the specified x coordinateY
- the specified y coordinateW
- the width of theRectangle
H
- the height of theRectangle
- Returns:
true
if theRectangle
specified by (X, Y, W, H) is entirely enclosed inside thisRectangle
;false
otherwise.
-
intersects
Determines whether or not thisRectangle
and the specifiedRectangle
intersect. Two rectangles intersect if their intersection is nonempty.- Parameters:
r
- the specifiedRectangle
- Returns:
true
if the specifiedRectangle
and thisRectangle
intersect;false
otherwise.
-
intersection
Computes the intersection of thisRectangle
with the specifiedRectangle
. Returns a newRectangle
that represents the intersection of the two rectangles. If the two rectangles do not intersect, the result will be an empty rectangle.- Parameters:
r
- the specifiedRectangle
- Returns:
- the largest
Rectangle
contained in both the specifiedRectangle
and in thisRectangle
; or if the rectangles do not intersect, an empty rectangle.
-
union
Computes the union of thisRectangle
with the specifiedRectangle
. Returns a newRectangle
that represents the union of the two rectangles- Parameters:
r
- the specifiedRectangle
- Returns:
- the smallest
Rectangle
containing both the specifiedRectangle
and thisRectangle
.
-
add
public void add(int newx, int newy) Adds a point, specified by the integer argumentsnewx
andnewy
, to thisRectangle
. The resultingRectangle
is the smallestRectangle
that contains both the originalRectangle
and the specified point.After adding a point, a call to
contains
with the added point as an argument does not necessarily returntrue
. Thecontains
method does not returntrue
for points on the right or bottom edges of aRectangle
. Therefore, if the added point falls on the right or bottom edge of the enlargedRectangle
,contains
returnsfalse
for that point.- Parameters:
newx
- the x coordinates of the new pointnewy
- the y coordinates of the new point
-
add
Adds the specifiedPoint
to thisRectangle
. The resultingRectangle
is the smallestRectangle
that contains both the originalRectangle
and the specifiedPoint
.After adding a
Point
, a call tocontains
with the addedPoint
as an argument does not necessarily returntrue
. Thecontains
method does not returntrue
for points on the right or bottom edges of aRectangle
. Therefore if the addedPoint
falls on the right or bottom edge of the enlargedRectangle
,contains
returnsfalse
for thatPoint
.- Parameters:
pt
- the newPoint
to add to thisRectangle
-
add
Adds aRectangle
to thisRectangle
. The resultingRectangle
is the union of the two rectangles.- Parameters:
r
- the specifiedRectangle
-
grow
public void grow(int h, int v) Resizes theRectangle
both horizontally and vertically.This method modifies the
Rectangle
so that it ish
units larger on both the left and right side, andv
units larger at both the top and bottom.The new
Rectangle
has (x - h
,y - v
) as its top-left corner, a width ofwidth
+
2h
, and a height ofheight
+
2v
.If negative values are supplied for
h
andv
, the size of theRectangle
decreases accordingly. Thegrow
method does not check whether the resulting values ofwidth
andheight
are non-negative.- Parameters:
h
- the horizontal expansionv
- the vertical expansion
-
isEmpty
public boolean isEmpty()Determines whether or not thisRectangle
is empty. ARectangle
is empty if its width or its height is less than or equal to zero.- Returns:
true
if thisRectangle
is empty;false
otherwise.
-
equals
Checks whether two rectangles are equal.The result is
true
if and only if the argument is notnull
and is aRectangle
object that has the same top-left corner, width, and height as thisRectangle
. -
toString
Debugging -
getHeight
public int getHeight()Gets the height.- Specified by:
getHeight
in interfaceReadableDimension
- Returns:
- Returns a int
-
setHeight
public void setHeight(int height) Sets the height.- Specified by:
setHeight
in interfaceWritableDimension
- Parameters:
height
- The height to set
-
getWidth
public int getWidth()Gets the width.- Specified by:
getWidth
in interfaceReadableDimension
- Returns:
- Returns a int
-
setWidth
public void setWidth(int width) Sets the width.- Specified by:
setWidth
in interfaceWritableDimension
- Parameters:
width
- The width to set
-
getX
public int getX()Gets the x.- Specified by:
getX
in interfaceReadablePoint
- Returns:
- Returns a int
-
setX
public void setX(int x) Sets the x.- Specified by:
setX
in interfaceWritablePoint
- Parameters:
x
- The x to set
-
getY
public int getY()Gets the y.- Specified by:
getY
in interfaceReadablePoint
- Returns:
- Returns a int
-
setY
public void setY(int y) Sets the y.- Specified by:
setY
in interfaceWritablePoint
- Parameters:
y
- The y to set
-