KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > geom > Ellipse2D


1 /*
2  * @(#)Ellipse2D.java 1.16 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt.geom;
9
10 /**
11  * The <code>Ellipse2D</code> class describes an ellipse that is defined
12  * by a bounding rectangle.
13  * <p>
14  * This class is only the abstract superclass for all objects which
15  * store a 2D ellipse.
16  * The actual storage representation of the coordinates is left to
17  * the subclass.
18  *
19  * @version 1.16, 12/19/03
20  * @author Jim Graham
21  */

22 public abstract class Ellipse2D extends RectangularShape JavaDoc {
23     /**
24      * The <code>Float</code> class defines an ellipse specified
25      * in <code>float</code> precision.
26      */

27     public static class Float extends Ellipse2D JavaDoc {
28     /**
29      * The x coordinate of the upper left corner of this
30          * <code>Ellipse2D</code>.
31      */

32     public float x;
33
34     /**
35      * The y coordinate of the upper left corner of this
36          * <code>Ellipse2D</code>.
37      */

38     public float y;
39
40     /**
41      * The overall width of this <code>Ellipse2D</code>.
42      */

43     public float width;
44
45     /**
46      * The overall height of this <code>Ellipse2D</code>.
47      */

48     public float height;
49
50     /**
51      * Constructs a new <code>Ellipse2D</code>, initialized to
52          * location (0,&nbsp;0) and size (0,&nbsp;0).
53      */

54     public Float() {
55     }
56
57     /**
58      * Constructs and initializes an <code>Ellipse2D</code> from the
59          * specified coordinates.
60      * @param x,&nbsp;y the coordinates of the bounding rectangle
61      * @param w the width of the bounding rectangle
62      * @param h the height of the bounding rectangle
63      */

64     public Float(float x, float y, float w, float h) {
65         setFrame(x, y, w, h);
66     }
67
68     /**
69      * Returns the X coordinate of the upper left corner of this
70          * <code>Ellipse2D</code> in <code>double</code> precision.
71          * @return the X coordinate of the upper left corner of the
72          * bounding rectangle of this <code>Ellipse2D</code>.
73      */

74     public double getX() {
75         return (double) x;
76     }
77
78     /**
79      * Returns the Y coordinate of the upper left corner of this
80          * <code>Ellipse2D</code> in <code>double</code> precision.
81          * @return the Y coordinate of the upper left corner of the
82          * bounding rectangle of this <code>Ellipse2D</code>.
83      */

84     public double getY() {
85         return (double) y;
86     }
87
88     /**
89      * Returns the overall width of this <code>Ellipse2D</code> in
90          * <code>double</code> precision.
91          * @return the width of this <code>Ellipse2D</code>.
92      */

93     public double getWidth() {
94         return (double) width;
95     }
96
97     /**
98      * Returns the overall height of this <code>Ellipse2D</code> in
99          * <code>double</code> precision.
100          * @return the height of this <code>Ellipse2D</code>.
101      */

102     public double getHeight() {
103         return (double) height;
104     }
105
106     /**
107      * Determines whether or not the bounding box of this
108          * <code>Ellipse2D</code> is empty.
109          * @return <code>true</code> if the bounding rectangle of this
110          * <code>Ellipse2D</code> is empty; <code>false</code> otherwise.
111      */

112     public boolean isEmpty() {
113         return (width <= 0.0 || height <= 0.0);
114     }
115
116     /**
117      * Sets the location and size of this <code>Ellipse2D</code> to
118          * the specified <code>float</code> values.
119          * @param x,&nbsp;y the specified coordinates to which to set
120          * the location of the bounding box of this <code>Ellipse2D</code>
121          * @param w the specified width to which to set the width of
122          * this <code>Ellipse2D</code>
123          * @param h the specified height to which to set the height of
124          * the <code>Ellipse2D</code>
125      */

126     public void setFrame(float x, float y, float w, float h) {
127         this.x = x;
128         this.y = y;
129         this.width = w;
130         this.height = h;
131     }
132
133     /**
134      * Sets the location and size of this <code>Ellipse2D</code> to
135          * the specified <code>double</code> values.
136          * @param x,&nbsp;y the specified coordinates to which to set
137          * the location of the bounding box of this <code>Ellipse2D</code>
138          * @param w the specified width to which to set the width of
139          * this <code>Ellipse2D</code>
140          * @param h the specified height to which to set the height of
141          * this <code>Ellipse2D</code>
142      */

143     public void setFrame(double x, double y, double w, double h) {
144         this.x = (float) x;
145         this.y = (float) y;
146         this.width = (float) w;
147         this.height = (float) h;
148     }
149
150     /**
151      * Returns the high precision bounding box of this
152          * <code>Ellipse2D</code>.
153          * @return a {@link Rectangle2D} that is the bounding box
154          * of this <code>Ellipse2D</code>.
155      */

156     public Rectangle2D JavaDoc getBounds2D() {
157         return new Rectangle2D.Float JavaDoc(x, y, width, height);
158     }
159     }
160
161     /**
162      * The <code>Double</code> class defines an ellipse specified
163      * in <code>double</code> precision.
164      */

165     public static class Double extends Ellipse2D JavaDoc {
166     /**
167      * The x coordinate of the upper left corner of this
168          * <code>Ellipse2D</code>.
169      */

170     public double x;
171
172     /**
173      * The y coordinate of the upper left corner of this
174          * <code>Ellipse2D</code>.
175      */

176     public double y;
177
178     /**
179      * The overall width of this <code>Ellipse2D</code>.
180      */

181     public double width;
182
183     /**
184      * The overall height of the <code>Ellipse2D</code>.
185      */

186     public double height;
187
188     /**
189      * Constructs a new <code>Ellipse2D</code>, initialized to
190          * location (0,&nbsp;0) and size (0,&nbsp;0).
191      */

192     public Double() {
193     }
194
195     /**
196      * Constructs and initializes an <code>Ellipse2D</code> from the
197          * specified coordinates.
198      * @param x,&nbsp;y the coordinates of the bounding rectangle
199      * @param w the width of the rectangle
200      * @param h the height of the rectangle
201      */

202     public Double(double x, double y, double w, double h) {
203         setFrame(x, y, w, h);
204     }
205
206     /**
207      * Returns the X coordinate of the upper left corner of this
208          * <code>Ellipse2D</code> in <code>double</code> precision.
209          * @return the X coordinate of the upper left corner of
210          * the bounding box of this <code>Ellipse2D</code>.
211      */

212     public double getX() {
213         return x;
214     }
215
216     /**
217      * Returns the Y coordinate of the upper left corner of this
218          * <code>Ellipse2D</code> in <code>double</code> precision.
219          * @return the Y coordinate of the upper left corner of
220          * the bounding box of this <code>Ellipse2D</code>.
221      */

222     public double getY() {
223         return y;
224     }
225
226     /**
227      * Returns the overall width of this <code>Ellipse2D</code>
228          * in <code>double</code> precision.
229          * @return the width of this <code>Ellipse2D</code>.
230      */

231     public double getWidth() {
232         return width;
233     }
234
235     /**
236      * Returns the overall height of this <code>Ellipse2D</code>
237          * in <code>double</code> precision.
238          * @return the height of this <code>Ellipse2D</code>.
239      */

240     public double getHeight() {
241         return height;
242     }
243
244     /**
245      * Determines whether or not the bounding box of this
246          * <code>Ellipse2D</code> is empty.
247          * @return <code>true</code> if the bounding box of this
248          * <code>Ellipse2D</code> is empty;
249          * <code>false</code> otherwise.
250      */

251     public boolean isEmpty() {
252         return (width <= 0.0 || height <= 0.0);
253     }
254
255     /**
256      * Sets the location and size of this <code>Ellipse2D</code>
257          * to the specified <code>double</code> values.
258          * @param x,&nbsp;y the specified coordinates to which to set
259          * the location of the bounding box of this <code>Ellipse2D</code>
260          * @param w the width to which to set the width of this
261          * <code>Ellipse2D</code>
262          * @param h the height to which to set the height of this
263          * <code>Ellipse2D</code>
264      */

265     public void setFrame(double x, double y, double w, double h) {
266         this.x = x;
267         this.y = y;
268         this.width = w;
269         this.height = h;
270     }
271
272     /**
273      * Returns the high precision bounding box of this
274          * <code>Ellipse2D</code>.
275          * @return a <code>Rectangle2D</code> that is the bounding
276          * box of this <code>Ellipse2D</code>.
277      */

278     public Rectangle2D JavaDoc getBounds2D() {
279         return new Rectangle2D.Double JavaDoc(x, y, width, height);
280     }
281     }
282
283     /**
284      * This is an abstract class that cannot be instantiated directly.
285      * Type-specific implementation subclasses are available for
286      * instantiation and provide a number of formats for storing
287      * the information necessary to satisfy the various accessor
288      * methods below.
289      *
290      * @see java.awt.geom.Ellipse2D.Float
291      * @see java.awt.geom.Ellipse2D.Double
292      */

293     protected Ellipse2D() {
294     }
295
296     /**
297      * Tests if a specified point is inside the boundary of this
298      * <code>Ellipse2D</code>.
299      * @param x,&nbsp;y the coordinates to test
300      * @return <code>true</code> if the specified point is contained
301      * in this ellipse; <code>false</code> otherwise.
302      */

303     public boolean contains(double x, double y) {
304     // Normalize the coordinates compared to the ellipse
305
// having a center at 0,0 and a radius of 0.5.
306
double ellw = getWidth();
307     if (ellw <= 0.0) {
308         return false;
309     }
310     double normx = (x - getX()) / ellw - 0.5;
311     double ellh = getHeight();
312     if (ellh <= 0.0) {
313         return false;
314     }
315     double normy = (y - getY()) / ellh - 0.5;
316     return (normx * normx + normy * normy) < 0.25;
317     }
318
319     /**
320      * Tests if the interior of this <code>Ellipse2D</code> intersects
321      * the interior of a specified rectangular area.
322      * @param x,&nbsp;y the coordinates of the upper left corner of the
323      * specified rectangular area
324      * @param w the width of the specified rectangular area
325      * @param h the height of the specified rectangluar area
326      * @return <code>true</code> if this <code>Ellipse2D</code> contains
327      * the specified rectangular area; <code>false</code> otherwise.
328      */

329     public boolean intersects(double x, double y, double w, double h) {
330     if (w <= 0.0 || h <= 0.0) {
331         return false;
332     }
333     // Normalize the rectangular coordinates compared to the ellipse
334
// having a center at 0,0 and a radius of 0.5.
335
double ellw = getWidth();
336     if (ellw <= 0.0) {
337         return false;
338     }
339     double normx0 = (x - getX()) / ellw - 0.5;
340     double normx1 = normx0 + w / ellw;
341     double ellh = getHeight();
342     if (ellh <= 0.0) {
343         return false;
344     }
345     double normy0 = (y - getY()) / ellh - 0.5;
346     double normy1 = normy0 + h / ellh;
347     // find nearest x (left edge, right edge, 0.0)
348
// find nearest y (top edge, bottom edge, 0.0)
349
// if nearest x,y is inside circle of radius 0.5, then intersects
350
double nearx, neary;
351     if (normx0 > 0.0) {
352         // center to left of X extents
353
nearx = normx0;
354     } else if (normx1 < 0.0) {
355         // center to right of X extents
356
nearx = normx1;
357     } else {
358         nearx = 0.0;
359     }
360     if (normy0 > 0.0) {
361         // center above Y extents
362
neary = normy0;
363     } else if (normy1 < 0.0) {
364         // center below Y extents
365
neary = normy1;
366     } else {
367         neary = 0.0;
368     }
369     return (nearx * nearx + neary * neary) < 0.25;
370     }
371
372     /**
373      * Tests if the interior of this <code>Ellipse2D</code> entirely
374      * contains the specified rectangular area.
375      * @param x,&nbsp;y the coordinates of the upper left corner of the
376      * specified rectangular area
377      * @param w the width of the specified rectangular area
378      * @param h the height of the specified rectangular area
379      * @return <code>true</code> if this <code>Ellipse2D</code> contains
380      * the specified rectangular area; <code>false</code> otherwise.
381      */

382     public boolean contains(double x, double y, double w, double h) {
383     return (contains(x, y) &&
384         contains(x + w, y) &&
385         contains(x, y + h) &&
386         contains(x + w, y + h));
387     }
388
389     /**
390      * Returns an iteration object that defines the boundary of this
391      * <code>Ellipse2D</code>.
392      * The iterator for this class is multi-threaded safe, which means
393      * that this <code>Ellipse2D</code> class guarantees that
394      * modifications to the geometry of this <code>Ellipse2D</code>
395      * object do not affect any iterations of that geometry that
396      * are already in process.
397      * @param at an optional <code>AffineTransform</code> to be applied to
398      * the coordinates as they are returned in the iteration, or
399      * <code>null</code> if untransformed coordinates are desired
400      * @return the <code>PathIterator</code> object that returns the
401      * geometry of the outline of this <code>Ellipse2D</code>,
402      * one segment at a time.
403      */

404     public PathIterator JavaDoc getPathIterator(AffineTransform JavaDoc at) {
405     return new EllipseIterator JavaDoc(this, at);
406     }
407 }
408
Popular Tags