home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / geom / RectangularShape.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  7.1 KB  |  256 lines

  1. /*
  2.  * @(#)RectangularShape.java    1.6 98/03/18
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt.geom;
  16.  
  17. import java.awt.Shape;
  18. import java.awt.Rectangle;
  19.  
  20. /**
  21.  * The base class for a number of shapes which inscribe a rectangular
  22.  * set of outer bounds.  This class provides common manipulation
  23.  * routines for operating on a shape by querying and modifying its
  24.  * bounding rectangle.
  25.  *
  26.  * @version 10 Feb 1997
  27.  * @author    Jim Graham
  28.  */
  29. public abstract class RectangularShape implements Shape, Cloneable {
  30.     protected RectangularShape() {
  31.     }
  32.  
  33.     /**
  34.      * Returns the X coordinate of the bounding rectangle in double precision.
  35.      */
  36.     public abstract double getX();
  37.  
  38.     /**
  39.      * Returns the Y coordinate of the bounding rectangle in double precision.
  40.      */
  41.     public abstract double getY();
  42.  
  43.     /**
  44.      * Returns the width of the bounding rectangle in double precision.
  45.      */
  46.     public abstract double getWidth();
  47.  
  48.     /**
  49.      * Returns the height of the bounding rectangle in double precision.
  50.      */
  51.     public abstract double getHeight();
  52.  
  53.     /**
  54.      * Returns the X coordinate of the left edge of the shape in
  55.      * double precision.
  56.      */
  57.     public double getLeft() {
  58.     return getX();
  59.     }
  60.  
  61.     /**
  62.      * Returns the Y coordinate of the top edge of the shape in
  63.      * double precision.
  64.      */
  65.     public double getTop() {
  66.     return getY();
  67.     }
  68.  
  69.     /**
  70.      * Returns the X coordinate of the right edge of the shape in
  71.      * double precision.
  72.      */
  73.     public double getRight() {
  74.     return getX() + getWidth();
  75.     }
  76.  
  77.     /**
  78.      * Returns the Y coordinate of the bottom edge of the shape in
  79.      * double precision.
  80.      */
  81.     public double getBottom() {
  82.     return getY() + getHeight();
  83.     }
  84.  
  85.     /**
  86.      * Returns the X coordinate of the center of the shape in
  87.      * double precision.
  88.      */
  89.     public double getCenterX() {
  90.     return getX() + getWidth() / 2.0;
  91.     }
  92.  
  93.     /**
  94.      * Returns the Y coordinate of the center of the shape in
  95.      * double precision.
  96.      */
  97.     public double getCenterY() {
  98.     return getY() + getHeight() / 2.0;
  99.     }
  100.  
  101.     /**
  102.      * Determines whether the rectangular shape is empty.
  103.      */
  104.     public abstract boolean isEmpty();
  105.  
  106.     /**
  107.      * Sets the location and size of the outer bounds of this shape
  108.      * to the specified rectangular values.
  109.      */
  110.     public abstract void setBounds(double x, double y, double w, double h);
  111.  
  112.     /**
  113.      * Sets the outer bounds of this shape to be the same as the specified
  114.      * Point and Dimension objects.
  115.      */
  116.     public void setBounds(Point2D loc, Dimension2D size) {
  117.     setBounds(loc.getX(), loc.getY(), size.getWidth(), size.getHeight());
  118.     }
  119.  
  120.     /**
  121.      * Sets the outer bounds of this shape to be the same as the specified
  122.      * Rectangle.
  123.      */
  124.     public void setBounds(Rectangle2D r) {
  125.     setBounds(r.getX(), r.getY(), r.getWidth(), r.getHeight());
  126.     }
  127.  
  128.     /**
  129.      * Sets the outer bounds of this shape based on two points along
  130.      * one of its diagonals.
  131.      */
  132.     public void setBoundsFromDiagonal(double x1, double y1,
  133.                       double x2, double y2) {
  134.     if (x2 < x1) {
  135.         double t = x1;
  136.         x1 = x2;
  137.         x2 = t;
  138.     }
  139.     if (y2 < y1) {
  140.         double t = y1;
  141.         y1 = y2;
  142.         y2 = t;
  143.     }
  144.     setBounds(x1, y1, x2 - x1, y2 - y1);
  145.     }
  146.  
  147.     /**
  148.      * Sets the outer bounds of this shape based on two Point objects
  149.      * along one of its diagonals.
  150.      */
  151.     public void setBoundsFromDiagonal(Point2D p1, Point2D p2) {
  152.     setBoundsFromDiagonal(p1.getX(), p1.getY(), p2.getX(), p2.getY());
  153.     }
  154.  
  155.     /**
  156.      * Sets the outer bounds of this shape based on a center point
  157.      * and a corner point.
  158.      */
  159.     public void setBoundsFromCenter(double centerX, double centerY,
  160.                     double cornerX, double cornerY) {
  161.     double halfW = Math.abs(cornerX - centerX);
  162.     double halfH = Math.abs(cornerY - centerY);
  163.     setBounds(centerX - halfW, centerY - halfH, halfW * 2.0, halfH * 2.0);
  164.     }
  165.  
  166.     /**
  167.      * Sets the outer bounds of this shape based on a center point
  168.      * and a corner point.
  169.      */
  170.     public void setBoundsFromCenter(Point2D center, Point2D corner) {
  171.     setBoundsFromCenter(center.getX(), center.getY(),
  172.                 corner.getX(), corner.getY());
  173.     }
  174.  
  175.     /**
  176.      * Test if a given Point is inside the boundary of the shape.
  177.      */
  178.     public boolean contains(Point2D p) {
  179.     return contains(p.getX(), p.getY());
  180.     }
  181.  
  182.     /**
  183.      * Test if the interior of the Shape intersects the interior of a given
  184.      * Rectangle.
  185.      */
  186.     public boolean intersects(Rectangle2D r) {
  187.     return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
  188.     }
  189.  
  190.     /**
  191.      * Test if the interior of the Shape entirely contains the given
  192.      * Rectangle.
  193.      */
  194.     public boolean contains(Rectangle2D r) {
  195.     return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
  196.     }
  197.  
  198.     /**
  199.      * Return the bounding box of the shape.
  200.      */
  201.     public Rectangle getBounds() {
  202.     double width = getWidth();
  203.     double height = getHeight();
  204.     if (width < 0 || height < 0) {
  205.         return new Rectangle();
  206.     }
  207.     double x = getX();
  208.     double y = getY();
  209.     double x1 = Math.floor(x);
  210.     double y1 = Math.floor(y);
  211.     double x2 = Math.ceil(x + width);
  212.     double y2 = Math.ceil(y + height);
  213.     return new Rectangle((int) x1, (int) y1,
  214.                       (int) (x2 - x1), (int) (y2 - y1));
  215.     }
  216.  
  217.     /**
  218.      * Return an iterator object that iterates along the boundary of
  219.      * the shape and provides access to a flattened view of the
  220.      * geometry of the outline of the shape.
  221.      * Only SEG_MOVETO, SEG_LINETO, and SEG_CLOSE point types will
  222.      * be returned by the iterator.
  223.      * The amount of subdivision of the curved segments is controlled
  224.      * by the <code>flatness</code> parameter which specifies ?REMIND?.
  225.      * An optional affine transform can be specified in which case
  226.      * the coordinates returned in the iteration will be transformed
  227.      * accordingly.
  228.      * @param at an optional AffineTransform to be applied to the
  229.      * coordinates as they are returned in the iteration, or null
  230.      * if the untransformed coordinates are desired.
  231.      * @param flatness the maximum amount that the control points
  232.      * for a given curve can vary from colinear before a subdivided
  233.      * curve is replaced by a straight line connecting the endpoints.
  234.      */
  235.     public PathIterator getPathIterator(AffineTransform at, double flatness) {
  236.     return new FlatteningPathIterator(getPathIterator(at), flatness);
  237.     }
  238.  
  239.     /**
  240.      * Creates a new object of the same class as this object.
  241.      *
  242.      * @return     a clone of this instance.
  243.      * @exception  OutOfMemoryError            if there is not enough memory.
  244.      * @see        java.lang.Cloneable
  245.      * @since      JDK1.2
  246.      */
  247.     public Object clone() {
  248.     try {
  249.         return super.clone();
  250.     } catch (CloneNotSupportedException e) {
  251.         // this shouldn't happen, since we are Cloneable
  252.         throw new InternalError();
  253.     }
  254.     }
  255. }
  256.