java.awt.geom
Class Rectangle2D

java.lang.Object
  extended by java.awt.geom.RectangularShape
      extended by java.awt.geom.Rectangle2D
All Implemented Interfaces:
Shape, Cloneable
Direct Known Subclasses:
Rectangle, Rectangle2D.Double, Rectangle2D.Float

public abstract class Rectangle2D
extends RectangularShape

This class describes a rectangle by a point (x,y) and dimension (w x h). The actual storage is left up to subclasses.

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.

Since:
1.2

Nested Class Summary
static class Rectangle2D.Double
          This class defines a rectangle in double precision.
static class Rectangle2D.Float
          This class defines a rectangle in float precision.
 
Field Summary
static int OUT_BOTTOM
          The point lies below of the rectangle (p.y > r.maxY).
static int OUT_LEFT
          The point lies left of the rectangle (p.x < r.x).
static int OUT_RIGHT
          The point lies right of the rectangle (p.x > r.maxX).
static int OUT_TOP
          The point lies above the rectangle (p.y < r.y).
 
Constructor Summary
protected Rectangle2D()
          Default constructor.
 
Method Summary
 void add(double newx, double newy)
          Modifies this rectangle so that it represents the smallest rectangle that contains both the existing rectangle and the specified point.
 void add(Point2D p)
          Modifies this rectangle so that it represents the smallest rectangle that contains both the existing rectangle and the specified point.
 void add(Rectangle2D r)
          Modifies this rectangle so that it represents the smallest rectangle that contains both the existing rectangle and the specified rectangle.
 boolean contains(double x, double y)
          Test if the given point is contained in the rectangle.
 boolean contains(double x, double y, double w, double h)
          Tests if this rectangle contains the given one.
abstract  Rectangle2D createIntersection(Rectangle2D r)
          Return a new rectangle which is the intersection of this and the given one.
abstract  Rectangle2D createUnion(Rectangle2D r)
          Return a new rectangle which is the union of this and the given one.
 boolean equals(Object obj)
          Tests this rectangle for equality against the specified object.
 Rectangle2D getBounds2D()
          Returns the bounds of this rectangle.
 PathIterator getPathIterator(AffineTransform at)
          Return an iterator along the shape boundary.
 PathIterator getPathIterator(AffineTransform at, double flatness)
          Return an iterator along the shape boundary.
 int hashCode()
          Return the hashcode for this rectangle.
static void intersect(Rectangle2D src1, Rectangle2D src2, Rectangle2D dest)
          Intersects a pair of rectangles, and places the result in the destination; this can be used to avoid object creation.
 boolean intersects(double x, double y, double w, double h)
          Tests if the given rectangle intersects this one.
 boolean intersectsLine(double x1, double y1, double x2, double y2)
          Tests if the specified line intersects the interior of this rectangle.
 boolean intersectsLine(Line2D l)
          Tests if the specified line intersects the interior of this rectangle.
abstract  int outcode(double x, double y)
          Determine where the point lies with respect to this rectangle.
 int outcode(Point2D p)
          Determine where the point lies with respect to this rectangle.
 void setFrame(double x, double y, double w, double h)
          Set the bounding box of this rectangle.
abstract  void setRect(double x, double y, double w, double h)
          Set the bounding box of this rectangle.
 void setRect(Rectangle2D r)
          Set the bounding box of this rectangle from the given one.
static void union(Rectangle2D src1, Rectangle2D src2, Rectangle2D dest)
          Joins a pair of rectangles, and places the result in the destination; this can be used to avoid object creation.
 
Methods inherited from 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
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OUT_LEFT

public static final int OUT_LEFT
The point lies left of the rectangle (p.x < r.x).

See Also:
outcode(double, double), Constant Field Values

OUT_TOP

public static final int OUT_TOP
The point lies above the rectangle (p.y < r.y).

See Also:
outcode(double, double), Constant Field Values

OUT_RIGHT

public static final int OUT_RIGHT
The point lies right of the rectangle (p.x > r.maxX).

See Also:
outcode(double, double), Constant Field Values

OUT_BOTTOM

public static final int OUT_BOTTOM
The point lies below of the rectangle (p.y > r.maxY).

See Also:
outcode(double, double), Constant Field Values
Constructor Detail

Rectangle2D

protected Rectangle2D()
Default constructor.

Method Detail

setRect

public abstract void setRect(double x,
                             double y,
                             double w,
                             double h)
Set the bounding box of this rectangle.

Parameters:
x - the new X coordinate
y - the new Y coordinate
w - the new width
h - the new height

setRect

public void setRect(Rectangle2D r)
Set the bounding box of this rectangle from the given one.

Parameters:
r - rectangle to copy
Throws:
NullPointerException - if r is null

intersectsLine

public boolean intersectsLine(double x1,
                              double y1,
                              double x2,
                              double y2)
Tests if the specified line intersects the interior of this rectangle.

Parameters:
x1 - the first x coordinate of line segment
y1 - the first y coordinate of line segment
x2 - the second x coordinate of line segment
y2 - the second y coordinate of line segment
Returns:
true if the line intersects the rectangle

intersectsLine

public boolean intersectsLine(Line2D l)
Tests if the specified line intersects the interior of this rectangle.

Parameters:
l - the line segment
Returns:
true if the line intersects the rectangle
Throws:
NullPointerException - if l is null

outcode

public abstract 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
See Also:
OUT_LEFT, OUT_TOP, OUT_RIGHT, OUT_BOTTOM

outcode

public int outcode(Point2D p)
Determine where the point lies with respect to this rectangle. The result will be the binary OR of the appropriate bit masks.

Parameters:
p - the point to check
Returns:
the binary OR of the result
Throws:
NullPointerException - if p is null
See Also:
OUT_LEFT, OUT_TOP, OUT_RIGHT, OUT_BOTTOM

setFrame

public void setFrame(double x,
                     double y,
                     double w,
                     double h)
Set the bounding box of this rectangle.

Specified by:
setFrame in class RectangularShape
Parameters:
x - the new X coordinate
y - the new Y coordinate
w - the new width
h - the new height
See Also:
RectangularShape.getFrame()

getBounds2D

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

Returns:
a copy of this rectangle
See Also:
Shape.getBounds()

contains

public boolean contains(double x,
                        double y)
Test if the given point is contained in the rectangle.

Parameters:
x - the x coordinate of the point
y - the y coordinate of the point
Returns:
true if (x,y) is in the rectangle

intersects

public boolean intersects(double x,
                          double y,
                          double w,
                          double h)
Tests if the given rectangle intersects this one. In other words, test if the two rectangles share at least one internal point.

Parameters:
x - the x coordinate of the other rectangle
y - the y coordinate of the other rectangle
w - the width of the other rectangle
h - the height of the other rectangle
Returns:
true if the rectangles intersect
See Also:
Area

contains

public boolean contains(double x,
                        double y,
                        double w,
                        double h)
Tests if this rectangle contains the given one. In other words, test if this rectangle contains all points in the given one.

Parameters:
x - the x coordinate of the other rectangle
y - the y coordinate of the other rectangle
w - the width of the other rectangle
h - the height of the other rectangle
Returns:
true if this rectangle contains the other
See Also:
Area

createIntersection

public abstract Rectangle2D createIntersection(Rectangle2D r)
Return a new rectangle which is the intersection of this and the given one. The result will be empty if there is no intersection.

Parameters:
r - the rectangle to be intersected
Returns:
the intersection
Throws:
NullPointerException - if r is null

intersect

public static void intersect(Rectangle2D src1,
                             Rectangle2D src2,
                             Rectangle2D dest)
Intersects a pair of rectangles, and places the result in the destination; this can be used to avoid object creation. This method even works when the destination is also a source, although you stand to lose the original data.

Parameters:
src1 - the first source
src2 - the second source
dest - the destination for the intersection
Throws:
NullPointerException - if any rectangle is null

createUnion

public abstract Rectangle2D createUnion(Rectangle2D r)
Return a new rectangle which is the union of this and the given one.

Parameters:
r - the rectangle to be merged
Returns:
the union
Throws:
NullPointerException - if r is null

union

public static void union(Rectangle2D src1,
                         Rectangle2D src2,
                         Rectangle2D dest)
Joins a pair of rectangles, and places the result in the destination; this can be used to avoid object creation. This method even works when the destination is also a source, although you stand to lose the original data.

Parameters:
src1 - the first source
src2 - the second source
dest - the destination for the union
Throws:
NullPointerException - if any rectangle is null

add

public void add(double newx,
                double newy)
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:
newx - the X coordinate of the point to add to this rectangle
newy - the Y coordinate of the point to add to this rectangle

add

public void add(Point2D 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(Rectangle2D 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(Rectangle2D, Rectangle2D, Rectangle2D)

getPathIterator

public PathIterator getPathIterator(AffineTransform at)
Return an iterator along the shape boundary. If the optional transform is provided, the iterator is transformed accordingly. Each call returns a new object, independent from others in use. This iterator is thread safe; modifications to the rectangle do not affect the results of this path instance.

Parameters:
at - an optional transform to apply to the iterator
Returns:
a new iterator over the boundary
Since:
1.2

getPathIterator

public PathIterator getPathIterator(AffineTransform at,
                                    double flatness)
Return an iterator along the shape boundary. If the optional transform is provided, the iterator is transformed accordingly. Each call returns a new object, independent from others in use. This iterator is thread safe; modifications to the rectangle do not affect the results of this path instance. As the rectangle is already flat, the flatness parameter is ignored.

Specified by:
getPathIterator in interface Shape
Overrides:
getPathIterator in class RectangularShape
Parameters:
at - an optional transform to apply to the iterator
flatness - the maximum distance for deviation from the real boundary
Returns:
a new iterator over the boundary
Since:
1.2

hashCode

public int hashCode()
Return the hashcode for this rectangle. The formula is not documented, but appears to be the same as:
 long l = Double.doubleToLongBits(getX())
   + 37 * Double.doubleToLongBits(getY())
   + 43 * Double.doubleToLongBits(getWidth())
   + 47 * Double.doubleToLongBits(getHeight());
 return (int) ((l >> 32) ^ l);
 

Overrides:
hashCode in class Object
Returns:
the hashcode
See Also:
Object.equals(Object), System.identityHashCode(Object)

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.

Overrides:
equals in class Object
Parameters:
obj - the object to test against for equality
Returns:
true if the specified object is equal to this one
See Also:
Object.hashCode()