java.awt

Class Rectangle

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: 1.0

UNKNOWN: updated to 1.4

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(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(Point p, 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(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(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(Point p)
Modifies this rectangle so that it represents the smallest rectangle that contains both the existing rectangle and the specified point.
voidadd(Rectangle r)
Modifies this rectangle so that it represents the smallest rectangle that contains both the existing rectangle and the specified rectangle.
booleancontains(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(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.
Rectangle2DcreateIntersection(Rectangle2D r)
Determines the rectangle which is formed by the intersection of this rectangle with the specified rectangle.
Rectangle2DcreateUnion(Rectangle2D r)
Returns the smallest rectangle that contains both this rectangle and the specified rectangle.
booleanequals(Object obj)
Tests this rectangle for equality against the specified object.
RectanglegetBounds()
Returns the bounds of this rectangle.
Rectangle2DgetBounds2D()
Returns the high-precision bounds of this rectangle.
doublegetHeight()
Get the height of the rectangle.
PointgetLocation()
Returns the location of this rectangle, which is the coordinates of its upper left corner.
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.
Rectangleintersection(Rectangle r)
Determines the rectangle which is formed by the intersection of this rectangle with the specified rectangle.
booleanintersects(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(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(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, rounded to the integer precision used by this class (the values are rounded "outwards" so that the stored rectangle completely encloses the specified double precision rectangle).
voidsetSize(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.
StringtoString()
Returns a string representation of this rectangle.
voidtranslate(int dx, int dy)
Translate the location of this rectangle by the given amounts.
Rectangleunion(Rectangle r)
Returns the smallest rectangle that contains both this rectangle and the specified rectangle.

Field Detail

height

public int height
The height of the rectangle.

Serial:

See Also: Rectangle getSize

width

public int width
The width of the rectangle.

Serial:

See Also: Rectangle getSize

x

public int x
The X coordinate of the top-left corner of the rectangle.

Serial: the x coordinate

See Also: Rectangle getLocation

y

public int y
The Y coordinate of the top-left corner of the rectangle.

Serial: the y coordinate

See Also: Rectangle getLocation

Constructor Detail

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(Rectangle r)
Initializes a new instance of Rectangle from the coordinates of the specified rectangle.

Parameters: r the rectangle to copy from

Since: 1.1

Rectangle

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

Parameters: x the X coordinate of the top left corner y the Y coordinate of the top left corner width the width of the rectangle height the height of the rectangle

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: width the width of the rectangle height the height of the rectange

Rectangle

public Rectangle(Point p, 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: p the upper left corner of the rectangle d the width and height of the rectangle

Rectangle

public Rectangle(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: p the upper left corner of the rectangle

Rectangle

public Rectangle(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: d the width and height of the rectangle

Method Detail

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: x the X coordinate of the point to add to this rectangle y the Y coordinate of the point to add to this rectangle

add

public void add(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: p the point to add to this rectangle

Throws: NullPointerException if p is null

add

public void add(Rectangle r)
Modifies this rectangle so that it represents the smallest rectangle that contains both the existing rectangle and the specified rectangle.

Parameters: r the rectangle to add to this rectangle

Throws: NullPointerException if r is null

See Also: union

contains

public boolean contains(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.

Parameters: p the point to test

Returns: true if the point is inside the rectangle

Throws: NullPointerException if p is null

Since: 1.1

See Also: Rectangle

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.

Parameters: x the X coordinate of the point to test y the Y coordinate of the point to test

Returns: true if the point is inside the rectangle

Since: 1.1

contains

public boolean contains(Rectangle r)
Checks whether all points in the given rectangle are contained in this rectangle.

Parameters: r the rectangle to check

Returns: true if r is contained in this rectangle

Throws: NullPointerException if r is null

Since: 1.1

See Also: Rectangle

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.

Parameters: x the x coordinate of the rectangle to check y the y coordinate of the rectangle to check w the width of the rectangle to check h the height of the rectangle to check

Returns: true if the parameters are contained in this rectangle

Since: 1.1

createIntersection

public Rectangle2D createIntersection(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).

Parameters: r the rectange to calculate the intersection with

Returns: a new rectangle bounding the intersection

Throws: NullPointerException if r is null

Since: 1.2

createUnion

public Rectangle2D createUnion(Rectangle2D r)
Returns the smallest rectangle that contains both this rectangle and the specified rectangle.

Parameters: r the rectangle to compute the union with

Returns: the smallest rectangle containing both rectangles

Throws: NullPointerException if r is null

Since: 1.2

equals

public boolean equals(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: obj the object to test against for equality

Returns: true if the specified object is equal to this one

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.

Returns: a copy of this rectangle

Since: 1.1

See Also: setBounds

getBounds2D

public Rectangle2D getBounds2D()
Returns the high-precision bounds of this rectangle. A pretty useless method, as this is already a rectangle.

Returns: a copy of this rectangle

Since: 1.2

See Also: setBounds

getHeight

public double getHeight()
Get the height of the rectangle.

Returns: the value of height, as a double

getLocation

public Point getLocation()
Returns the location of this rectangle, which is the coordinates of its upper left corner.

Returns: the point where this rectangle is located

Since: 1.1

See Also: setLocation

getSize

public Dimension getSize()
Returns the size of this rectangle.

Returns: the size of this rectangle

Since: 1.1

See Also: setSize

getWidth

public double getWidth()
Get the width of the rectangle.

Returns: the value of width, as a double

getX

public double getX()
Get the X coordinate of the upper-left corner.

Returns: the value of x, as a double

getY

public double getY()
Get the Y coordinate of the upper-left corner.

Returns: the value of y, as a double

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: h the horizontal expansion value v the vertical expansion value

inside

public boolean inside(int x, int y)

Deprecated: use {@link #contains(int, int)} instead

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

Parameters: x the X coordinate of the point to test y the Y coordinate of the point to test

Returns: true if the point is inside the rectangle

intersection

public Rectangle intersection(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: r the rectange to calculate the intersection with

Returns: a new rectangle bounding the intersection

Throws: NullPointerException if r is null

intersects

public boolean intersects(Rectangle r)
Tests whether or not the specified rectangle intersects this rectangle. This means the two rectangles share at least one internal point.

Parameters: r the rectangle to test against

Returns: true if the specified rectangle intersects this one

Throws: NullPointerException if r is null

Since: 1.2

isEmpty

public boolean isEmpty()
Tests whether or not this rectangle is empty. An empty rectangle has a non-positive width or height.

Returns: true if the rectangle is empty

move

public void move(int x, int y)

Deprecated: use {@link #setLocation(int, int)} instead

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

Parameters: x the new X coordinate for this rectangle y the new Y coordinate for this rectangle

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.

Parameters: x the x coordinate to check y the y coordinate to check

Returns: the binary OR of the result

Since: 1.2

See Also: Rectangle Rectangle Rectangle Rectangle

reshape

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

Deprecated: use {@link #setBounds(int, int, int, int)} instead

Updates this rectangle to have the specified dimensions.

Parameters: x the new X coordinate of the upper left hand corner y the new Y coordinate of the upper left hand corner width the new width of this rectangle height the new height of this rectangle

resize

public void resize(int width, int height)

Deprecated: use {@link #setSize(int, int)} instead

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

Parameters: width the new width of the rectangle height the new height of the rectangle

setBounds

public void setBounds(Rectangle r)
Updates this rectangle to match the dimensions of the specified rectangle.

Parameters: r the rectangle to update from

Throws: NullPointerException if r is null

Since: 1.1

See Also: Rectangle

setBounds

public void setBounds(int x, int y, int width, int height)
Updates this rectangle to have the specified dimensions.

Parameters: x the new X coordinate of the upper left hand corner y the new Y coordinate of the upper left hand corner width the new width of this rectangle height the new height of this rectangle

Since: 1.1

setLocation

public void setLocation(Point p)
Moves the location of this rectangle by setting its upper left corner to the specified point.

Parameters: p the point to move the rectangle to

Throws: NullPointerException if p is null

Since: 1.1

See Also: getLocation

setLocation

public void setLocation(int x, int y)
Moves the location of this rectangle by setting its upper left corner to the specified coordinates.

Parameters: x the new X coordinate for this rectangle y the new Y coordinate for this rectangle

Since: 1.1

setRect

public void setRect(double x, double y, double width, double height)
Updates this rectangle to have the specified dimensions, rounded to the integer precision used by this class (the values are rounded "outwards" so that the stored rectangle completely encloses the specified double precision rectangle).

Parameters: x the new X coordinate of the upper left hand corner y the new Y coordinate of the upper left hand corner width the new width of this rectangle height the new height of this rectangle

Since: 1.2

setSize

public void setSize(Dimension d)
Sets the size of this rectangle based on the specified dimensions.

Parameters: d the new dimensions of the rectangle

Throws: NullPointerException if d is null

Since: 1.1

See Also: getSize

setSize

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

Parameters: width the new width of the rectangle height the new height of the rectangle

Since: 1.1

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: a string representation of this rectangle

translate

public void translate(int dx, int dy)
Translate the location of this rectangle by the given amounts.

Parameters: dx the x distance to move by dy the y distance to move by

See Also: Rectangle

union

public Rectangle union(Rectangle r)
Returns the smallest rectangle that contains both this rectangle and the specified rectangle.

Parameters: r the rectangle to compute the union with

Returns: the smallest rectangle containing both rectangles

Throws: NullPointerException if r is null