Rectangle
Name
Rectangle
Description
The Rectangle class represents a rectangle by combining its origin (a pair of x and y coordinates) with its size (a width and a height).
Class Definition
public class java.awt.Rectangle extends java.lang.Object implements java.awt.Shape, java.io.Serializable {
// Variables pubic int height;
public int width;
public int x;
public int y; // Constructors public Rectangle();
public Rectangle (int width, int height);
public Rectangle (int x, int y, int width, int height);
public Rectangle (Dimension d);
public Rectangle (Point p);
public Rectangle (Point p, Dimension d);
public Rectangle (Rectangle r); // Instance Methods public void add (int newX, int newY);
public void add (Point p);
public void add (Rectangle r);
public boolean contains (int x, int y); public boolean contains (Point p); public boolean equals (Object object);
public Rectangle getBounds(); public Point getLocation(); public Dimension getSize(); public void grow (int horizontal, int vertical);
public int hashCode();
public boolean inside (int x, int y);
public Rectangle intersection (Rectangle r);
public boolean intersects (Rectangle r);
public boolean isEmpty();
public void move (int x, int y);
public void reshape (int x, int y, int width, int height);
public void resize (int width, int height);
public void setBounds (Rectangle r); public void setBounds (int x, int y, int width, int height); public void setLocation (int x, int y); public void setLocation (Point p); public void setSize (int width, int height); public void setSize (Dimension d); public String toString();
public void translate (int x, int y);
public Rectangle union (Rectangle r);
}
Variables
height
public int height
The height of the Rectangle.
width
public int width
The width of the Rectangle.
x
public int x
The x coordinate of the Rectangle's upper left corner (its origin).
y
public int y
The y coordinate of the Rectangle's upper left corner (its origin).
Constructors
Rectangle
public Rectangle()
- Description
- Constructs an empty
Rectangleobject with an origin of (0, 0) and dimensions of 0 x 0.
public Rectangle (int width, int height)
- Parameters
-
- width
- width of
Rectangle - height
- height of
Rectangle
- Description
- Constructs a
Rectangleobject with an origin of (0, 0) and dimensions ofwidthxheight.
public Rectangle (int x, int y, int width, int height)
- Parameters
-
- x
- x coordinate of the
Rectangle's origin - y
- y coordinate of the
Rectangle's origin - width
- width of
Rectangle - height
- height of
Rectangle
- Description
- Constructs a
Rectangleobject with an origin of (x,y) and dimensions ofwidthxheight.
public Rectangle (Dimension d)
- Parameters
-
- d
- dimensions of
Rectangle
- Description
- Constructs a
Rectangleobject with an origin of (0, 0) and dimensions ofd.widthxd.height.
public Rectangle (Point p)
- Parameters
-
- p
- origin of
Rectangle
- Description
- Constructs an empty
Rectangleobject with an origin of (p.x,p.y) and dimensions of 0 x 0.
public Rectangle (Point p, Dimension d)
- Parameters
-
- p
- origin of
Rectangle - d
- dimensions of
Rectangle
- Description
- Constructs a
Rectangleobject with an origin of (p.x,p.y) and dimensions ofd.widthxd.height.
public Rectangle (Rectangle r)
- Parameters
-
- r
- original
Rectangle
- Description
- Constructs copy of the given
Rectangle.
Instance Methods
add
public void add (int newX, int newY)
- Parameters
-
- newX
- The x-coordinate of a point to incorporate within the
Rectangle. - newY
- The y-coordinate of a point to incorporate within the
Rectangle.
- Description
- Extends the
Rectangleso that the point (newX,newY) is within it.
public void add (Point p)
- Parameters
-
- p
- The new
Pointto add to theRectangle.
- Description
- Extends the
Rectangleso that the pointpis within it.
public void add (Rectangle r)
- Parameters
-
- r
- The
Rectanglebeing added to the currentRectangle.
- Description
- Extends the
Rectangleto include theRectangler.
contains
public boolean contains (int x, int y)
- Parameters
-
- x
- The x coordinate to test.
- y
- The y coordinate to test.
- Returns
trueif theRectanglecontains the point;falseotherwise.
public boolean contains (Point p)
- Parameters
-
- p
- The point to be tested.
- Returns
trueif theRectanglecontains the point;falseotherwise.
equals
public boolean equals (Object object)
- Parameters
-
- object
- The object to compare.
- Returns
trueif bothRectangles have the same origin, width, and height;falseotherwise.- Overrides
Object.equals(Object)- Description
- Compares two different
Rectangleinstances for equivalence.
getBounds
public Rectangle getBounds()
- Implements
Shape.getBounds()- Returns
- Bounding
Rectangle.
getLocation
public Point getLocation()
- Returns
- Position of the rectangle.
- Description
- Gets the current position of this
Rectangle.
getSize
public Dimension getSize()
- Returns
- Dimensions of the rectangle.
- Description
- Gets width and height of the rectangle.
grow
public void grow (int horizontal, int vertical)
- Parameters
-
- horizontal
- Amount to extend
Rectanglein horizontal direction on both the left and right sides. - vertical
- Amount to extend
Rectanglein vertical direction on both the top and the bottom.
- Description
- Increases the rectangle's dimensions.
hashCode
public int hashCode()
- Returns
- A hashcode to use when using the
Rectangleas a key in aHashtable. - Overrides
Object.hashCode()- Description
- Generates a hashcode for the
Rectangle.
inside
public boolean inside (int x, int y) 
- Parameters
-
- x
- The x coordinate to check.
- y
- The y coordinate to check.
- Returns
trueif (x,y) falls within theRectangle,falseotherwise.- Description
- Checks to see if the point (
x,y) is within theRectangle. Replaced bycontains(int, int).
intersection
public Rectangle intersection (Rectangle r)
- Parameters
-
- r
Rectangleto add to the currentRectangle.
- Returns
- A new
Rectangleconsisting of all points in both the currentRectangleandr. - Description
- Generates a new
Rectanglethat is the intersection ofrand the currentRectangle.
intersects
public boolean intersects (Rectangle r)
- Parameters
-
- r
Rectangleto check.
- Returns
trueif any points inrare also in the currentRectangle,falseotherwise.- Description
- Checks to see if
rcrosses theRectangle.
isEmpty
public boolean isEmpty()
- Returns
trueif theRectangleis empty,falseotherwise.- Description
- Determines if the rectangle is dimensionless (i.e., width or height are less than or equal to 0).
move
public void move (int x, int y) 
- Parameters
-
- x
- The new x coordinate of the
Rectangle's upper left corner. - y
- The new y coordinate of the
Rectangle's upper left corner.
- Description
- Changes the
Rectangle's origin to (x,y). Replaced bysetLocation(int, int).
reshape
public void reshape (int x, int y, int width, int height) 
- Parameters
-
- x
- The new x coordinate of the
Rectangle's upper left corner. - y
- The new y coordinate of the
Rectangle's upper left corner. - width
- The new width.
- height
- The new height.
- Description
- Changes
Rectangle's origin and dimensions. Replaced bysetBounds(int, int, int, int).
resize
public void resize (int width, int height) 
- Parameters
-
- width
- The new width.
- height
- The new height.
- Description
- Changes
Rectangle's dimensions. Replaced bysetSize(int, int).
setBounds
public void setBounds (Rectangle r)
- Parameters
-
- r
- A
Rectangledescribing the new bounds.
- Description
- Changes
Rectangle's location and size.
public void setBounds (int x, int y, int width, int height) [New in 1.1]
- Parameters
-
- x
- The new x coordinate of the
Rectangle's upper left corner. - y
- The new y coordinate of the
Rectangle's upper left corner. - width
- The new width.
- height
- The new height.
- Description
- Changes
Rectangle's location and size.
setLocation
public void setLocation (int x, int y)
- Parameters
-
- x
- New horizontal position.
- y
- New vertical position.
- Description
- Relocates the rectangle.
public void setLocation (Point p)
- Parameters
-
- p
- New position for component.
- Description
- Relocates the rectangle.
setSize
public void setSize (int width, int height)
- Parameters
-
- width
- New width.
- height
- New height.
- Description
- Resizes the rectangle.
public void setSize (Dimension d)
- Parameters
-
- d
- New dimensions.
- Description
- Resizes the rectangle.
toString
public String toString()
- Returns
- A string representation of the
Rectangleobject. - Overrides
Object.toString()
translate
public void translate (int deltax, int deltay)
- Parameters
-
- deltax
- Amount to move
Rectanglehorizontally. - deltay
- Amount to move
Rectanglevertically.
- Description
- Moves the
Rectangle's origin to (x+deltax,y+deltay).
union
public Rectangle union (Rectangle r)
- Parameters
-
- r
- Rectangle to determine union with.
- Returns
- The smallest
Rectanglecontaining bothrand the currentRectangle. - Description
- Generates a new
Rectangleby combiningrand the currentRectangle.
See Also
Dimension, Object, Point, String