java.lang.Objectjava.awt.geom.RectangularShape
java.awt.geom.Rectangle2D
java.awt.Rectangle
public class Rectangle
A Rectangle specifies an area in a coordinate space that is enclosed by the Rectangle object's
upper-left
top-left
point
(x,y)
(
x
,
y
)
in the coordinate space, its width, and its height.
A Rectangle object's width and height are public fields. The constructors that create a Rectangle, and the methods that can modify one, do not prevent setting a negative value for width or height.
A Rectangle whose width or height is exactly zero has location along those axes with zero dimension, but is otherwise considered empty. The
isEmpty()
method will return true for such a Rectangle. Methods which test if an empty Rectangle contains or intersects a point or rectangle will always return false if either dimension is zero. Methods which combine such a Rectangle with a point or rectangle will include the location of the Rectangle on that axis in the result as if the
add(Point)
method were being called.
A Rectangle whose width or height is negative is considered empty. If the Rectangle is empty, then the isEmpty method returns true. No point can be contained by or inside an empty Rectangle. The values of width and height, however, are still valid. An empty Rectangle still has a location in the coordinate space, and methods that change its size or location remain valid. The behavior of methods that operate on more than one Rectangle is undefined if any of the participating Rectangle objects has a negative width or height. These methods include intersects, intersection, and union.
A Rectangle whose width or height is negative has neither location nor dimension along those axes with negative dimensions. Such a Rectangle is treated as non-existant along those axes. Such a Rectangle is also empty with respect to containment calculations and methods which test if it contains or intersects a point or rectangle will always return false. Methods which combine such a Rectangle with a point or rectangle will ignore the Rectangle entirely in generating the result. If two Rectangle objects are combined and each has a negative dimension, the result will have at least one negative dimension.
Methods which affect only the location of a Rectangle will operate on its location regardless of whether or not it has a negative or zero dimension along either axis.
Note that a Rectangle constructed with the default no-argument constructor will have dimensions of 0x0 and therefore be empty. That Rectangle will still have a location of (0,0) and will contribute that location to the union and add operations. Code attempting to accumulate the bounds of a set of points should therefore initially construct the Rectangle with a specifically negative width and height or it should use the first point in the set to construct the Rectangle. For example:
Rectangle bounds = new Rectangle(0, 0, -1, -1); for (int i = 0; i
Nested Class Summary |
---|
Nested classes/interfaces inherited from class java.awt.geom.Rectangle2D |
---|
Rectangle2D.Double, Rectangle2D.Float |
Field Summary | |
---|---|
int | height The height of the Rectangle. |
int | width The width of the Rectangle. |
int | x The ![]() ![]() ![]() |
int | y The ![]() ![]() ![]() |
Fields inherited from class java.awt.geom.Rectangle2D |
---|
OUT_BOTTOM, OUT_LEFT, OUT_RIGHT, OUT_TOP |
Constructor Summary | |
---|---|
Rectangle() Constructs a new Rectangle whose ![]() ![]() | |
Rectangle(Dimension d) Constructs a new Rectangle whose top left corner is (0, 0) and whose width and height are specified by the Dimension argument. | |
Rectangle(int width, int height) Constructs a new Rectangle whose ![]() ![]() | |
Rectangle(int x, int y, int width, int height) Constructs a new Rectangle whose ![]() ![]() ![]() ![]() | |
Rectangle( Point Constructs a new Rectangle whose ![]() ![]() | |
Rectangle( Point p, Dimension Constructs a new Rectangle whose ![]() ![]() | |
Rectangle(Rectangle r) Constructs a new Rectangle, initialized to match the values of the specified Rectangle. |
Method Summary | |
---|---|
void | add(int newx, int newy) Adds a point, specified by the integer arguments ![]() ![]() ![]() |
void | add( Point Adds the specified Point to ![]() |
void | add(Rectangle r) Adds a Rectangle to this Rectangle. |
boolean | contains(int x, int y) Checks whether or not this Rectangle contains the point at the specified location ![]() ![]() ![]() ![]() ![]() ![]() |
boolean | contains(int X, int Y, int W, int H) Checks whether this Rectangle entirely contains the Rectangle at the specified location ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
boolean | contains(Point p) Checks whether or not this Rectangle contains the specified Point. |
boolean | contains(Rectangle r) Checks whether or not this Rectangle entirely contains the specified Rectangle. |
Rectangle2D | createIntersection( Rectangle2D Returns a new ![]() ![]() |
Rectangle2D | createUnion( Rectangle2D Returns a new Rectangle2D object representing the union of this ![]() ![]() |
boolean | equals(Object obj) Checks whether two rectangles are equal. |
Rectangle | getBounds() Gets the bounding Rectangle of this Rectangle. |
Rectangle2D | getBounds2D() ![]() ![]() ![]() ![]() ![]() |
double | getHeight() Returns the height of the bounding Rectangle in double precision. |
Point | getLocation() Returns the location of this Rectangle. |
Dimension | getSize() Gets the size of this Rectangle, represented by the returned Dimension. |
double | getWidth() Returns the width of the bounding Rectangle in double precision. |
double | getX() Returns the X coordinate of the bounding Rectangle in double precision. |
double | getY() Returns the Y coordinate of the bounding Rectangle in double precision. |
void | grow(int h, int v) Resizes the Rectangle both horizontally and vertically. |
boolean | inside(int X, int Y) Deprecated. As of JDK version 1.1, replaced by contains(int, int). |
Rectangle | intersection(Rectangle r) Computes the intersection of this Rectangle with the specified Rectangle. |
boolean | intersects(Rectangle r) Determines whether or not this Rectangle and the specified Rectangle intersect. |
boolean | isEmpty() Determines whether ![]() ![]() |
void | move(int x, int y) Deprecated. As of JDK version 1.1, replaced by setLocation(int, int). |
int | outcode(double x, double y) Determines where the specified coordinates lie with respect to this ![]() ![]() |
void | reshape(int x, int y, int width, int height) Deprecated. As of JDK version 1.1, replaced by setBounds(int, int, int, int). |
void | resize(int width, int height) Deprecated. As of JDK version 1.1, replaced by setSize(int, int). |
void | setBounds(int x, int y, int width, int height) Sets the bounding Rectangle of this Rectangle to the specified x, y, width, and height. |
void | setBounds(Rectangle r) Sets the bounding Rectangle of this Rectangle to match the specified Rectangle. |
void | setLocation(int x, int y) Moves this Rectangle to the specified location. |
void | setLocation(Point p) Moves this Rectangle to the specified location. |
void | setRect(double x, double y, double width, double height) Sets the bounds of this Rectangle to the ![]() |
void | setSize(Dimension d) Sets the size of this Rectangle to match the specified Dimension. |
void | setSize(int width, int height) Sets the size of this Rectangle to the specified width and height. |
String | toString() Returns a String representing this Rectangle and its values. |
void | translate![]() ![]() Translates this Rectangle the indicated distance, to the right along the ![]() ![]() ![]() ![]() |
Rectangle | union(Rectangle r) Computes the union of this Rectangle with the specified Rectangle. |
Methods inherited from class java.awt.geom.Rectangle2D |
---|
add, add, add, contains, contains, getPathIterator, getPathIterator, hashCode, intersect, intersects, intersectsLine, intersectsLine, outcode, setFrame, setRect, union |
Methods inherited from class java.awt.geom.RectangularShape |
---|
clone, contains, contains, getCenterX, getCenterY, getFrame, getMaxX, getMaxY, getMinX, getMinY, intersects, setFrame, setFrame, setFrameFromCenter, setFrameFromCenter, setFrameFromDiagonal, setFrameFromDiagonal |
Methods inherited from class java.lang.Object |
---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
Methods inherited from interface java.awt.Shape |
---|
contains, contains, contains, contains, getPathIterator, getPathIterator, intersects, intersects |
Field Detail |
---|
public int x
public int y
public int width
public int height
Constructor Detail |
---|
public Rectangle()
public Rectangle(Rectangle r)
public Rectangle(int x, int y, int width, int height)
public Rectangle(int width, int height)
public Rectangle(Point p, Dimension d)
public Rectangle(Point p)
public Rectangle(Dimension d)
Method Detail |
---|
public double getX()
public double getY()
public double getWidth()
public double getHeight()
public Rectangle getBounds()
This method is included for completeness, to parallel the getBounds method of Component .
public Rectangle2D getBounds2D()
public void setBounds(Rectangle r)
This method is included for completeness, to parallel the setBounds method of Component.
public void setBounds(int x, int y, int width, int height)
This method is included for completeness, to parallel the setBounds method of Component.
public void setRect(double x, double y, double width, double height)
@Deprecated public void reshape(int x, int y, int width, int height)
public Point getLocation()
This method is included for completeness, to parallel the getLocation method of Component.
public void setLocation(Point p)
This method is included for completeness, to parallel the setLocation method of Component.
public void setLocation(int x, int y)
This method is included for completeness, to parallel the setLocation method of Component.
@Deprecated public void move(int x, int y)
public void translate(int dx, int dy)
(int x, int y)
public Dimension getSize()
This method is included for completeness, to parallel the getSize method of Component.
public void setSize(Dimension d)
This method is included for completeness, to parallel the setSize method of Component.
public void setSize(int width, int height)
This method is included for completeness, to parallel the setSize method of Component.
@Deprecated public void resize(int width, int height)
public boolean contains(Point p)
public boolean contains(int x, int y)
public boolean contains(Rectangle r)
public boolean contains(int X, int Y, int W, int H)
@Deprecated public boolean inside(int X, int Y)
public boolean intersects(Rectangle r)
public Rectangle intersection(Rectangle r)
public Rectangle union(Rectangle r)
If either Rectangle has any dimension less than zero the rules for
non-existant
rectangles apply. If only one has a dimension less than zero, then the result will be a copy of the other Rectangle. If both have dimension less than zero, then the result will have at least one dimension less than zero.
If the resulting Rectangle would have a dimension too large to be expressed as an int, the result will have a dimension of Integer.MAX_VALUE along that dimension.
public void add(int newx, int newy)
If this Rectangle has any dimension less than zero, the rules for
non-existant
rectangles apply. In that case, the new bounds of this Rectangle will have a location equal to the specified coordinates and width and height equal to zero.
After adding a point, a call to contains with the added point as an argument does not necessarily return true. The contains method does not return true for points on the right or bottom edges of a Rectangle. Therefore, if the added point falls on the right or bottom edge of the enlarged Rectangle, contains returns false for that point.
If the specified point must be contained within the new Rectangle, a 1x1 rectangle should be added instead:
r.add(newx, newy, 1, 1);
public void add(Point pt)
If this Rectangle has any dimension less than zero, the rules for
non-existant
rectangles apply. In that case, the new bounds of this Rectangle will have a location equal to the coordinates of the specified Point and width and height equal to zero.
After adding a Point, a call to contains with the added Point as an argument does not necessarily return true. The contains method does not return true for points on the right or bottom edges of a Rectangle. Therefore if the added Point falls on the right or bottom edge of the enlarged Rectangle, contains returns false for that Point.
If the specified point must be contained within the new Rectangle, a 1x1 rectangle should be added instead:
r.add(pt.x, pt.y, 1, 1);
public void add(Rectangle r)
If either Rectangle has any dimension less than 0, the result will have the dimensions of the other Rectangle. If both Rectangles have at least one dimension less than 0, the result will have at least one dimension less than 0.
If either Rectangle has one or both dimensions equal to 0, the result along those axes with 0 dimensions will be equivalent to the results obtained by adding the corresponding origin coordinate to the result rectangle along that axis, similar to the operation of the
add(Point)
method, but contribute no further dimension beyond that.
If the resulting Rectangle would have a dimension too large to be expressed as an int, the result will have a dimension of Integer.MAX_VALUE along that dimension.
public void grow(int h, int v)
This method modifies the Rectangle so that it is h units larger on both the left and right side, and v units larger at both the top and bottom.
The new Rectangle has
(x - h, y - v)
(x - h, y - v)
as its
upper-left
top-left
corner,
a
width of
(width + 2h),
width + 2h,
and a height of
(height + 2v).
height + 2v.
If negative values are supplied for h and v, the size of the Rectangle decreases accordingly. The grow method
will check for integer overflow and underflow, but
does not check whether the resulting values of width and height
grow from negative to non-negative or shrink from non-negative to negative.
are non-negative.
public boolean isEmpty()
public int outcode(double x, double y)
public Rectangle2D createIntersection(Rectangle2D r)
public Rectangle2D createUnion(Rectangle2D r)
public boolean equals(Object obj)
The result is true if and only if the argument is not null and is a Rectangle object that has the same
upper-left
top-left
corner, width, and height as this Rectangle.
public String toString()