Rectangle
The Rectangle
class encapsulates x and y coordinates and width and height (Point
and Dimension
information) within a single object. It is often used by methods that return a rectangular boundary as a single object: for example, Polygon.getBounds()
, Component.getBounds()
, and Graphics.getClipBounds()
. Like Point
, the Rectangle
class is not a visual object and does not represent a rectangle on the screen; ironically, drawRect()
and fillRect()
don't take Rectangle
as an argument.
Rectangle Methods
VariablesThe four public variables available for Rectangle
have the same names as the public instance variables of Point
and Dimension
. They are all accessible directly or through use of the getBounds()
method.
- public int x
- The x coordinate of the upper left corner.
- public int y
- The y coordinate of the upper left corner.
- public int width
- The width variable represents the size of the
Rectangle
along the horizontal axis (left to right). Width should not be negative; however, there is nothing within the class to prevent this from happening. - public int height
- The height variable represents the size of the
Rectangle
along the vertical axis (top to bottom). Height should not be negative; however, there is nothing within the class to prevent this from happening.
The following seven constructors create Rectangle
objects. When you create a Rectangle
, you provide the location of the top left corner, along with the Rectangle
's width and height. A Rectangle
located at (0,0) with a width and height of 100 has its bottom right corner at (99, 99). The Point
(100, 100) lies outside the Rectangle
, since that would require a width and height of 101.
- public Rectangle ()
- This
Rectangle
constructor creates aRectangle
object in which x, y, width, and height are all 0. - public Rectangle (int width, int height)
- This
Rectangle
constructor creates aRectangle
with (x
,y
) coordinates of (0,0) and the specifiedwidth
andheight
. Notice that there is noRectangle(int x, int y)
constructor because that would have the same method signature as this one, and the compiler would have no means to differentiate them. - public Rectangle (int x, int y, int width, int height)
- The
Rectangle
constructor creates aRectangle
object with an initial x coordinate ofx
, y coordinate ofy
, width ofwidth
, and height ofheight
. Height and width should be positive, but the constructor does not check for this. - public Rectangle (Rectangle r)
- This
Rectangle
constructor creates aRectangle
matching the original. The (x
,y
) coordinates are (r.x
,r.y
), with a width ofr.width
and a height ofr.height
. - public Rectangle (Point p, Dimension d)
- This
Rectangle
constructor creates aRectangle
with (x
,y
) coordinates of (p.x
,p.y
), a width ofd.width
, and a height ofd.height
. - public Rectangle (Point p)
- This
Rectangle
constructor creates aRectangle
with (x
,y
) coordinates of (p.x
,p.y
). The width and height are both zero. - public Rectangle (Dimension d)
- The last
Rectangle
constructor creates aRectangle
with (x
,y
) coordinates of (0, 0). The initialRectangle
width isd.width
and height isd.height
.
- public Rectangle getBounds()
- The
getBounds()
method returns a copy of the originalRectangle
. - public void setBounds (int x, int y, int width, int height)
public void reshape (int x, int y, int width, int height) - The
setBounds()
method changes the origin of theRectangle
to (x
,y
) and changes the dimensions towidth
byheight
.reshape()
is the Java 1.0 name for this method. - public void setBounds (Rectangle r)
- The
setBounds()
method changes the origin of theRectangle
to (r.x
,r.y
) and changes the dimensions tor.width
byr.height
. - public Point getLocation()
- The
getLocation()
retrieves the current origin of this rectangle as aPoint
. - public void setLocation (int x, int y)
public void move (int x, int y) - The
setLocation()
method changes the origin of theRectangle
to (x
,y
).move()
is the Java 1.0 name for this method. - public void setLocation (Point p)
- The
setLocation()
method changes theRectangle
's origin to (p.x
,p.y
). - public void translate (int x, int y)
- The
translate()
method moves theRectangle
's origin by the amount (x
,y
). If the originalRectangle
's location (r) is (3, 4) and you callr.translate (4, 5)
, then r's location becomes (7, 9). x and y may be negative.translate()
has no effect on theRectangle
's width and height. - public Dimension getSize ()
- The
getSize()
method retrieves the current size of the rectangle as aDimension
. - public void setSize() (int width, int height)
public void resize (int width, int height) - The
setSize()
method changes theRectangle
's dimensions towidth
xheight
.resize()
is the Java 1.0 name for this method. - public void setSize() (Dimension d)
- The
setSize()
method changes theRectangle
's dimensions tod.width
xd.height
. - public void grow (int horizontal, int vertical)
- The
grow()
method increases theRectangle
's dimensions by adding the amounthorizontal
on the left and the right and adding the amountvertical
on the top and bottom. Therefore, all four of the rectangle's variables change. If the original location is (x
,y
), the new location will be (x-horizontal
, y-vertical
) (moving left and up if both values are positive); if the original size is (width,
height
), the new size will be (width+2*horizontal,
height+2*vertical
). Either horizontal or vertical can be negative to decrease the size of theRectangle
. The following code demonstrates the changes:
import java.awt.Rectangle; public class rect { public static void main (String[] args) { Rectangle r = new Rectangle (100, 100, 200, 200); System.out.println (r); r.grow (50, 75); System.out.println (r); r.grow (-25, -50); System.out.println (r); } }
This program produces the following output:
java.awt.Rectangle[x=100,y=100,width=200,height=200] java.awt.Rectangle[x=50,y=25,width=300,height=350] java.awt.Rectangle[x=75,y=75,width=250,height=250]
- public void add (int newX, int newY)
- The
add()
method incorporates the point (newX
,newY
) into theRectangle
. If this point is already in theRectangle
, there is no change. Otherwise, the size of theRectangle
increases to include (newX
,newY
) within itself. - public void add (Point p)
- This
add()
method incorporates the point (p.x
,p.y
) into theRectangle
. If this point is already in theRectangle
, there is no change. Otherwise, the size of theRectangle
increases to include (p.x
,p.y
) within itself. - public void add (Rectangle r)
- This
add()
method incorporates anotherRectangle
r
into thisRectangle
. This transforms the current rectangle into the union of the twoRectangle
s. This method might be useful in a drawing program that lets you select multiple objects on the screen and create a rectangular area from them.We will soon encounter a method called
union()
that is almost identical.add()
andunion()
differ in thatadd()
modifies the currentRectangle
, whileunion()
returns a newRectangle
. The resulting rectangles are identical.
- public boolean contains (int x, int y)
public boolean inside (int x, int y) - The
contains()
method determines if the point (x
,y
) is within thisRectangle
. If so,true
is returned. If not,false
is returned.inside()
is the Java 1.0 name for this method. - public boolean contains (Point p)
- The
contains()
method determines if the point (p.x
,p.y
) is within thisRectangle
. If so,true
is returned. If not,false
is returned. - public boolean intersects (Rectangle r)
- The
intersects()
method checks whetherRectangle
r
crosses thisRectangle
at any point. If it does,true
is returned. If not,false
is returned. - public Rectangle intersection (Rectangle r)
- The
intersection()
method returns a newRectangle
consisting of all points that are in both the currentRectangle
andRectangle
r
. For example, ifr = new Rectangle (50, 50, 100, 100)
andr1 = new Rectangle (100, 100, 75, 75)
, thenr.intersection (r1)
is theRectangle (100, 100, 50, 50)
, as shown in Figure 2.13.
- public Rectangle union (Rectangle r)
- The
union()
method combines the currentRectangle
andRectangle
r
to form a newRectangle
. For example, ifr = new Rectangle (50, 50, 100, 100)
andr1 = new Rectangle (100, 100, 75, 75)
, thenr.union (r1)
is theRectangle (50, 50, 125, 125)
. The original rectangle is unchanged. Figure 2.14 demonstrates the effect ofunion()
. BecausefillRect()
fills towidth-1
andheight-1
, the rectangle drawn appears slightly smaller than you would expect. However, that's an artifact of how rectangles are drawn; the returned rectangle contains all the points within both.
Figure 2.13: Rectangle intersection
Figure 2.14: Rectangle union
Miscellaneous methods
- public boolean isEmpty ()
- The
isEmpty()
method checks whether there are any points within theRectangle
. If the width and height of theRectangle
are both 0 (or less), theRectangle
is empty, and this method returnstrue
. If either width or height is greater than zero,isEmpty()
returnsfalse
. This method could be used to check the results of a call to any method that returns aRectangle
object. - public int hashCode ()
- The
hashCode()
method returns a hash code for the rectangle. The system calls this method when aRectangle
is used as the key for a hash table. - public boolean equals (Object object)
- The
equals()
method overrides theObject
'sequals()
method to define what equality means forRectangle
objects. TwoRectangle
objects are equal if theirx
,y
, width, and height values are equal. - public String toString ()
- The
toString()
method ofRectangle
displays the current values of thex
,y
, width, and height variables. For example:
java.awt.Rectangle[x=100,y=200,width=300,height=400]