home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 20.6 KB | 782 lines |
- /*
- * @(#)Rectangle2D.java 1.11 98/03/18
- *
- * Copyright 1997, 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- package java.awt.geom;
-
- /**
- * A rectangle defined by a location (x, y) and dimension (w x h).
- * <p>
- * This class is only the abstract superclass for all objects which
- * store a 2D rectangle.
- * The actual storage representation of the coordinates is left to
- * the subclass.
- *
- * @version 10 Feb 1997
- * @author Jim Graham
- */
- public abstract class Rectangle2D extends RectangularShape {
- /**
- * The bitmask which indicates that a point lies to the left of
- * this Rectangle.
- * @since JDK1.2
- */
- public static final int OUT_LEFT = 1;
-
- /**
- * The bitmask which indicates that a point lies above
- * this Rectangle.
- * @since JDK1.2
- */
- public static final int OUT_TOP = 2;
-
- /**
- * The bitmask which indicates that a point lies to the right of
- * this Rectangle.
- * @since JDK1.2
- */
- public static final int OUT_RIGHT = 4;
-
- /**
- * The bitmask which indicates that a point lies below
- * this Rectangle.
- * @since JDK1.2
- */
- public static final int OUT_BOTTOM = 8;
-
- /**
- * A rectangle specified in float coordinates.
- * @since JDK1.2
- */
- public static class Float extends Rectangle2D {
- /**
- * The x coordinate of the rectangle.
- * @since JDK1.2
- */
- public float x;
-
- /**
- * The y coordinate of the rectangle.
- * @since JDK1.2
- */
- public float y;
-
- /**
- * The width of the rectangle.
- * @since JDK1.2
- */
- public float width;
-
- /**
- * The height of the rectangle.
- * @since JDK1.2
- */
- public float height;
-
- /**
- * Constructs a new rectangle, initialized to location (0.0, 0.0) and
- * size (0.0, 0.0).
- * @since JDK1.2
- */
- public Float() {
- }
-
- /**
- * Constructs and initializes a rectangle from the specified
- * float coordinates.
- * @param x the x coordinate
- * @param y the y coordinate
- * @param width the width of the rectangle
- * @param height the height of the rectangle
- * @since JDK1.2
- */
- public Float(float x, float y, float w, float h) {
- setRect(x, y, w, h);
- }
-
- /**
- * Returns the X coordinate of the rectangle in double precision.
- * @since JDK1.2
- */
- public double getX() {
- return (double) x;
- }
-
- /**
- * Returns the Y coordinate of the rectangle in double precision.
- * @since JDK1.2
- */
- public double getY() {
- return (double) y;
- }
-
- /**
- * Returns the width of the rectangle in double precision.
- * @since JDK1.2
- */
- public double getWidth() {
- return (double) width;
- }
-
- /**
- * Returns the height of the rectangle in double precision.
- * @since JDK1.2
- */
- public double getHeight() {
- return (double) height;
- }
-
- /**
- * Determines whether the rectangle is empty.
- * @since JDK1.2
- */
- public boolean isEmpty() {
- return (width <= 0.0f) || (height <= 0.0f);
- }
-
- /**
- * Sets the location and size of this rectangle to the specified
- * float values.
- * @since JDK1.2
- */
- public void setRect(float x, float y, float w, float h) {
- this.x = x;
- this.y = y;
- this.width = w;
- this.height = h;
- }
-
- /**
- * Sets the location and size of this rectangle to the specified
- * double values.
- * @since JDK1.2
- */
- public void setRect(double x, double y, double w, double h) {
- this.x = (float) x;
- this.y = (float) y;
- this.width = (float) w;
- this.height = (float) h;
- }
-
- /**
- * Sets this rectangle to be the same as the specified Rectangle.
- * @since JDK1.2
- */
- public void setRect(Rectangle2D r) {
- this.x = (float) r.getX();
- this.y = (float) r.getY();
- this.width = (float) r.getWidth();
- this.height = (float) r.getHeight();
- }
-
- /**
- * Determines where the specified float coordinates lie with respect
- * to this Rectangle.
- * This method computes a binary OR of the appropriate mask values
- * indicating which sides of the rectangle the given point is
- * outside of.
- * @return the logical OR of all appropriate out codes
- * @see Rectangle2D#OUT_LEFT
- * @see Rectangle2D#OUT_TOP
- * @see Rectangle2D#OUT_RIGHT
- * @see Rectangle2D#OUT_BOTTOM
- * @since JDK1.2
- */
- public int outcode(double x, double y) {
- int out = 0;
- if (this.width <= 0) {
- out |= OUT_LEFT | OUT_RIGHT;
- } else if (x < this.x) {
- out |= OUT_LEFT;
- } else if (x > this.x + this.width) {
- out |= OUT_RIGHT;
- }
- if (this.height <= 0) {
- out |= OUT_TOP | OUT_BOTTOM;
- } else if (y < this.y) {
- out |= OUT_TOP;
- } else if (y > this.y + this.height) {
- out |= OUT_BOTTOM;
- }
- return out;
- }
-
- /**
- * Return a new Rectangle2D object representing the intersection of
- * this rectangle with the given rectangle.
- * @param r the rectangle to be intersected with
- * @return the largest rectangle contained in both the
- * specified rectangle and in this rectangle
- * @since JDK1.2
- */
- public Rectangle2D createIntersection(Rectangle2D r) {
- Rectangle2D dest;
- if (r instanceof Float) {
- dest = new Rectangle2D.Float();
- } else {
- dest = new Rectangle2D.Double();
- }
- Rectangle2D.intersect(this, r, dest);
- return dest;
- }
-
- /**
- * Return a new Rectangle2D object representing the union of
- * this rectangle with the given rectangle.
- * @param r the rectangle to be combined with
- * @return the smallest rectangle containing both the specified
- * rectangle and this rectangle.
- * @since JDK1.2
- */
- public Rectangle2D createUnion(Rectangle2D r) {
- Rectangle2D dest;
- if (r instanceof Float) {
- dest = new Rectangle2D.Float();
- } else {
- dest = new Rectangle2D.Double();
- }
- Rectangle2D.union(this, r, dest);
- return dest;
- }
-
- /**
- * Returns the String representation of this Rectangle.
- * @since JDK1.2
- */
- public String toString() {
- return getClass().getName()
- + "[x=" + x +
- ",y=" + y +
- ",w=" + width +
- ",h=" + height + "]";
- }
- }
-
- /**
- * A rectangle specified in double coordinates.
- * @since JDK1.2
- */
- public static class Double extends Rectangle2D {
- /**
- * The x coordinate of the rectangle.
- * @since JDK1.2
- */
- public double x;
-
- /**
- * The y coordinate of the rectangle.
- * @since JDK1.2
- */
- public double y;
-
- /**
- * The width of the rectangle.
- * @since JDK1.2
- */
- public double width;
-
- /**
- * The height of the rectangle.
- * @since JDK1.2
- */
- public double height;
-
- /**
- * Constructs a new rectangle, initialized to location (0, 0) and
- * size (0, 0).
- * @since JDK1.2
- */
- public Double() {
- }
-
- /**
- * Constructs and initializes a rectangle from the specified
- * double coordinates.
- * @param x the x coordinate
- * @param y the y coordinate
- * @param width the width of the rectangle
- * @param height the height of the rectangle
- * @since JDK1.2
- */
- public Double(double x, double y, double w, double h) {
- setRect(x, y, w, h);
- }
-
- /**
- * Returns the X coordinate of the rectangle in double precision.
- * @since JDK1.2
- */
- public double getX() {
- return x;
- }
-
- /**
- * Returns the Y coordinate of the rectangle in double precision.
- * @since JDK1.2
- */
- public double getY() {
- return y;
- }
-
- /**
- * Returns the width of the rectangle in double precision.
- * @since JDK1.2
- */
- public double getWidth() {
- return width;
- }
-
- /**
- * Returns the height of the rectangle in double precision.
- * @since JDK1.2
- */
- public double getHeight() {
- return height;
- }
-
- /**
- * Determines whether the rectangle is empty.
- * @since JDK1.2
- */
- public boolean isEmpty() {
- return (width <= 0.0) || (height <= 0.0);
- }
-
- /**
- * Sets the location and size of this rectangle to the specified
- * double values.
- * @since JDK1.2
- */
- public void setRect(double x, double y, double w, double h) {
- this.x = x;
- this.y = y;
- this.width = w;
- this.height = h;
- }
-
- /**
- * Sets this rectangle to be the same as the specified Rectangle.
- * @since JDK1.2
- */
- public void setRect(Rectangle2D r) {
- this.x = r.getX();
- this.y = r.getY();
- this.width = r.getWidth();
- this.height = r.getHeight();
- }
-
- /**
- * Determines where the specified double coordinates lie with respect
- * to this Rectangle.
- * This method computes a binary OR of the appropriate mask values
- * indicating which sides of the rectangle the given point is
- * outside of.
- * @return the logical OR of all appropriate out codes
- * @see Rectangle2D#OUT_LEFT
- * @see Rectangle2D#OUT_TOP
- * @see Rectangle2D#OUT_RIGHT
- * @see Rectangle2D#OUT_BOTTOM
- * @since JDK1.2
- */
- public int outcode(double x, double y) {
- int out = 0;
- if (this.width <= 0) {
- out |= OUT_LEFT | OUT_RIGHT;
- } else if (x < this.x) {
- out |= OUT_LEFT;
- } else if (x > this.x + this.width) {
- out |= OUT_RIGHT;
- }
- if (this.height <= 0) {
- out |= OUT_TOP | OUT_BOTTOM;
- } else if (y < this.y) {
- out |= OUT_TOP;
- } else if (y > this.y + this.height) {
- out |= OUT_BOTTOM;
- }
- return out;
- }
-
- /**
- * Return a new Rectangle2D object representing the intersection of
- * this rectangle with the given rectangle.
- * @param r the rectangle to be intersected with
- * @return the largest rectangle contained in both the
- * specified rectangle and in this rectangle
- * @since JDK1.2
- */
- public Rectangle2D createIntersection(Rectangle2D r) {
- Rectangle2D dest = new Rectangle2D.Double();
- Rectangle2D.intersect(this, r, dest);
- return dest;
- }
-
- /**
- * Return a new Rectangle2D object representing the union of
- * this rectangle with the given rectangle.
- * @param r the rectangle to be combined with
- * @return the smallest rectangle containing both the specified
- * rectangle and this rectangle.
- * @since JDK1.2
- */
- public Rectangle2D createUnion(Rectangle2D r) {
- Rectangle2D dest = new Rectangle2D.Double();
- Rectangle2D.union(this, r, dest);
- return dest;
- }
-
- /**
- * Returns the String representation of this Rectangle.
- * @since JDK1.2
- */
- public String toString() {
- return getClass().getName()
- + "[x=" + x +
- ",y=" + y +
- ",w=" + width +
- ",h=" + height + "]";
- }
- }
-
- protected Rectangle2D() {
- }
-
- /**
- * Sets the location and size of this rectangle to the specified
- * double values.
- * @since JDK1.2
- */
- public abstract void setRect(double x, double y, double w, double h);
-
- /**
- * Sets this rectangle to be the same as the specified Rectangle.
- * @since JDK1.2
- */
- public void setRect(Rectangle2D r) {
- setRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
- }
-
- /**
- * Tests if the given line segment intersects the interior of this
- * Rectangle.
- * @since JDK1.2
- */
- public boolean intersectsLine(double x1, double y1, double x2, double y2) {
- int out1 = outcode(x1, y1);
- int out2 = outcode(x2, y2);
- while (true) {
- if ((out1 & out2) != 0) {
- return false;
- }
- if (out1 == 0) {
- if (out2 == 0) {
- // Both are inside, line intersects
- return true;
- }
- double f = x1;
- x1 = x2;
- x2 = f;
- f = y1;
- y1 = y2;
- y2 = f;
- int out = out1;
- out1 = out2;
- out2 = out;
- }
- 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 if ((out1 & (OUT_TOP | OUT_BOTTOM)) != 0) {
- double y = getY();
- if ((out1 & OUT_BOTTOM) != 0) {
- y += getHeight();
- }
- x1 = x1 + (y - y1) * (x2 - x1) / (y2 - y1);
- y1 = y;
- }
- out1 = outcode(x1, y1);
- }
- }
-
- /**
- * Tests if the given line segment intersects the interior of this
- * Rectangle.
- * @since JDK1.2
- */
- public boolean intersectsLine(Line2D l) {
- return intersectsLine(l.getX1(), l.getY1(), l.getX2(), l.getY2());
- }
-
- /**
- * Determines where the specified coordinates lie with respect
- * to this Rectangle.
- * This method computes a binary OR of the appropriate mask values
- * indicating which sides of the rectangle the given point is
- * outside of.
- * @return the logical OR of all appropriate out codes
- * @see #OUT_LEFT
- * @see #OUT_TOP
- * @see #OUT_RIGHT
- * @see #OUT_BOTTOM
- * @since JDK1.2
- */
- public abstract int outcode(double x, double y);
-
- /**
- * Determines where the specified point lies with respect to this
- * Rectangle.
- * This method computes a binary OR of the appropriate mask values
- * indicating which sides of the rectangle the given point is
- * outside of.
- * @return the logical OR of all appropriate out codes
- * @see #OUT_LEFT
- * @see #OUT_TOP
- * @see #OUT_RIGHT
- * @see #OUT_BOTTOM
- * @since JDK1.2
- */
- public int outcode(Point2D p) {
- return outcode(p.getX(), p.getY());
- }
-
- /**
- * Sets the location and size of the outer bounds of this shape
- * to the specified rectangular values.
- * @since JDK1.2
- */
- public void setBounds(double x, double y, double w, double h) {
- setRect(x, y, w, h);
- }
-
- /**
- * Return the high precision bounding box of the shape.
- * @since JDK1.2
- */
- public Rectangle2D getBounds2D() {
- return this;
- }
-
- /**
- * Test if a given coordinate is inside the boundary of the shape.
- * @since JDK1.2
- */
- public boolean contains(double x, double y) {
- double x0 = getX();
- double y0 = getY();
- return (x >= x0 &&
- y >= y0 &&
- x < x0 + getWidth() &&
- y < y0 + getHeight());
- }
-
- /**
- * Test if the interior of the Shape intersects the interior of a given
- * set of rectangular coordinates.
- * @since JDK1.2
- */
- public boolean intersects(double x, double y, double w, double h) {
- 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());
- }
-
- /**
- * Test if the interior of the Shape entirely contains the given
- * set of rectangular coordinates.
- * @since JDK1.2
- */
- public boolean contains(double x, double y, double w, double h) {
- 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());
- }
-
- /**
- * Return a new Rectangle2D object representing the intersection of
- * this rectangle with the given rectangle.
- * @param r the rectangle to be intersected with
- * @return the largest rectangle contained in both the
- * specified rectangle and in this rectangle
- * @since JDK1.2
- */
- public abstract Rectangle2D createIntersection(Rectangle2D r);
-
- /**
- * Intersect the pair of Rectangles and put the result into
- * the indicated destination Rectangle2D object.
- * @param src1 the first rectangle to be intersected
- * @param src2 the second rectangle to be intersected
- * @param dest the rectangle to store the results in
- * @since JDK1.2
- */
- public static void intersect(Rectangle2D src1,
- Rectangle2D src2,
- Rectangle2D dest) {
- double x1 = Math.max(src1.getLeft(), src2.getLeft());
- double y1 = Math.max(src1.getTop(), src2.getTop());
- double x2 = Math.min(src1.getRight(), src2.getRight());
- double y2 = Math.min(src1.getBottom(), src2.getBottom());
- dest.setBoundsFromDiagonal(x1, y1, x2, y2);
- }
-
- /**
- * Return a new Rectangle2D object representing the union of
- * this rectangle with the given rectangle.
- * @param r the rectangle to be combined with
- * @return the smallest rectangle containing both the specified
- * rectangle and this rectangle.
- * @since JDK1.2
- */
- public abstract Rectangle2D createUnion(Rectangle2D r);
-
- /**
- * Union the pair of Rectangles and put the result into
- * the indicated destination Rectangle2D object.
- * @param src1 the first rectangle to be combined
- * @param src2 the second rectangle to be combined
- * @param dest the rectangle to store the results in
- * @since JDK1.2
- */
- public static void union(Rectangle2D src1,
- Rectangle2D src2,
- Rectangle2D dest) {
- double x1 = Math.min(src1.getLeft(), src2.getLeft());
- double y1 = Math.min(src1.getTop(), src2.getTop());
- double x2 = Math.max(src1.getRight(), src2.getRight());
- double y2 = Math.max(src1.getBottom(), src2.getBottom());
- dest.setBoundsFromDiagonal(x1, y1, x2, y2);
- }
-
- /**
- * Adds a point, specified by the double precision arguments
- * <code>newx</code> and <code>newy</code>, to this rectangle.
- * The resulting rectangle is the smallest rectangle that
- * contains both the original rectangle and the specified point.
- * <p>
- * After adding a point, a call to <code>contains<code> with the
- * added point as an argument will not necessarily return
- * <code>true</code>. The <code>contains</code> method does not
- * return <code>true</code> 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,
- * <code>contains</code> will return <code>false</code> for that point.
- *
- * @param newx the <i>x</i> coordinate of the new point.
- * @param newy the <i>y</i> coordinate of the new point.
- * @since JDK1.0
- */
- public void add(double newx, double newy) {
- double x1 = Math.min(getLeft(), newx);
- double x2 = Math.max(getRight(), newx);
- double y1 = Math.min(getTop(), newy);
- double y2 = Math.max(getBottom(), newy);
- setRect(x1, y1, x2 - x1, y2 - y1);
- }
-
- /**
- * Adds the Point2D object <code>pt</code> to this rectangle.
- * The resulting rectangle is the smallest rectangle that
- * contains both the original rectangle and the specified point.
- * <p>
- * After adding a point, a call to <code>contains<code> with the
- * added point as an argument will not necessarily return
- * <code>true</code>. The <code>contains</code> method does not
- * return <code>true</code> 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,
- * <code>contains</code> will return <code>false</code> for that point.
- *
- * @param pt the new point to add to the rectangle.
- * @since JDK1.0
- */
- public void add(Point2D pt) {
- add(pt.getX(), pt.getY());
- }
-
- /**
- * Adds a Rectangle2D object to this rectangle.
- * The resulting rectangle is the union of the two rectangles.
- * @param a rectangle.
- * @since JDK1.0
- */
- public void add(Rectangle2D r) {
- double x1 = Math.min(getLeft(), r.getLeft());
- double x2 = Math.max(getRight(), r.getRight());
- double y1 = Math.min(getTop(), r.getTop());
- double y2 = Math.max(getBottom(), r.getBottom());
- setRect(x1, y1, x2 - x1, y2 - y1);
- }
-
- /**
- * Return an iteration object that defines the boundary of the
- * shape.
- * @param at the AffineTransform by which to transform the points
- * @since JDK1.2
- */
- public PathIterator getPathIterator(AffineTransform at) {
- return new RectIterator(this, at);
- }
-
- /**
- * Return an iteration object that defines the boundary of the
- * flattened shape.
- * @param at the AffineTransform by which to transform the points
- * @param flatness the maximum approximation error desired for the
- * flattened path
- * @since JDK1.2
- */
- public PathIterator getPathIterator(AffineTransform at, double flatness) {
- return new RectIterator(this, at);
- }
-
- /**
- * Determines whether two rectangles are equal. Two instances of
- * <code>Rectangle2D</code> are equal if their location and size
- * are the same.
- * @param obj an object to be compared with this rectangle.
- * @return <code>true</code> if the object to be compared is
- * an instance of <code>Rectangle2D</code> and has
- * the same values; <code>false</code> otherwise.
- * @since JDK1.2
- */
- public boolean equals(Object obj) {
- if (obj == this) {
- return true;
- }
- if (obj != null && obj instanceof Rectangle2D) {
- Rectangle2D r2d = (Rectangle2D) obj;
- return ((getX() == r2d.getX()) &&
- (getY() == r2d.getY()) &&
- (getWidth() == r2d.getWidth()) &&
- (getHeight() == r2d.getHeight()));
- }
- return super.equals(obj);
- }
- }
-