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 / Ellipse2D.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  7.2 KB  |  324 lines

  1. /*
  2.  * @(#)Ellipse2D.java    1.5 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. /**
  18.  * An ellipse defined by a bounding rectangle.
  19.  * <p>
  20.  * This class is only the abstract superclass for all objects which
  21.  * store a 2D ellipse.
  22.  * The actual storage representation of the coordinates is left to
  23.  * the subclass.
  24.  *
  25.  * @version 10 Feb 1997
  26.  * @author    Jim Graham
  27.  */
  28. public abstract class Ellipse2D extends RectangularShape {
  29.     /**
  30.      * An ellipse specified in float precision.
  31.      */
  32.     public static class Float extends Ellipse2D {
  33.     /**
  34.      * The x coordinate of the upper left corner of the ellipse.
  35.      */
  36.     public float x;
  37.  
  38.     /**
  39.      * The y coordinate of the upper left corner of the ellipse.
  40.      */
  41.     public float y;
  42.  
  43.     /**
  44.      * The overall width of the ellipse.
  45.      */
  46.     public float width;
  47.  
  48.     /**
  49.      * The overall height of the ellipse.
  50.      */
  51.     public float height;
  52.  
  53.     /**
  54.      * Constructs a new ellipse, initialized to location (0, 0) and
  55.      * size (0, 0).
  56.      */
  57.     public Float() {
  58.     }
  59.  
  60.     /**
  61.      * Constructs and initializes an ellipse from the specified coordinates.
  62.      * @param x the x coordinate
  63.      * @param y the y coordinate
  64.      * @param width the width of the rectangle
  65.      * @param height the height of the rectangle
  66.      */
  67.     public Float(float x, float y, float w, float h) {
  68.         setBounds(x, y, w, h);
  69.     }
  70.  
  71.     /**
  72.      * Returns the X coordinate of the upper left corner of the ellipse
  73.      * in double precision.
  74.      */
  75.     public double getX() {
  76.         return (double) x;
  77.     }
  78.  
  79.     /**
  80.      * Returns the Y coordinate of the upper left corner of the ellipse
  81.      * in double precision.
  82.      */
  83.     public double getY() {
  84.         return (double) y;
  85.     }
  86.  
  87.     /**
  88.      * Returns the overall width of the ellipse in double precision.
  89.      */
  90.     public double getWidth() {
  91.         return (double) width;
  92.     }
  93.  
  94.     /**
  95.      * Returns the overall height of the ellipse in double precision.
  96.      */
  97.     public double getHeight() {
  98.         return (double) height;
  99.     }
  100.  
  101.     /**
  102.      * Determines whether the rectangular shape is empty.
  103.      */
  104.     public boolean isEmpty() {
  105.         return (width <= 0.0 || height <= 0.0);
  106.     }
  107.  
  108.     /**
  109.      * Sets the location and size of this ellipse to the specified
  110.      * float values.
  111.      */
  112.     public void setBounds(float x, float y, float w, float h) {
  113.         this.x = x;
  114.         this.y = y;
  115.         this.width = w;
  116.         this.height = h;
  117.     }
  118.  
  119.     /**
  120.      * Sets the location and size of this ellipse to the specified
  121.      * double values.
  122.      */
  123.     public void setBounds(double x, double y, double w, double h) {
  124.         this.x = (float) x;
  125.         this.y = (float) y;
  126.         this.width = (float) w;
  127.         this.height = (float) h;
  128.     }
  129.  
  130.     /**
  131.      * Return the high precision bounding box of the shape.
  132.      */
  133.     public Rectangle2D getBounds2D() {
  134.         return new Rectangle2D.Float(x, y, width, height);
  135.     }
  136.     }
  137.  
  138.     /**
  139.      * An ellipse specified in double precision.
  140.      */
  141.     public static class Double extends Ellipse2D {
  142.     /**
  143.      * The x coordinate of the upper left corner of the ellipse.
  144.      */
  145.     public double x;
  146.  
  147.     /**
  148.      * The y coordinate of the upper left corner of the ellipse.
  149.      */
  150.     public double y;
  151.  
  152.     /**
  153.      * The overall width of the ellipse.
  154.      */
  155.     public double width;
  156.  
  157.     /**
  158.      * The overall height of the ellipse.
  159.      */
  160.     public double height;
  161.  
  162.     /**
  163.      * Constructs a new ellipse, initialized to location (0, 0) and
  164.      * size (0, 0).
  165.      */
  166.     public Double() {
  167.     }
  168.  
  169.     /**
  170.      * Constructs and initializes an ellipse from the specified coordinates.
  171.      * @param x the x coordinate
  172.      * @param y the y coordinate
  173.      * @param width the width of the rectangle
  174.      * @param height the height of the rectangle
  175.      */
  176.     public Double(double x, double y, double w, double h) {
  177.         setBounds(x, y, w, h);
  178.     }
  179.  
  180.     /**
  181.      * Returns the X coordinate of the upper left corner of the ellipse
  182.      * in double precision.
  183.      */
  184.     public double getX() {
  185.         return x;
  186.     }
  187.  
  188.     /**
  189.      * Returns the Y coordinate of the upper left corner of the ellipse
  190.      * in double precision.
  191.      */
  192.     public double getY() {
  193.         return y;
  194.     }
  195.  
  196.     /**
  197.      * Returns the overall width of the ellipse in double precision.
  198.      */
  199.     public double getWidth() {
  200.         return width;
  201.     }
  202.  
  203.     /**
  204.      * Returns the overall height of the ellipse in double precision.
  205.      */
  206.     public double getHeight() {
  207.         return height;
  208.     }
  209.  
  210.     /**
  211.      * Determines whether the rectangular shape is empty.
  212.      */
  213.     public boolean isEmpty() {
  214.         return (width <= 0.0 || height <= 0.0);
  215.     }
  216.  
  217.     /**
  218.      * Sets the location and size of this ellipse to the specified
  219.      * double values.
  220.      */
  221.     public void setBounds(double x, double y, double w, double h) {
  222.         this.x = x;
  223.         this.y = y;
  224.         this.width = w;
  225.         this.height = h;
  226.     }
  227.  
  228.     /**
  229.      * Return the high precision bounding box of the shape.
  230.      */
  231.     public Rectangle2D getBounds2D() {
  232.         return new Rectangle2D.Double(x, y, width, height);
  233.     }
  234.     }
  235.  
  236.     protected Ellipse2D() {
  237.     }
  238.  
  239.     /**
  240.      * Test if a given Point is inside the boundary of the shape.
  241.      */
  242.     public boolean contains(double x, double y) {
  243.     // Normalize the coordinates compared to the ellipse
  244.     // having a center at 0,0 and a radius of 0.5.
  245.     double ellw = getWidth();
  246.     if (ellw <= 0.0) {
  247.         return false;
  248.     }
  249.     double normx = (x - getX()) / ellw - 0.5;
  250.     double ellh = getHeight();
  251.     if (ellh <= 0.0) {
  252.         return false;
  253.     }
  254.     double normy = (y - getY()) / ellh - 0.5;
  255.     return (normx * normx + normy * normy) < 0.25;
  256.     }
  257.  
  258.     /**
  259.      * Test if the interior of the Shape intersects the interior of a given
  260.      * Rectangle.
  261.      */
  262.     public boolean intersects(double x, double y, double w, double h) {
  263.     if (w <= 0.0 || h <= 0.0) {
  264.         return false;
  265.     }
  266.     // Normalize the rectangular coordinates compared to the ellipse
  267.     // having a center at 0,0 and a radius of 0.5.
  268.     double ellw = getWidth();
  269.     if (ellw <= 0.0) {
  270.         return false;
  271.     }
  272.     double normx0 = (x - getX()) / ellw - 0.5;
  273.     double normx1 = normx0 + w / ellw;
  274.     double ellh = getHeight();
  275.     if (ellh <= 0.0) {
  276.         return false;
  277.     }
  278.     double normy0 = (y - getY()) / ellh - 0.5;
  279.     double normy1 = normy0 + h / ellh;
  280.     // find nearest x (left edge, right edge, 0.0)
  281.     // find nearest y (top edge, bottom edge, 0.0)
  282.     // if nearest x,y is inside circle of radius 0.5, then intersects
  283.     double nearx, neary;
  284.     if (normx0 > 0.0) {
  285.         // center to left of X extents
  286.         nearx = normx0;
  287.     } else if (normx1 < 0.0) {
  288.         // center to right of X extents
  289.         nearx = normx1;
  290.     } else {
  291.         nearx = 0.0;
  292.     }
  293.     if (normy0 > 0.0) {
  294.         // center above Y extents
  295.         neary = normy0;
  296.     } else if (normy1 < 0.0) {
  297.         // center below Y extents
  298.         neary = normy1;
  299.     } else {
  300.         neary = 0.0;
  301.     }
  302.     return (nearx * nearx + neary * neary) < 0.25;
  303.     }
  304.  
  305.     /**
  306.      * Test if the interior of the Shape entirely contains the given
  307.      * Rectangle.
  308.      */
  309.     public boolean contains(double x, double y, double w, double h) {
  310.     return (contains(x, y) &&
  311.         contains(x + w, y) &&
  312.         contains(x, y + h) &&
  313.         contains(x + w, y + h));
  314.     }
  315.  
  316.     /**
  317.      * Return an iteration object that defines the boundary of the
  318.      * shape.
  319.      */
  320.     public PathIterator getPathIterator(AffineTransform at) {
  321.     return new EllipseIterator(this, at);
  322.     }
  323. }
  324.