Rectangle specifies an area in a coordinate space that is
enclosed by the Rectangle object's upper-left point
(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.
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 < points.length; i++) {
bounds.add(points[i]);
}
or if we know that the points array contains at least one point:
Rectangle bounds = new Rectangle(points[0]);
for (int i = 1; i < points.length; i++) {
bounds.add(points[i]);
}
This class uses 32-bit integers to store its location and dimensions.
Frequently operations may produce a result that exceeds the range of
a 32-bit integer.
The methods will calculate their results in a way that avoids any
32-bit overflow for intermediate results and then choose the best
representation to store the final results back into the 32-bit fields
which hold the location and dimensions.
The location of the result will be stored into the x and
y fields by clipping the true result to the nearest 32-bit value.
The values stored into the width and height dimension
fields will be chosen as the 32-bit values that encompass the largest
part of the true result as possible.
Generally this means that the dimension will be clipped independently
to the range of 32-bit integers except that if the location had to be
moved to store it into its pair of 32-bit fields then the dimensions
will be adjusted relative to the "best representation" of the location.
If the true result had a negative dimension and was therefore
non-existent along one or both axes, the stored dimensions will be
negative numbers in those axes.
If the true result had a location that could be represented within
the range of 32-bit integers, but zero dimension along one or both
axes, then the stored dimensions will be zero in those axes.
| Modifier and Type | Field and Description |
|---|---|
| public int | height
The height of the |
| private static final long | serialVersionUID
Use serialVersionUID from JDK 1.1 for interoperability. |
| public int | width
The width of the |
| public int | x
The X coordinate of the upper-left corner of the |
| public int | y
The Y coordinate of the upper-left corner of the |
| Access | Constructor and Description |
|---|---|
| public | Rectangle()
Constructs a new |
| public | |
| public | Rectangle(int
the specified X coordinate x, int the specified Y coordinate y, int the width of the width, int Rectanglethe height of the height)RectangleConstructs a new |
| public | Rectangle(int
the width of the width, int Rectanglethe height of the height)RectangleConstructs a new |
| public | Rectangle(Point
a p, Dimension Point that is the upper-left corner of
the Rectanglea d)Dimension, representing the
width and height of the RectangleConstructs a new |
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | add(int
the X coordinate of the new point newx, int the Y coordinate of the new point newy)Adds a point, specified by the integer arguments |
| public void | |
| public void | |
| private static int | |
| public boolean | |
| public boolean | Returns: true if the point
(x,y) is inside this
Rectangle;
false otherwise.the specified X coordinate x, int the specified Y coordinate y)Checks whether or not this |
| public boolean | |
| public boolean | Returns: true if the Rectangle specified by
(X, Y, W, H)
is entirely enclosed inside this Rectangle;
false otherwise.the specified X coordinate X, int the specified Y coordinate Y, int the width of the W, int Rectanglethe height of the H)RectangleChecks whether this |
| public Rectangle2D | createIntersection(Rectangle2D r)
Implements abstract java. Returns a new |
| public Rectangle2D | createUnion(Rectangle2D r)
Implements abstract java. Returns a new |
| public boolean | Returns: true if the objects are equal;
false otherwise.the obj)Object to compare with
this RectangleOverrides java. Checks whether two rectangles are equal. |
| public Rectangle | Returns: a newRectangle, equal to the
bounding Rectangle for this Rectangle.Overrides java. Implements java. Gets the bounding |
| public Rectangle2D | getBounds2D()
Overrides java. Implements java. Returns a high precision and more accurate bounding box of
the |
| public double | Returns: the height of the boundingRectangle.Implements abstract java. Returns the height of the bounding |
| public Point | Returns: thePoint that is the upper-left corner of
this Rectangle.Returns the location of this |
| public Dimension | Returns: aDimension, representing the size of
this Rectangle.Gets the size of this |
| public double | Returns: the width of the boundingRectangle.Implements abstract java. Returns the width of the bounding |
| public double | Returns: the X coordinate of the boundingRectangle.Implements abstract java. Returns the X coordinate of the bounding |
| public double | Returns: the Y coordinate of the boundingRectangle.Implements abstract java. Returns the Y coordinate of the bounding |
| public void | grow(int
the horizontal expansion h, int the vertical expansion v)Resizes the |
| public boolean | Returns: true if the point
(X,Y) is inside this
Rectangle;
false otherwise.the specified X coordinate X, int the specified Y coordinate Y)
Deprecated
As of JDK version 1.1,
replaced by
contains(int, int).
Checks whether or not this |
| public Rectangle | Returns: the largestRectangle contained in both the
specified Rectangle and in
this Rectangle; or if the rectangles
do not intersect, an empty rectangle.the specified r)RectangleComputes the intersection of this |
| public boolean | Returns: true if the specified Rectangle
and this Rectangle intersect;
false otherwise.the specified r)RectangleDetermines whether or not this |
| public boolean | isEmpty()
Implements abstract java. Determines whether the |
| public void | move(int
the X coordinate of the new location x, int the Y coordinate of the new location y)
Deprecated
As of JDK version 1.1,
replaced by
setLocation(int, int).
Moves this |
| public int | outcode(double x, double y)
Implements abstract java. Determines where the specified coordinates lie with respect
to this |
| public void | reshape(int
the new X coordinate for the upper-left
corner of this x, int Rectanglethe new Y coordinate for the upper-left
corner of this y, int Rectanglethe new width for this width, int Rectanglethe new height for this height)Rectangle
Deprecated
As of JDK version 1.1,
replaced by
setBounds(int, int, int, int).
Sets the bounding |
| public void | resize(int
the new width for this width, int Rectanglethe new height for this height)Rectangle
Deprecated
As of JDK version 1.1,
replaced by
setSize(int, int).
Sets the size of this |
| public void | |
| public void | setBounds(int
the new X coordinate for the upper-left
corner of this x, int Rectanglethe new Y coordinate for the upper-left
corner of this y, int Rectanglethe new width for this width, int Rectanglethe new height for this height)RectangleSets the bounding |
| public void | setLocation(Point
the p)Point specifying the new location
for this RectangleMoves this |
| public void | setLocation(int
the X coordinate of the new location x, int the Y coordinate of the new location y)Moves this |
| public void | setRect(double
the X coordinate of the upper-left corner of
the specified rectangle x, double the Y coordinate of the upper-left corner of
the specified rectangle y, double the width of the specified rectangle width, double the new height of the specified rectangle height)Implements abstract java. Sets the bounds of this |
| public void | |
| public void | setSize(int
the new width for this width, int Rectanglethe new height for this height)RectangleSets the size of this |
| public String | Returns: aString representing this
Rectangle object's coordinate and size values.Overrides java. Returns a |
| public void | translate(int
the distance to move this dx, int Rectangle
along the X axisthe distance to move this dy)Rectangle
along the Y axisTranslates this |
| public Rectangle |
| height | back to summary |
|---|---|
| public int height The height of the
| |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Use serialVersionUID from JDK 1.1 for interoperability. | |
| width | back to summary |
|---|---|
| public int width The width of the
| |
| x | back to summary |
|---|---|
| public int x The X coordinate of the upper-left corner of the
| |
| y | back to summary |
|---|---|
| public int y The Y coordinate of the upper-left corner of the
| |
| Rectangle | back to summary |
|---|---|
| public Rectangle() Constructs a new | |
| Rectangle | back to summary |
|---|---|
| public Rectangle(Rectangle r) Constructs a new
| |
| Rectangle | back to summary |
|---|---|
| public Rectangle(int x, int y, int width, int height) Constructs a new
| |
| Rectangle | back to summary |
|---|---|
| public Rectangle(int width, int height) Constructs a new
| |
| Rectangle | back to summary |
|---|---|
| public Rectangle(Point p, Dimension d) Constructs a new | |
| Rectangle | back to summary |
|---|---|
| public Rectangle(Point p) Constructs a new
| |
| Rectangle | back to summary |
|---|---|
| public Rectangle(Dimension d) Constructs a new
| |
| add | back to summary |
|---|---|
| public void add(int newx, int newy) Adds a point, specified by the integer arguments
If this
After adding a point, a call to
r.add(new Rectangle(newx, newy, 1, 1));
| |
| add | back to summary |
|---|---|
| public void add(Point pt) Adds the specified
If this
After adding a
r.add(new Rectangle(pt, new Dimension(1, 1)));
| |
| add | back to summary |
|---|---|
| public void add(Rectangle r) Adds a
If either
If either
If the resulting
| |
| clip | back to summary |
|---|---|
| private static int clip(double v, boolean doceil) | |
| contains | back to summary |
|---|---|
| public boolean contains(Point p) Checks whether or not this
| |
| contains | back to summary |
|---|---|
| public boolean contains(int x, int y) Checks whether or not this
| |
| contains | back to summary |
|---|---|
| public boolean contains(Rectangle r) Checks whether or not this
| |
| contains | back to summary |
|---|---|
| public boolean contains(int X, int Y, int W, int H) Checks whether this
| |
| createIntersection | back to summary |
|---|---|
| public Rectangle2D createIntersection(Rectangle2D r) Implements abstract java. Doc from java. Returns a new
| |
| createUnion | back to summary |
|---|---|
| public Rectangle2D createUnion(Rectangle2D r) Implements abstract java. Doc from java. Returns a new
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object obj) Overrides java. Checks whether two rectangles are equal.
The result is
| |
| getBounds | back to summary |
|---|---|
| public Rectangle getBounds() Overrides java. Implements java. Gets the bounding
This method is included for completeness, to parallel the
| |
| getBounds2D | back to summary |
|---|---|
| public Rectangle2D getBounds2D() Overrides java. Implements java. Doc from java. Returns a high precision and more accurate bounding box of
the
Note that the
definition of insideness can lead to situations where points
on the defining outline of the
If a
If a
| |
| getHeight | back to summary |
|---|---|
| public double getHeight() Implements abstract java. Returns the height of the bounding
| |
| getLocation | back to summary |
|---|---|
| public Point getLocation() Returns the location of this
This method is included for completeness, to parallel the
| |
| getSize | back to summary |
|---|---|
| public Dimension getSize() Gets the size of this
This method is included for completeness, to parallel the
| |
| getWidth | back to summary |
|---|---|
| public double getWidth() Implements abstract java. Returns the width of the bounding
| |
| getX | back to summary |
|---|---|
| public double getX() Implements abstract java. Returns the X coordinate of the bounding
| |
| getY | back to summary |
|---|---|
| public double getY() Implements abstract java. Returns the Y coordinate of the bounding
| |
| grow | back to summary |
|---|---|
| public void grow(int h, int v) Resizes the
This method modifies the
The new
If negative values are supplied for
| |
| inside | back to summary |
|---|---|
| public boolean inside(int X, int Y)
Deprecated As of JDK version 1.1,
replaced by Checks whether or not this
| |
| intersection | back to summary |
|---|---|
| public Rectangle intersection(Rectangle r) Computes the intersection of this | |
| intersects | back to summary |
|---|---|
| public boolean intersects(Rectangle r) Determines whether or not this
| |
| isEmpty | back to summary |
|---|---|
| public boolean isEmpty() Implements abstract java. Doc from java. Determines whether the
| |
| move | back to summary |
|---|---|
| public void move(int x, int y)
Deprecated As of JDK version 1.1,
replaced by Moves this
| |
| outcode | back to summary |
|---|---|
| public int outcode(double x, double y) Implements abstract java. Doc from java. Determines where the specified coordinates lie with respect
to this
| |
| reshape | back to summary |
|---|---|
| public void reshape(int x, int y, int width, int height)
Deprecated As of JDK version 1.1,
replaced by Sets the bounding
| |
| resize | back to summary |
|---|---|
| public void resize(int width, int height)
Deprecated As of JDK version 1.1,
replaced by Sets the size of this
| |
| setBounds | back to summary |
|---|---|
| public void setBounds(Rectangle r) Sets the bounding
This method is included for completeness, to parallel the
| |
| setBounds | back to summary |
|---|---|
| public void setBounds(int x, int y, int width, int height) Sets the bounding
This method is included for completeness, to parallel the
| |
| setLocation | back to summary |
|---|---|
| public void setLocation(Point p) Moves this
This method is included for completeness, to parallel the
| |
| setLocation | back to summary |
|---|---|
| public void setLocation(int x, int y) Moves this
This method is included for completeness, to parallel the
| |
| setRect | back to summary |
|---|---|
| public void setRect(double x, double y, double width, double height) Implements abstract java. Sets the bounds of this
| |
| setSize | back to summary |
|---|---|
| public void setSize(Dimension d) Sets the size of this
This method is included for completeness, to parallel the
| |
| setSize | back to summary |
|---|---|
| public void setSize(int width, int height) Sets the size of this
This method is included for completeness, to parallel the
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Returns a
| |
| translate | back to summary |
|---|---|
| public void translate(int dx, int dy) Translates this
| |
| union | back to summary |
|---|---|
| public Rectangle union(Rectangle r) Computes the union of this
If either
If the resulting | |