Java Reference
In-Depth Information
Rectangle Objects
As discussed earlier, the Rectangle class defines four public data members, all of type int . The
position of a Rectangle object is defined by the members x and y , and its size is defined by the
members width and height . As they are all public class members, you can retrieve or modify any
of these directly, but your code will be a little more readable if you use the methods provided.
There are no less than seven constructors that you can use:
Constructor
Description
Rectangle()
Creates a rectangle at (0, 0) with zero width
and height.
Rectangle(int x, int y,
int width, int height)
Creates a rectangle at ( x , y) with the
specified width and height.
Rectangle(int width, int height)
Creates a rectangle at (0, 0) with the
specified width and height.
Rectangle(Point p,
Dimension d)
Creates a rectangle at point p with the
width and height specified by d .
Rectangle(Point p)
Creates a rectangle at point p with zero
width and height.
Rectangle(Dimension d)
Creates a rectangle at (0, 0) with the width
and height specified by d .
Rectangle(Rectangle r)
Creates a rectangle with the same position
and dimensions as r .
You can retrieve or modify the position of a Rectangle object using the method getLocation() that
returns a Point object, and setLocation() that comes in two versions, one of which requires x and y
coordinates of the new position as arguments and the other, which requires a Point object. You can also
apply the translate() method to a Rectangle object, in the same way as the Point object.
To retrieve or modify the size of a Rectangle object you use the methods getSize() , which returns
a Dimension object, and setSize() , which requires either a Dimension object specifying the new
size as an argument, or two arguments corresponding to the new width and height values as type int .
Search WWH ::




Custom Search