FileDocCategorySizeDatePackage
Rectangle2D.javaAPI DocJava SE 5 API33178Fri Aug 26 14:56:52 BST 2005java.awt.geom

Rectangle2D

public abstract class Rectangle2D extends RectangularShape
The 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.

version
1.29, 12/19/03
author
Jim Graham

Fields Summary
public static final int
OUT_LEFT
The bitmask that indicates that a point lies to the left of this Rectangle2D.
public static final int
OUT_TOP
The bitmask that indicates that a point lies above this Rectangle2D.
public static final int
OUT_RIGHT
The bitmask that indicates that a point lies to the right of this Rectangle2D.
public static final int
OUT_BOTTOM
The bitmask that indicates that a point lies below this Rectangle2D.
Constructors Summary
protected Rectangle2D()
This is an abstract class that cannot be instantiated directly. Type-specific implementation subclasses are available for instantiation and provide a number of formats for storing the information necessary to satisfy the various accessor methods below.

see
java.awt.geom.Rectangle2D.Float
see
java.awt.geom.Rectangle2D.Double
see
java.awt.Rectangle

    
Methods Summary
public voidadd(double newx, double newy)
Adds a point, specified by the double precision arguments 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 left or bottom edge of the enlarged rectangle, contains returns false for that point.

param
newx, newy the coordinates of the new point
since
JDK1.0

	double x1 = Math.min(getMinX(), newx);
	double x2 = Math.max(getMaxX(), newx);
	double y1 = Math.min(getMinY(), newy);
	double y2 = Math.max(getMaxY(), newy);
	setRect(x1, y1, x2 - x1, y2 - y1);
    
public voidadd(java.awt.geom.Point2D pt)
Adds the 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 left or bottom edge of the enlarged rectangle, contains returns false for that point.

param
pt the new Point2D to add to this Rectangle2D.
since
JDK1.0

	add(pt.getX(), pt.getY());
    
public voidadd(java.awt.geom.Rectangle2D r)
Adds a Rectangle2D object to this Rectangle2D. The resulting Rectangle2D is the union of the two Rectangle2D objects.

param
r the Rectangle2D to add to this Rectangle2D.
since
JDK1.0

	double x1 = Math.min(getMinX(), r.getMinX());
	double x2 = Math.max(getMaxX(), r.getMaxX());
	double y1 = Math.min(getMinY(), r.getMinY());
	double y2 = Math.max(getMaxY(), r.getMaxY());
	setRect(x1, y1, x2 - x1, y2 - y1);
    
public booleancontains(double x, double y)
Tests if a specified coordinate is inside the boundary of this Rectangle2D.

param
x, y the coordinates to test
return
true if the specified coordinates are inside the boundary of this Rectangle2D; false otherwise.
since
1.2

	double x0 = getX();
	double y0 = getY();
	return (x >= x0 &&
		y >= y0 &&
		x < x0 + getWidth() &&
		y < y0 + getHeight());
    
public booleancontains(double x, double y, double w, double h)
Tests if the interior of this Rectangle2D entirely contains the specified set of rectangular coordinates.

param
x, y the coordinates of the upper left corner of the specified set of rectangular coordinates
param
w the width of the specified set of rectangular coordinates
param
h the height of the specified set of rectangular coordinates
return
true if this Rectangle2D entirely contains specified set of rectangular coordinates; false otherwise.
since
1.2

	if (isEmpty() || w <= 0 || h <= 0) {
	    return false;
	}
	double x0 = getX();
	double y0 = getY();
	return (x >= x0 &&
		y >= y0 &&
		(x + w) <= x0 + getWidth() &&
		(y + h) <= y0 + getHeight());
    
public abstract java.awt.geom.Rectangle2DcreateIntersection(java.awt.geom.Rectangle2D r)
Returns a new Rectangle2D object representing the intersection of this Rectangle2D with the specified Rectangle2D.

param
r the Rectangle2D to be intersected with this Rectangle2D
return
the largest Rectangle2D contained in both the specified Rectangle2D and in this Rectangle2D.
since
1.2

public abstract java.awt.geom.Rectangle2DcreateUnion(java.awt.geom.Rectangle2D r)
Returns a new Rectangle2D object representing the union of this Rectangle2D with the specified Rectangle2D.

param
r the Rectangle2D to be combined with this Rectangle2D
return
the smallest Rectangle2D containing both the specified Rectangle2D and this Rectangle2D.
since
1.2

public booleanequals(java.lang.Object obj)
Determines whether or not the specified 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.

param
obj an Object to be compared with this Rectangle2D.
return
true if obj is an instance of Rectangle2D and has the same values; false otherwise.
since
1.2

	if (obj == this) {
	    return true;
	}
	if (obj instanceof Rectangle2D) {
	    Rectangle2D r2d = (Rectangle2D) obj;
	    return ((getX() == r2d.getX()) &&
		    (getY() == r2d.getY()) &&
		    (getWidth() == r2d.getWidth()) &&
		    (getHeight() == r2d.getHeight()));
	}
	return false;
    
public java.awt.geom.Rectangle2DgetBounds2D()
Returns the high precision bounding box of this Rectangle2D.

return
the bounding box of this Rectangle2D.
since
1.2

	return (Rectangle2D) clone();
    
public java.awt.geom.PathIteratorgetPathIterator(java.awt.geom.AffineTransform at)
Returns an iteration object that defines the boundary of this 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.

param
at an optional AffineTransform to be applied to the coordinates as they are returned in the iteration, or null if untransformed coordinates are desired
return
the PathIterator object that returns the geometry of the outline of this Rectangle2D, one segment at a time.
since
1.2

	return new RectIterator(this, at);
    
public java.awt.geom.PathIteratorgetPathIterator(java.awt.geom.AffineTransform at, double flatness)
Returns an iteration object that defines the boundary of the flattened 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.

param
at an optional AffineTransform to be applied to the coordinates as they are returned in the iteration, or null if untransformed coordinates are desired
param
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, the flatness parameter is ignored.
return
the PathIterator object that returns the geometry of the outline of this Rectangle2D, one segment at a time.
since
1.2

	return new RectIterator(this, at);
    
public inthashCode()
Returns the hashcode for this Rectangle2D.

return
the hashcode for this Rectangle2D.

	long bits = java.lang.Double.doubleToLongBits(getX());
	bits += java.lang.Double.doubleToLongBits(getY()) * 37;
	bits += java.lang.Double.doubleToLongBits(getWidth()) * 43;
	bits += java.lang.Double.doubleToLongBits(getHeight()) * 47;
	return (((int) bits) ^ ((int) (bits >> 32)));
    
public static voidintersect(java.awt.geom.Rectangle2D src1, java.awt.geom.Rectangle2D src2, java.awt.geom.Rectangle2D dest)
Intersects the pair of specified source 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.

param
src1 the first of a pair of Rectangle2D objects to be intersected with each other
param
src2 the second of a pair of Rectangle2D objects to be intersected with each other
param
dest the Rectangle2D that holds the results of the intersection of src1 and src2
since
1.2

	double x1 = Math.max(src1.getMinX(), src2.getMinX());
	double y1 = Math.max(src1.getMinY(), src2.getMinY());
	double x2 = Math.min(src1.getMaxX(), src2.getMaxX());
	double y2 = Math.min(src1.getMaxY(), src2.getMaxY());
	dest.setFrame(x1, y1, x2-x1, y2-y1);
    
public booleanintersects(double x, double y, double w, double h)
Tests if the interior of this Rectangle2D intersects the interior of a specified set of rectangular coordinates.

param
x, y the coordinates of the upper left corner of the specified set of rectangular coordinates
param
w the width of the specified set of rectangular coordinates
param
h the height of the specified set of rectangular coordinates
return
true if this Rectangle2D intersects the interior of a specified set of rectangular coordinates; false otherwise.
since
1.2

	if (isEmpty() || w <= 0 || h <= 0) {
	    return false;
	}
	double x0 = getX();
	double y0 = getY();
	return (x + w > x0 &&
		y + h > y0 &&
		x < x0 + getWidth() &&
		y < y0 + getHeight());
    
public booleanintersectsLine(double x1, double y1, double x2, double y2)
Tests if the specified line segment intersects the interior of this Rectangle2D.

param
x1, y1 the first endpoint of the specified line segment
param
x2, y2 the second endpoint of the specified line segment
return
true if the specified line segment intersects the interior of this Rectangle2D; false otherwise.
since
1.2

	int out1, out2;
	if ((out2 = outcode(x2, y2)) == 0) {
	    return true;
	}
	while ((out1 = outcode(x1, y1)) != 0) {
	    if ((out1 & out2) != 0) {
		return false;
	    }
	    if ((out1 & (OUT_LEFT | OUT_RIGHT)) != 0) {
		double x = getX();
		if ((out1 & OUT_RIGHT) != 0) {
		    x += getWidth();
		}
		y1 = y1 + (x - x1) * (y2 - y1) / (x2 - x1);
		x1 = x;
	    } else {
		double y = getY();
		if ((out1 & OUT_BOTTOM) != 0) {
		    y += getHeight();
		}
		x1 = x1 + (y - y1) * (x2 - x1) / (y2 - y1);
		y1 = y;
	    }
	}
	return true;
    
public booleanintersectsLine(java.awt.geom.Line2D l)
Tests if the specified line segment intersects the interior of this Rectangle2D.

param
l the specified {@link Line2D} to test for intersection with the interior of this Rectangle2D
return
true if the specified Line2D intersects the interior of this Rectangle2D; false otherwise.
since
1.2

	return intersectsLine(l.getX1(), l.getY1(), l.getX2(), l.getY2());
    
public abstract intoutcode(double x, double y)
Determines where the specified coordinates lie 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 coordinates are on the same side of the edge as the rest of this Rectangle2D.

param
x, y the specified coordinates
return
the logical OR of all appropriate out codes.
see
#OUT_LEFT
see
#OUT_TOP
see
#OUT_RIGHT
see
#OUT_BOTTOM
since
1.2

public intoutcode(java.awt.geom.Point2D p)
Determines where the specified {@link 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.

param
p the specified Point2D
return
the logical OR of all appropriate out codes.
see
#OUT_LEFT
see
#OUT_TOP
see
#OUT_RIGHT
see
#OUT_BOTTOM
since
1.2

	return outcode(p.getX(), p.getY());
    
public voidsetFrame(double x, double y, double w, double h)
Sets the location and size of the outer bounds of this Rectangle2D to the specified rectangular values.

param
x, y the coordinates to which to set the location of the upper left corner of the outer bounds of this Rectangle2D
param
w the value to use to set the width of the outer bounds of this Rectangle2D
param
h the value to use to set the height of the outer bounds of this Rectangle2D
since
1.2

	setRect(x, y, w, h);
    
public abstract voidsetRect(double x, double y, double w, double h)
Sets the location and size of this Rectangle2D to the specified double values.

param
x, y the coordinates to which to set the location of the upper left corner of this Rectangle2D
param
w the value to use to set the width of this Rectangle2D
param
h the value to use to set the height of this Rectangle2D
since
1.2

public voidsetRect(java.awt.geom.Rectangle2D r)
Sets this Rectangle2D to be the same as the specified Rectangle2D.

param
r the specified Rectangle2D
since
1.2

	setRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
    
public static voidunion(java.awt.geom.Rectangle2D src1, java.awt.geom.Rectangle2D src2, java.awt.geom.Rectangle2D dest)
Unions the pair of source 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.

param
src1 the first of a pair of Rectangle2D objects to be combined with each other
param
src2 the second of a pair of Rectangle2D objects to be combined with each other
param
dest the Rectangle2D that holds the results of the union of src1 and src2
since
1.2

	double x1 = Math.min(src1.getMinX(), src2.getMinX());
	double y1 = Math.min(src1.getMinY(), src2.getMinY());
	double x2 = Math.max(src1.getMaxX(), src2.getMaxX());
	double y2 = Math.max(src1.getMaxY(), src2.getMaxY());
	dest.setFrameFromDiagonal(x1, y1, x2, y2);