home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 7.1 KB | 256 lines |
- /*
- * @(#)RectangularShape.java 1.6 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;
-
- import java.awt.Shape;
- import java.awt.Rectangle;
-
- /**
- * The base class for a number of shapes which inscribe a rectangular
- * set of outer bounds. This class provides common manipulation
- * routines for operating on a shape by querying and modifying its
- * bounding rectangle.
- *
- * @version 10 Feb 1997
- * @author Jim Graham
- */
- public abstract class RectangularShape implements Shape, Cloneable {
- protected RectangularShape() {
- }
-
- /**
- * Returns the X coordinate of the bounding rectangle in double precision.
- */
- public abstract double getX();
-
- /**
- * Returns the Y coordinate of the bounding rectangle in double precision.
- */
- public abstract double getY();
-
- /**
- * Returns the width of the bounding rectangle in double precision.
- */
- public abstract double getWidth();
-
- /**
- * Returns the height of the bounding rectangle in double precision.
- */
- public abstract double getHeight();
-
- /**
- * Returns the X coordinate of the left edge of the shape in
- * double precision.
- */
- public double getLeft() {
- return getX();
- }
-
- /**
- * Returns the Y coordinate of the top edge of the shape in
- * double precision.
- */
- public double getTop() {
- return getY();
- }
-
- /**
- * Returns the X coordinate of the right edge of the shape in
- * double precision.
- */
- public double getRight() {
- return getX() + getWidth();
- }
-
- /**
- * Returns the Y coordinate of the bottom edge of the shape in
- * double precision.
- */
- public double getBottom() {
- return getY() + getHeight();
- }
-
- /**
- * Returns the X coordinate of the center of the shape in
- * double precision.
- */
- public double getCenterX() {
- return getX() + getWidth() / 2.0;
- }
-
- /**
- * Returns the Y coordinate of the center of the shape in
- * double precision.
- */
- public double getCenterY() {
- return getY() + getHeight() / 2.0;
- }
-
- /**
- * Determines whether the rectangular shape is empty.
- */
- public abstract boolean isEmpty();
-
- /**
- * Sets the location and size of the outer bounds of this shape
- * to the specified rectangular values.
- */
- public abstract void setBounds(double x, double y, double w, double h);
-
- /**
- * Sets the outer bounds of this shape to be the same as the specified
- * Point and Dimension objects.
- */
- public void setBounds(Point2D loc, Dimension2D size) {
- setBounds(loc.getX(), loc.getY(), size.getWidth(), size.getHeight());
- }
-
- /**
- * Sets the outer bounds of this shape to be the same as the specified
- * Rectangle.
- */
- public void setBounds(Rectangle2D r) {
- setBounds(r.getX(), r.getY(), r.getWidth(), r.getHeight());
- }
-
- /**
- * Sets the outer bounds of this shape based on two points along
- * one of its diagonals.
- */
- public void setBoundsFromDiagonal(double x1, double y1,
- double x2, double y2) {
- if (x2 < x1) {
- double t = x1;
- x1 = x2;
- x2 = t;
- }
- if (y2 < y1) {
- double t = y1;
- y1 = y2;
- y2 = t;
- }
- setBounds(x1, y1, x2 - x1, y2 - y1);
- }
-
- /**
- * Sets the outer bounds of this shape based on two Point objects
- * along one of its diagonals.
- */
- public void setBoundsFromDiagonal(Point2D p1, Point2D p2) {
- setBoundsFromDiagonal(p1.getX(), p1.getY(), p2.getX(), p2.getY());
- }
-
- /**
- * Sets the outer bounds of this shape based on a center point
- * and a corner point.
- */
- public void setBoundsFromCenter(double centerX, double centerY,
- double cornerX, double cornerY) {
- double halfW = Math.abs(cornerX - centerX);
- double halfH = Math.abs(cornerY - centerY);
- setBounds(centerX - halfW, centerY - halfH, halfW * 2.0, halfH * 2.0);
- }
-
- /**
- * Sets the outer bounds of this shape based on a center point
- * and a corner point.
- */
- public void setBoundsFromCenter(Point2D center, Point2D corner) {
- setBoundsFromCenter(center.getX(), center.getY(),
- corner.getX(), corner.getY());
- }
-
- /**
- * Test if a given Point is inside the boundary of the shape.
- */
- public boolean contains(Point2D p) {
- return contains(p.getX(), p.getY());
- }
-
- /**
- * Test if the interior of the Shape intersects the interior of a given
- * Rectangle.
- */
- public boolean intersects(Rectangle2D r) {
- return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
- }
-
- /**
- * Test if the interior of the Shape entirely contains the given
- * Rectangle.
- */
- public boolean contains(Rectangle2D r) {
- return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
- }
-
- /**
- * Return the bounding box of the shape.
- */
- public Rectangle getBounds() {
- double width = getWidth();
- double height = getHeight();
- if (width < 0 || height < 0) {
- return new Rectangle();
- }
- double x = getX();
- double y = getY();
- double x1 = Math.floor(x);
- double y1 = Math.floor(y);
- double x2 = Math.ceil(x + width);
- double y2 = Math.ceil(y + height);
- return new Rectangle((int) x1, (int) y1,
- (int) (x2 - x1), (int) (y2 - y1));
- }
-
- /**
- * Return an iterator object that iterates along the boundary of
- * the shape and provides access to a flattened view of the
- * geometry of the outline of the shape.
- * Only SEG_MOVETO, SEG_LINETO, and SEG_CLOSE point types will
- * be returned by the iterator.
- * The amount of subdivision of the curved segments is controlled
- * by the <code>flatness</code> parameter which specifies ?REMIND?.
- * An optional affine transform can be specified in which case
- * the coordinates returned in the iteration will be transformed
- * accordingly.
- * @param at an optional AffineTransform to be applied to the
- * coordinates as they are returned in the iteration, or null
- * if the untransformed coordinates are desired.
- * @param flatness the maximum amount that the control points
- * for a given curve can vary from colinear before a subdivided
- * curve is replaced by a straight line connecting the endpoints.
- */
- public PathIterator getPathIterator(AffineTransform at, double flatness) {
- return new FlatteningPathIterator(getPathIterator(at), flatness);
- }
-
- /**
- * Creates a new object of the same class as this object.
- *
- * @return a clone of this instance.
- * @exception OutOfMemoryError if there is not enough memory.
- * @see java.lang.Cloneable
- * @since JDK1.2
- */
- public Object clone() {
- try {
- return super.clone();
- } catch (CloneNotSupportedException e) {
- // this shouldn't happen, since we are Cloneable
- throw new InternalError();
- }
- }
- }
-