public class Rectangle extends Rectangle2D implements Shape, Serializable
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.
Rectangle2D.Double, Rectangle2D.Float
Modifier and Type | Field and Description |
---|---|
int |
height
The height of the rectangle.
|
int |
width
The width of the rectangle.
|
int |
x
The X coordinate of the top-left corner of the rectangle.
|
int |
y
The Y coordinate of the top-left corner of the rectangle.
|
OUT_BOTTOM, OUT_LEFT, OUT_RIGHT, OUT_TOP
Constructor and Description |
---|
Rectangle()
Initializes a new instance of
Rectangle with a top
left corner at (0,0) and a width and height of 0. |
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. |
Rectangle(int width,
int height)
Initializes a new instance of
Rectangle with the specified
width and height. |
Rectangle(int x,
int y,
int width,
int height)
Initializes a new instance of
Rectangle from the specified
inputs. |
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(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(Rectangle r)
Initializes a new instance of
Rectangle from the
coordinates of the specified rectangle. |
Modifier and Type | Method and Description |
---|---|
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.
|
void |
add(Point p)
Modifies this rectangle so that it represents the smallest rectangle
that contains both the existing rectangle and the specified point.
|
void |
add(Rectangle r)
Modifies this rectangle so that it represents the smallest rectangle
that contains both the existing rectangle and the specified rectangle.
|
boolean |
contains(int x,
int y)
Tests whether or not the specified point is inside this rectangle.
|
boolean |
contains(int x,
int y,
int w,
int h)
Checks whether all points in the given rectangle are contained in this
rectangle.
|
boolean |
contains(Point p)
Tests whether or not the specified point is inside this rectangle.
|
boolean |
contains(Rectangle r)
Checks whether all points in the given rectangle are contained in this
rectangle.
|
Rectangle2D |
createIntersection(Rectangle2D r)
Determines the rectangle which is formed by the intersection of this
rectangle with the specified rectangle.
|
Rectangle2D |
createUnion(Rectangle2D r)
Returns the smallest rectangle that contains both this rectangle
and the specified rectangle.
|
boolean |
equals(Object obj)
Tests this rectangle for equality against the specified object.
|
Rectangle |
getBounds()
Returns the bounds of this rectangle.
|
Rectangle2D |
getBounds2D()
Returns the high-precision bounds of this rectangle.
|
double |
getHeight()
Get the height of the rectangle.
|
Point |
getLocation()
Returns the location of this rectangle, which is the coordinates of
its upper left corner.
|
Dimension |
getSize()
Returns the size of this rectangle.
|
double |
getWidth()
Get the width of the rectangle.
|
double |
getX()
Get the X coordinate of the upper-left corner.
|
double |
getY()
Get the Y coordinate of the upper-left corner.
|
void |
grow(int h,
int v)
Expands the rectangle by the specified amount.
|
boolean |
inside(int x,
int y)
Deprecated.
use
contains(int, int) instead |
Rectangle |
intersection(Rectangle r)
Determines the rectangle which is formed by the intersection of this
rectangle with the specified rectangle.
|
boolean |
intersects(Rectangle r)
Tests whether or not the specified rectangle intersects this rectangle.
|
boolean |
isEmpty()
Tests whether or not this rectangle is empty.
|
void |
move(int x,
int y)
Deprecated.
use
setLocation(int, int) instead |
int |
outcode(double x,
double y)
Determine where the point lies with respect to this rectangle.
|
void |
reshape(int x,
int y,
int width,
int height)
Deprecated.
use
setBounds(int, int, int, int) instead |
void |
resize(int width,
int height)
Deprecated.
use
setSize(int, int) instead |
void |
setBounds(int x,
int y,
int width,
int height)
Updates this rectangle to have the specified dimensions.
|
void |
setBounds(Rectangle r)
Updates this rectangle to match the dimensions of the specified
rectangle.
|
void |
setLocation(int x,
int y)
Moves the location of this rectangle by setting its upper left
corner to the specified coordinates.
|
void |
setLocation(Point p)
Moves the location of this rectangle by setting its upper left
corner to the specified point.
|
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).
|
void |
setSize(Dimension d)
Sets the size of this rectangle based on the specified dimensions.
|
void |
setSize(int width,
int height)
Sets the size of this rectangle based on the specified dimensions.
|
String |
toString()
Returns a string representation of this rectangle.
|
void |
translate(int dx,
int dy)
Translate the location of this rectangle by the given amounts.
|
Rectangle |
union(Rectangle r)
Returns the smallest rectangle that contains both this rectangle
and the specified rectangle.
|
add, add, add, contains, contains, getPathIterator, getPathIterator, hashCode, intersect, intersects, intersectsLine, intersectsLine, outcode, setFrame, setRect, union
clone, contains, contains, getCenterX, getCenterY, getFrame, getMaxX, getMaxY, getMinX, getMinY, intersects, setFrame, setFrame, setFrameFromCenter, setFrameFromCenter, setFrameFromDiagonal, setFrameFromDiagonal
finalize, getClass, notify, notifyAll, wait, wait, wait
contains, contains, contains, contains, getPathIterator, getPathIterator, intersects, intersects
public int x
setLocation(int, int)
,
getLocation()
public int y
setLocation(int, int)
,
getLocation()
public int width
setSize(int, int)
,
getSize()
public int height
setSize(int, int)
,
getSize()
public Rectangle()
Rectangle
with a top
left corner at (0,0) and a width and height of 0.public Rectangle(Rectangle r)
Rectangle
from the
coordinates of the specified rectangle.r
- the rectangle to copy frompublic Rectangle(int x, int y, int width, int height)
Rectangle
from the specified
inputs.x
- the X coordinate of the top left cornery
- the Y coordinate of the top left cornerwidth
- the width of the rectangleheight
- the height of the rectanglepublic Rectangle(int width, int height)
Rectangle
with the specified
width and height. The upper left corner of the rectangle will be at
the origin (0,0).width
- the width of the rectangleheight
- the height of the rectangepublic Rectangle(Point p, Dimension d)
Rectangle
with a top-left
corner represented by the specified point and the width and height
represented by the specified dimension.p
- the upper left corner of the rectangled
- the width and height of the rectanglepublic Rectangle(Point p)
Rectangle
with a top left
corner at the specified point and a width and height of zero.p
- the upper left corner of the rectanglepublic double getX()
getX
in class RectangularShape
public double getY()
getY
in class RectangularShape
public double getWidth()
getWidth
in class RectangularShape
public double getHeight()
getHeight
in class RectangularShape
public Rectangle getBounds()
getBounds
method in Component.getBounds
in interface Shape
getBounds
in class RectangularShape
setBounds(Rectangle)
public Rectangle2D getBounds2D()
getBounds2D
in interface Shape
getBounds2D
in class Rectangle2D
setBounds(Rectangle)
public void setBounds(Rectangle r)
r
- the rectangle to update fromNullPointerException
- if r is nullsetBounds(int, int, int, int)
public void setBounds(int x, int y, int width, int height)
x
- the new X coordinate of the upper left hand cornery
- the new Y coordinate of the upper left hand cornerwidth
- the new width of this rectangleheight
- the new height of this rectanglepublic void setRect(double x, double y, double width, double height)
setRect
in class Rectangle2D
x
- the new X coordinate of the upper left hand cornery
- the new Y coordinate of the upper left hand cornerwidth
- the new width of this rectangleheight
- the new height of this rectanglepublic void reshape(int x, int y, int width, int height)
setBounds(int, int, int, int)
insteadx
- the new X coordinate of the upper left hand cornery
- the new Y coordinate of the upper left hand cornerwidth
- the new width of this rectangleheight
- the new height of this rectanglepublic Point getLocation()
setLocation(Point)
public void setLocation(Point p)
p
- the point to move the rectangle toNullPointerException
- if p is nullgetLocation()
public void setLocation(int x, int y)
x
- the new X coordinate for this rectangley
- the new Y coordinate for this rectanglepublic void move(int x, int y)
setLocation(int, int)
insteadx
- the new X coordinate for this rectangley
- the new Y coordinate for this rectanglepublic void translate(int dx, int dy)
dx
- the x distance to move bydy
- the y distance to move bysetLocation(int, int)
public Dimension getSize()
setSize(Dimension)
public void setSize(Dimension d)
d
- the new dimensions of the rectangleNullPointerException
- if d is nullgetSize()
public void setSize(int width, int height)
width
- the new width of the rectangleheight
- the new height of the rectanglepublic void resize(int width, int height)
setSize(int, int)
insteadwidth
- the new width of the rectangleheight
- the new height of the rectanglepublic boolean contains(Point p)
p
- the point to testNullPointerException
- if p is nullcontains(int, int)
public boolean contains(int x, int y)
x
- the X coordinate of the point to testy
- the Y coordinate of the point to testpublic boolean contains(Rectangle r)
r
- the rectangle to checkNullPointerException
- if r is nullcontains(int, int, int, int)
public boolean contains(int x, int y, int w, int h)
x
- the x coordinate of the rectangle to checky
- the y coordinate of the rectangle to checkw
- the width of the rectangle to checkh
- the height of the rectangle to checkpublic boolean inside(int x, int y)
contains(int, int)
insteadx
- the X coordinate of the point to testy
- the Y coordinate of the point to testpublic boolean intersects(Rectangle r)
r
- the rectangle to test againstNullPointerException
- if r is nullpublic Rectangle intersection(Rectangle r)
r
- the rectange to calculate the intersection withNullPointerException
- if r is nullpublic Rectangle union(Rectangle r)
r
- the rectangle to compute the union withNullPointerException
- if r is nullpublic void add(int x, int y)
contains
may
return false.x
- the X coordinate of the point to add to this rectangley
- the Y coordinate of the point to add to this rectanglepublic void add(Point p)
contains
may
return false.p
- the point to add to this rectangleNullPointerException
- if p is nullpublic void add(Rectangle r)
r
- the rectangle to add to this rectangleNullPointerException
- if r is nullunion(Rectangle)
public void grow(int h, int v)
h
- the horizontal expansion valuev
- the vertical expansion valuepublic boolean isEmpty()
isEmpty
in class RectangularShape
public int outcode(double x, double y)
outcode
in class Rectangle2D
x
- the x coordinate to checky
- the y coordinate to checkRectangle2D.OUT_LEFT
,
Rectangle2D.OUT_TOP
,
Rectangle2D.OUT_RIGHT
,
Rectangle2D.OUT_BOTTOM
public Rectangle2D createIntersection(Rectangle2D r)
createIntersection
in class Rectangle2D
r
- the rectange to calculate the intersection withNullPointerException
- if r is nullpublic Rectangle2D createUnion(Rectangle2D r)
createUnion
in class Rectangle2D
r
- the rectangle to compute the union withNullPointerException
- if r is nullpublic boolean equals(Object obj)
equals
in class Rectangle2D
obj
- the object to test against for equalityObject.hashCode()
public String toString()
getClass().getName() + "[x=" + x + ",y=" + y + ",width=" + width
+ ",height=" + height + ']'
.toString
in class Object
Object.getClass()
,
Object.hashCode()
,
Class.getName()
,
Integer.toHexString(int)