Index (Frames) | Index (No Frames) | Package | Package Tree | Tree
java.awt

Class Rectangle

java.lang.Object
|
+--java.awt.geom.RectangularShape
   |
   +--java.awt.geom.Rectangle2D
      |
      +--java.awt.Rectangle

All Implemented Interfaces:

Shape, Serializable, Shape, Cloneable


public class Rectangle

extends Rectangle2D

implements Shape, Serializable

This class represents a rectangle and all the interesting things you might want to do with it. Note that the coordinate system uses the origin (0,0) as the top left of the screen, with the x and y values increasing as they move to the right and down respectively.

It is valid for a rectangle to have negative width or height; but it is considered to have no area or internal points. Therefore, the behavior in methods like contains or intersects is undefined unless the rectangle has positive width and height.

There are some public fields; if you mess with them in an inconsistent manner, it is your own fault when you get NullPointerException, ArrayIndexOutOfBoundsException, or invalid results. Also, this class is not threadsafe.

Since:Authors:

Field Summary

intheight

The height of the rectangle.
intwidth

The width of the rectangle.
intx

The X coordinate of the top-left corner of the rectangle.
inty

The Y coordinate of the top-left corner of the rectangle.

Constructor Summary

Rectangle()

Initializes a new instance of Rectangle with a top left corner at (0,0) and a width and height of 0.
Rectangle(java.awt.Rectangle r)

Initializes a new instance of Rectangle from the coordinates of the specified rectangle.
Rectangle(int x, int y, int width, int height)

Initializes a new instance of Rectangle from the specified inputs.
Rectangle(int width, int height)

Initializes a new instance of Rectangle with the specified width and height.
Rectangle(java.awt.Point p, java.awt.Dimension d)

Initializes a new instance of Rectangle with a top-left corner represented by the specified point and the width and height represented by the specified dimension.
Rectangle(java.awt.Point p)

Initializes a new instance of Rectangle with a top left corner at the specified point and a width and height of zero.
Rectangle(java.awt.Dimension d)

Initializes a new instance of Rectangle with an upper left corner at the origin (0,0) and a width and height represented by the specified dimension.

Method Summary

voidadd(int x, int y)

Modifies this rectangle so that it represents the smallest rectangle that contains both the existing rectangle and the specified point.
voidadd(java.awt.Point p)

Modifies this rectangle so that it represents the smallest rectangle that contains both the existing rectangle and the specified point.
voidadd(java.awt.Rectangle r)

Modifies this rectangle so that it represents the smallest rectangle that contains both the existing rectangle and the specified rectangle.
booleancontains(java.awt.Point p)

Tests whether or not the specified point is inside this rectangle.
booleancontains(int x, int y)

Tests whether or not the specified point is inside this rectangle.
booleancontains(java.awt.Rectangle r)

Checks whether all points in the given rectangle are contained in this rectangle.
booleancontains(int x, int y, int w, int h)

Checks whether all points in the given rectangle are contained in this rectangle.
java.awt.geom.Rectangle2DcreateIntersection(java.awt.geom.Rectangle2D r)

Determines the rectangle which is formed by the intersection of this rectangle with the specified rectangle.
java.awt.geom.Rectangle2DcreateUnion(java.awt.geom.Rectangle2D r)

Returns the smallest rectangle that contains both this rectangle and the specified rectangle.
booleanequals(java.lang.Object obj)

Tests this rectangle for equality against the specified object.
java.awt.RectanglegetBounds()

Returns the bounds of this rectangle.
java.awt.geom.Rectangle2DgetBounds2D()

Returns the high-precision bounds of this rectangle.
doublegetHeight()

Get the height of the rectangle.
java.awt.PointgetLocation()

Returns the location of this rectangle, which is the coordinates of its upper left corner.
java.awt.DimensiongetSize()

Returns the size of this rectangle.
doublegetWidth()

Get the width of the rectangle.
doublegetX()

Get the X coordinate of the upper-left corner.
doublegetY()

Get the Y coordinate of the upper-left corner.
voidgrow(int h, int v)

Expands the rectangle by the specified amount.
booleaninside(int x, int y)

Tests whether or not the specified point is inside this rectangle.
java.awt.Rectangleintersection(java.awt.Rectangle r)

Determines the rectangle which is formed by the intersection of this rectangle with the specified rectangle.
booleanintersects(java.awt.Rectangle r)

Tests whether or not the specified rectangle intersects this rectangle.
booleanisEmpty()

Tests whether or not this rectangle is empty.
voidmove(int x, int y)

Moves the location of this rectangle by setting its upper left corner to the specified coordinates.
intoutcode(double x, double y)

Determine where the point lies with respect to this rectangle.
voidreshape(int x, int y, int width, int height)

Updates this rectangle to have the specified dimensions.
voidresize(int width, int height)

Sets the size of this rectangle based on the specified dimensions.
voidsetBounds(java.awt.Rectangle r)

Updates this rectangle to match the dimensions of the specified rectangle.
voidsetBounds(int x, int y, int width, int height)

Updates this rectangle to have the specified dimensions.
voidsetLocation(java.awt.Point p)

Moves the location of this rectangle by setting its upper left corner to the specified point.
voidsetLocation(int x, int y)

Moves the location of this rectangle by setting its upper left corner to the specified coordinates.
voidsetRect(double x, double y, double width, double height)

Updates this rectangle to have the specified dimensions, as rounded to integers.
voidsetSize(java.awt.Dimension d)

Sets the size of this rectangle based on the specified dimensions.
voidsetSize(int width, int height)

Sets the size of this rectangle based on the specified dimensions.
java.lang.StringtoString()

Returns a string representation of this rectangle.
voidtranslate(int dx, int dy)

Translate the location of this rectangle by the given amounts.
java.awt.Rectangleunion(java.awt.Rectangle r)

Returns the smallest rectangle that contains both this rectangle and the specified rectangle.

Field Details

height

public int height

The height of the rectangle.

See Also:


width

public int width

The width of the rectangle.

See Also:


x

public int x

The X coordinate of the top-left corner of the rectangle.

See Also:


y

public int y

The Y coordinate of the top-left corner of the rectangle.

See Also:


Constructor Details

Rectangle

public Rectangle()

Initializes a new instance of Rectangle with a top left corner at (0,0) and a width and height of 0.


Rectangle

public Rectangle(int width, int height)

Initializes a new instance of Rectangle with the specified width and height. The upper left corner of the rectangle will be at the origin (0,0).

Parameters:


Rectangle

public Rectangle(int x, int y, int width, int height)

Initializes a new instance of Rectangle from the specified inputs.

Parameters:


Rectangle

public Rectangle(java.awt.Dimension d)

Initializes a new instance of Rectangle with an upper left corner at the origin (0,0) and a width and height represented by the specified dimension.

Parameters:


Rectangle

public Rectangle(java.awt.Point p)

Initializes a new instance of Rectangle with a top left corner at the specified point and a width and height of zero.

Parameters:


Rectangle

public Rectangle(java.awt.Point p, java.awt.Dimension d)

Initializes a new instance of Rectangle with a top-left corner represented by the specified point and the width and height represented by the specified dimension.

Parameters:

Throws:


Rectangle

public Rectangle(java.awt.Rectangle r)

Initializes a new instance of Rectangle from the coordinates of the specified rectangle.

Since:Parameters:

Throws:


Method Details

add

public void add(int x, int y)

Modifies this rectangle so that it represents the smallest rectangle that contains both the existing rectangle and the specified point. However, if the point falls on one of the two borders which are not inside the rectangle, a subsequent call to contains may return false.

Parameters:


add

public void add(java.awt.Point p)

Modifies this rectangle so that it represents the smallest rectangle that contains both the existing rectangle and the specified point. However, if the point falls on one of the two borders which are not inside the rectangle, a subsequent call to contains may return false.

Parameters:

Throws:


add

public void add(java.awt.Rectangle r)

Modifies this rectangle so that it represents the smallest rectangle that contains both the existing rectangle and the specified rectangle.

Parameters:

Throws:

See Also:


contains

public boolean contains(int x, int y)

Tests whether or not the specified point is inside this rectangle. According to the contract of Shape, a point on the border is in only if it has an adjacent point inside the rectangle in either the increasing x or y direction.

Since:Parameters:

Returns:


contains

public boolean contains(int x, int y, int w, int h)

Checks whether all points in the given rectangle are contained in this rectangle.

Since:Parameters:

Returns:


contains

public boolean contains(java.awt.Point p)

Tests whether or not the specified point is inside this rectangle. According to the contract of Shape, a point on the border is in only if it has an adjacent point inside the rectangle in either the increasing x or y direction.

Since:Parameters:

Returns:

Throws:

See Also:


contains

public boolean contains(java.awt.Rectangle r)

Checks whether all points in the given rectangle are contained in this rectangle.

Since:Parameters:

Returns:

Throws:

See Also:


createIntersection

public Rectangle2D createIntersection(java.awt.geom.Rectangle2D r)

Determines the rectangle which is formed by the intersection of this rectangle with the specified rectangle. If the two do not intersect, an empty rectangle will be returned (meaning the width and/or height will be non-positive).

Since:Parameters:

Returns:

Throws:


createUnion

public Rectangle2D createUnion(java.awt.geom.Rectangle2D r)

Returns the smallest rectangle that contains both this rectangle and the specified rectangle.

Since:Parameters:

Returns:

Throws:


equals

public boolean equals(java.lang.Object obj)

Tests this rectangle for equality against the specified object. This will be true if an only if the specified object is an instance of Rectangle2D with the same coordinates and dimensions.

Parameters:

Returns:


getBounds

public Rectangle getBounds()

Returns the bounds of this rectangle. A pretty useless method, as this is already a rectangle; it is included to mimic the getBounds method in Component.

Since:Returns:

See Also:


getBounds2D

public Rectangle2D getBounds2D()

Returns the high-precision bounds of this rectangle. A pretty useless method, as this is already a rectangle.

Since:Returns:

See Also:


getHeight

public double getHeight()

Get the height of the rectangle.

Returns:


getLocation

public Point getLocation()

Returns the location of this rectangle, which is the coordinates of its upper left corner.

Since:Returns:

See Also:


getSize

public Dimension getSize()

Returns the size of this rectangle.

Since:Returns:

See Also:


getWidth

public double getWidth()

Get the width of the rectangle.

Returns:


getX

public double getX()

Get the X coordinate of the upper-left corner.

Returns:


getY

public double getY()

Get the Y coordinate of the upper-left corner.

Returns:


grow

public void grow(int h, int v)

Expands the rectangle by the specified amount. The horizontal and vertical expansion values are applied both to the X,Y coordinate of this rectangle, and its width and height. Thus the width and height will increase by 2h and 2v accordingly.

Parameters:


inside

public boolean inside(int x, int y)

Tests whether or not the specified point is inside this rectangle.

Parameters:

Returns:


intersection

public Rectangle intersection(java.awt.Rectangle r)

Determines the rectangle which is formed by the intersection of this rectangle with the specified rectangle. If the two do not intersect, an empty rectangle will be returned (meaning the width and/or height will be non-positive).

Parameters:

Returns:

Throws:


intersects

public boolean intersects(java.awt.Rectangle r)

Tests whether or not the specified rectangle intersects this rectangle. This means the two rectangles share at least one internal point.

Since:Parameters:

Returns:

Throws:


isEmpty

public boolean isEmpty()

Tests whether or not this rectangle is empty. An empty rectangle has a non-positive width or height.

Returns:


move

public void move(int x, int y)

Moves the location of this rectangle by setting its upper left corner to the specified coordinates.

Parameters:


outcode

public int outcode(double x, double y)

Determine where the point lies with respect to this rectangle. The result will be the binary OR of the appropriate bit masks.

Since:Parameters:

Returns:

See Also:


reshape

public void reshape(int x, int y, int width, int height)

Updates this rectangle to have the specified dimensions.

Parameters:


resize

public void resize(int width, int height)

Sets the size of this rectangle based on the specified dimensions.

Parameters:


setBounds

public void setBounds(int x, int y, int width, int height)

Updates this rectangle to have the specified dimensions.

Since:Parameters:


setBounds

public void setBounds(java.awt.Rectangle r)

Updates this rectangle to match the dimensions of the specified rectangle.

Since:Parameters:

Throws:

See Also:


setLocation

public void setLocation(int x, int y)

Moves the location of this rectangle by setting its upper left corner to the specified coordinates.

Since:Parameters:


setLocation

public void setLocation(java.awt.Point p)

Moves the location of this rectangle by setting its upper left corner to the specified point.

Since:Parameters:

Throws:

See Also:


setRect

public void setRect(double x, double y, double width, double height)

Updates this rectangle to have the specified dimensions, as rounded to integers.

Since:Parameters:


setSize

public void setSize(int width, int height)

Sets the size of this rectangle based on the specified dimensions.

Since:Parameters:


setSize

public void setSize(java.awt.Dimension d)

Sets the size of this rectangle based on the specified dimensions.

Since:Parameters:

Throws:

See Also:


toString

public String toString()

Returns a string representation of this rectangle. This is in the form getClass().getName() + "[x=" + x + ",y=" + y + ",width=" + width + ",height=" + height + ']'.

Returns:


translate

public void translate(int dx, int dy)

Translate the location of this rectangle by the given amounts.

Parameters:

See Also:


union

public Rectangle union(java.awt.Rectangle r)

Returns the smallest rectangle that contains both this rectangle and the specified rectangle.

Parameters:

Returns:

Throws: