Class Rectangle2D
- Direct Known Subclasses:
-
Rectangle,Rectangle2D.Double,Rectangle2D.Float
public abstract class Rectangle2D extends RectangularShape
Rectangle2D class describes a rectangle defined by a location (x,y) and dimension (w x h). This class is only the abstract superclass for all objects that store a 2D rectangle. The actual storage representation of the coordinates is left to the subclass.
- Since:
- 1.2
Nested Class Summary
| Modifier and Type | Class | Description |
|---|---|---|
static class |
Rectangle2D.Double |
The Double class defines a rectangle specified in double coordinates. |
static class |
Rectangle2D.Float |
The Float class defines a rectangle specified in float coordinates. |
Field Summary
| Modifier and Type | Field | Description |
|---|---|---|
static final int |
OUT_BOTTOM |
The bitmask that indicates that a point lies below this Rectangle2D. |
static final int |
OUT_LEFT |
The bitmask that indicates that a point lies to the left of this Rectangle2D. |
static final int |
OUT_RIGHT |
The bitmask that indicates that a point lies to the right of this Rectangle2D. |
static final int |
OUT_TOP |
The bitmask that indicates that a point lies above this Rectangle2D. |
Constructor Summary
| Modifier | Constructor | Description |
|---|---|---|
protected |
This is an abstract class that cannot be instantiated directly. |
Method Summary
| Modifier and Type | Method | Description |
|---|---|---|
void |
add |
Adds a point, specified by the double precision arguments newx and newy, to this Rectangle2D. |
void |
add |
Adds the Point2D object pt to this Rectangle2D. |
void |
add |
Adds a Rectangle2D object to this Rectangle2D. |
boolean |
contains |
Tests if the specified coordinates are inside the boundary of the Shape, as described by the definition of insideness. |
boolean |
contains |
Tests if the interior of the Shape entirely contains the specified rectangular area. |
abstract Rectangle2D |
createIntersection |
Returns a new Rectangle2D object representing the intersection of this Rectangle2D with the specified Rectangle2D. |
abstract Rectangle2D |
createUnion |
Returns a new Rectangle2D object representing the union of this Rectangle2D with the specified Rectangle2D. |
boolean |
equals |
Determines whether or not the specified Object is equal to this Rectangle2D. |
Rectangle2D |
getBounds2D() |
Returns a high precision and more accurate bounding box of the Shape than the getBounds method. |
PathIterator |
getPathIterator |
Returns an iteration object that defines the boundary of this Rectangle2D. |
PathIterator |
getPathIterator |
Returns an iteration object that defines the boundary of the flattened Rectangle2D. |
int |
hashCode() |
Returns the hashcode for this Rectangle2D. |
static void |
intersect |
Intersects the pair of specified source Rectangle2D objects and puts the result into the specified destination Rectangle2D object. |
boolean |
intersects |
Tests if the interior of the Shape intersects the interior of a specified rectangular area. |
boolean |
intersectsLine |
Tests if the specified line segment intersects the interior of this Rectangle2D. |
boolean |
intersectsLine |
Tests if the specified line segment intersects the interior of this Rectangle2D. |
abstract int |
outcode |
Determines where the specified coordinates lie with respect to this Rectangle2D. |
int |
outcode |
Determines where the specified Point2D lies with respect to this Rectangle2D. |
void |
setFrame |
Sets the location and size of the outer bounds of this Rectangle2D to the specified rectangular values. |
abstract void |
setRect |
Sets the location and size of this Rectangle2D to the specified double values. |
void |
setRect |
Sets this Rectangle2D to be the same as the specified Rectangle2D. |
static void |
union |
Unions the pair of source Rectangle2D objects and puts the result into the specified destination Rectangle2D object. |
Methods declared in class java.awt.geom.RectangularShape
clone, contains, contains, getBounds, getCenterX, getCenterY, getFrame, getHeight, getMaxX, getMaxY, getMinX, getMinY, getWidth, getX, getY, intersects, isEmpty, setFrame, setFrame, setFrameFromCenter, setFrameFromCenter, setFrameFromDiagonal, setFrameFromDiagonal
Field Details
OUT_LEFT
public static final int OUT_LEFT
Rectangle2D.- Since:
- 1.2
- See Also:
OUT_TOP
public static final int OUT_TOP
Rectangle2D.- Since:
- 1.2
- See Also:
OUT_RIGHT
public static final int OUT_RIGHT
Rectangle2D.- Since:
- 1.2
- See Also:
OUT_BOTTOM
public static final int OUT_BOTTOM
Rectangle2D.- Since:
- 1.2
- See Also:
Constructor Details
Rectangle2D
protected Rectangle2D()
- Since:
- 1.2
- See Also:
Method Details
setRect
public abstract void setRect(double x, double y, double w, double h)
Rectangle2D to the specified double values.- Parameters:
-
x- the X coordinate of the upper-left corner of thisRectangle2D -
y- the Y coordinate of the upper-left corner of thisRectangle2D -
w- the width of thisRectangle2D -
h- the height of thisRectangle2D - Since:
- 1.2
setRect
public void setRect(Rectangle2D r)
Rectangle2D to be the same as the specified Rectangle2D.- Parameters:
-
r- the specifiedRectangle2D - Since:
- 1.2
intersectsLine
public boolean intersectsLine(double x1, double y1, double x2, double y2)
Rectangle2D.- Parameters:
-
x1- the X coordinate of the start point of the specified line segment -
y1- the Y coordinate of the start point of the specified line segment -
x2- the X coordinate of the end point of the specified line segment -
y2- the Y coordinate of the end point of the specified line segment - Returns:
-
trueif the specified line segment intersects the interior of thisRectangle2D;falseotherwise. - Since:
- 1.2
intersectsLine
public boolean intersectsLine(Line2D l)
Rectangle2D.- Parameters:
-
l- the specifiedLine2Dto test for intersection with the interior of thisRectangle2D - Returns:
-
trueif the specifiedLine2Dintersects the interior of thisRectangle2D;falseotherwise. - Since:
- 1.2
outcode
public abstract int outcode(double x, double y)
Rectangle2D. This method computes a binary OR of the appropriate mask values indicating, for each side of this Rectangle2D, whether or not the specified coordinates are on the same side of the edge as the rest of this Rectangle2D.- Parameters:
-
x- the specified X coordinate -
y- the specified Y coordinate - Returns:
- the logical OR of all appropriate out codes.
- Since:
- 1.2
- See Also:
outcode
public int outcode(Point2D p)
Point2D lies with respect to this Rectangle2D. This method computes a binary OR of the appropriate mask values indicating, for each side of this Rectangle2D, whether or not the specified Point2D is on the same side of the edge as the rest of this Rectangle2D.- Parameters:
-
p- the specifiedPoint2D - Returns:
- the logical OR of all appropriate out codes.
- Since:
- 1.2
- See Also:
setFrame
public void setFrame(double x, double y, double w, double h)
Rectangle2D to the specified rectangular values.- Specified by:
-
setFramein classRectangularShape - Parameters:
-
x- the X coordinate of the upper-left corner of thisRectangle2D -
y- the Y coordinate of the upper-left corner of thisRectangle2D -
w- the width of thisRectangle2D -
h- the height of thisRectangle2D - Since:
- 1.2
- See Also:
getBounds2D
public Rectangle2D getBounds2D()
Shape than the getBounds method. Note that there is no guarantee that the returned Rectangle2D is the smallest bounding box that encloses the Shape, only that the Shape lies entirely within the indicated Rectangle2D. The bounding box returned by this method is usually tighter than that returned by the getBounds method and never fails due to overflow problems since the return value can be an instance of the Rectangle2D that uses double precision values to store the dimensions. Note that the definition of insideness can lead to situations where points on the defining outline of the shape may not be considered contained in the returned bounds object, but only in cases where those points are also not considered contained in the original shape.
If a point is inside the shape according to the contains(point) method, then it must be inside the returned Rectangle2D bounds object according to the contains(point) method of the bounds. Specifically:
shape.contains(p) requires bounds.contains(p)
If a point is not inside the shape, then it might still be contained in the bounds object:
bounds.contains(p) does not imply shape.contains(p)
- Returns:
- an instance of
Rectangle2Dthat is a high-precision bounding box of theShape. - Since:
- 1.2
- See Also:
contains
public boolean contains(double x, double y)
Shape, as described by the definition of insideness.- Parameters:
-
x- the specified X coordinate to be tested -
y- the specified Y coordinate to be tested - Returns:
-
trueif the specified coordinates are inside theShapeboundary;falseotherwise. - Since:
- 1.2
intersects
public boolean intersects(double x, double y, double w, double h)
Shape intersects the interior of a specified rectangular area. The rectangular area is considered to intersect the Shape if any point is contained in both the interior of the Shape and the specified rectangular area. The Shape.intersects() method allows a Shape implementation to conservatively return true when:
- there is a high probability that the rectangular area and the
Shapeintersect, but - the calculations to accurately determine this intersection are prohibitively expensive.
Shapes this method might return true even though the rectangular area does not intersect the Shape. The Area class performs more accurate computations of geometric intersection than most Shape objects and therefore can be used if a more precise answer is required.- Parameters:
-
x- the X coordinate of the upper-left corner of the specified rectangular area -
y- the Y coordinate of the upper-left corner of the specified rectangular area -
w- the width of the specified rectangular area -
h- the height of the specified rectangular area - Returns:
-
trueif the interior of theShapeand the interior of the rectangular area intersect, or are both highly likely to intersect and intersection calculations would be too expensive to perform;falseotherwise. - Since:
- 1.2
- See Also:
contains
public boolean contains(double x, double y, double w, double h)
Shape entirely contains the specified rectangular area. All coordinates that lie inside the rectangular area must lie within the Shape for the entire rectangular area to be considered contained within the Shape. The Shape.contains() method allows a Shape implementation to conservatively return false when:
- the
intersectmethod returnstrueand - the calculations to determine whether or not the
Shapeentirely contains the rectangular area are prohibitively expensive.
Shapes this method might return false even though the Shape contains the rectangular area. The Area class performs more accurate geometric computations than most Shape objects and therefore can be used if a more precise answer is required.- Parameters:
-
x- the X coordinate of the upper-left corner of the specified rectangular area -
y- the Y coordinate of the upper-left corner of the specified rectangular area -
w- the width of the specified rectangular area -
h- the height of the specified rectangular area - Returns:
-
trueif the interior of theShapeentirely contains the specified rectangular area;falseotherwise or, if theShapecontains the rectangular area and theintersectsmethod returnstrueand the containment calculations would be too expensive to perform. - Since:
- 1.2
- See Also:
createIntersection
public abstract Rectangle2D createIntersection(Rectangle2D r)
Rectangle2D object representing the intersection of this Rectangle2D with the specified Rectangle2D.- Parameters:
-
r- theRectangle2Dto be intersected with thisRectangle2D - Returns:
- the largest
Rectangle2Dcontained in both the specifiedRectangle2Dand in thisRectangle2D. - Since:
- 1.2
intersect
public static void intersect(Rectangle2D src1, Rectangle2D src2, Rectangle2D dest)
Rectangle2D objects and puts the result into the specified destination Rectangle2D object. One of the source rectangles can also be the destination to avoid creating a third Rectangle2D object, but in this case the original points of this source rectangle will be overwritten by this method.- Parameters:
-
src1- the first of a pair ofRectangle2Dobjects to be intersected with each other -
src2- the second of a pair ofRectangle2Dobjects to be intersected with each other -
dest- theRectangle2Dthat holds the results of the intersection ofsrc1andsrc2 - Since:
- 1.2
createUnion
public abstract Rectangle2D createUnion(Rectangle2D r)
Rectangle2D object representing the union of this Rectangle2D with the specified Rectangle2D.- Parameters:
-
r- theRectangle2Dto be combined with thisRectangle2D - Returns:
- the smallest
Rectangle2Dcontaining both the specifiedRectangle2Dand thisRectangle2D. - Since:
- 1.2
union
public static void union(Rectangle2D src1, Rectangle2D src2, Rectangle2D dest)
Rectangle2D objects and puts the result into the specified destination Rectangle2D object. One of the source rectangles can also be the destination to avoid creating a third Rectangle2D object, but in this case the original points of this source rectangle will be overwritten by this method.- Parameters:
-
src1- the first of a pair ofRectangle2Dobjects to be combined with each other -
src2- the second of a pair ofRectangle2Dobjects to be combined with each other -
dest- theRectangle2Dthat holds the results of the union ofsrc1andsrc2 - Since:
- 1.2
add
public void add(double newx, double newy)
newx and newy, to this Rectangle2D. The resulting Rectangle2D is the smallest Rectangle2D that contains both the original Rectangle2D and the specified point. 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.
- Parameters:
-
newx- the X coordinate of the new point -
newy- the Y coordinate of the new point - Since:
- 1.2
add
public void add(Point2D pt)
Point2D object pt to this Rectangle2D. The resulting Rectangle2D is the smallest Rectangle2D that contains both the original Rectangle2D and the specified Point2D. 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.
- Parameters:
-
pt- the newPoint2Dto add to thisRectangle2D. - Since:
- 1.2
add
public void add(Rectangle2D r)
Rectangle2D object to this Rectangle2D. The resulting Rectangle2D is the union of the two Rectangle2D objects.- Parameters:
-
r- theRectangle2Dto add to thisRectangle2D. - Since:
- 1.2
getPathIterator
public PathIterator getPathIterator(AffineTransform at)
Rectangle2D. The iterator for this class is multi-threaded safe, which means that this Rectangle2D class guarantees that modifications to the geometry of this Rectangle2D object do not affect any iterations of that geometry that are already in process.- Parameters:
-
at- an optionalAffineTransformto be applied to the coordinates as they are returned in the iteration, ornullif untransformed coordinates are desired - Returns:
- the
PathIteratorobject that returns the geometry of the outline of thisRectangle2D, one segment at a time. - Since:
- 1.2
getPathIterator
public PathIterator getPathIterator(AffineTransform at, double flatness)
Rectangle2D. Since rectangles are already flat, the flatness parameter is ignored. The iterator for this class is multi-threaded safe, which means that this Rectangle2D class guarantees that modifications to the geometry of this Rectangle2D object do not affect any iterations of that geometry that are already in process.- Specified by:
-
getPathIteratorin interfaceShape - Overrides:
-
getPathIteratorin classRectangularShape - Parameters:
-
at- an optionalAffineTransformto be applied to the coordinates as they are returned in the iteration, ornullif untransformed coordinates are desired -
flatness- the maximum distance that the line segments used to approximate the curved segments are allowed to deviate from any point on the original curve. Since rectangles are already flat, theflatnessparameter is ignored. - Returns:
- the
PathIteratorobject that returns the geometry of the outline of thisRectangle2D, one segment at a time. - Since:
- 1.2
hashCode
public int hashCode()
Rectangle2D.equals
public boolean equals(Object obj)
Object is equal to this Rectangle2D. The specified Object is equal to this Rectangle2D if it is an instance of Rectangle2D and if its location and size are the same as this Rectangle2D.
© 1993, 2021, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/java/awt/geom/Rectangle2D.html