KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)RectangularShape.java 1.18 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 import java.awt.Shape JavaDoc;
11 import java.awt.Rectangle JavaDoc;
12
13 /**
14  * <code>RectangularShape</code> is the base class for a number of
15  * {@link Shape} objects whose geometry is defined by a rectangular frame.
16  * This class does not directly specify any specific geometry by
17  * itself, but merely provides manipulation methods inherited by
18  * a whole category of <code>Shape</code> objects.
19  * The manipulation methods provided by this class can be used to
20  * query and modify the rectangular frame, which provides a reference
21  * for the subclasses to define their geometry.
22  *
23  * @version 1.18, 12/19/03
24  * @author Jim Graham
25  */

26 public abstract class RectangularShape implements Shape JavaDoc, Cloneable JavaDoc {
27     /**
28      * This is an abstract class that cannot be instantiated directly.
29      *
30      * @see Arc2D
31      * @see Ellipse2D
32      * @see Rectangle2D
33      * @see RoundRectangle2D
34      */

35     protected RectangularShape() {
36     }
37
38     /**
39      * Returns the X coordinate of the upper left corner of
40      * the framing rectangle in <code>double</code> precision.
41      * @return the x coordinate of the upper left corner of
42      * the framing rectangle.
43      */

44     public abstract double getX();
45
46     /**
47      * Returns the Y coordinate of the upper left corner of
48      * the framing rectangle in <code>double</code> precision.
49      * @return the y coordinate of the upper left corner of
50      * the framing rectangle.
51      */

52     public abstract double getY();
53
54     /**
55      * Returns the width of the framing rectangle in
56      * <code>double</code> precision.
57      * @return the width of the framing rectangle.
58      */

59     public abstract double getWidth();
60
61     /**
62      * Returns the height of the framing rectangle
63      * in <code>double</code> precision.
64      * @return the height of the framing rectangle.
65      */

66     public abstract double getHeight();
67
68     /**
69      * Returns the smallest X coordinate of the framing
70      * rectangle of the <code>Shape</code> in <code>double</code>
71      * precision.
72      * @return the smallest x coordinate of the framing
73      * rectangle of the <code>Shape</code>.
74      */

75     public double getMinX() {
76     return getX();
77     }
78
79     /**
80      * Returns the smallest Y coordinate of the framing
81      * rectangle of the <code>Shape</code> in <code>double</code>
82      * precision.
83      * @return the smallest y coordinate of the framing
84      * rectangle of the <code>Shape</code>.
85      */

86     public double getMinY() {
87     return getY();
88     }
89
90     /**
91      * Returns the largest X coordinate of the framing
92      * rectangle of the <code>Shape</code> in <code>double</code>
93      * precision.
94      * @return the largest x coordinate of the framing
95      * rectangle of the <code>Shape</code>.
96      */

97     public double getMaxX() {
98     return getX() + getWidth();
99     }
100
101     /**
102      * Returns the largest Y coordinate of the framing
103      * rectangle of the <code>Shape</code> in <code>double</code>
104      * precision.
105      * @return the largest y coordinate of the framing
106      * rectangle of the <code>Shape</code>.
107      */

108     public double getMaxY() {
109     return getY() + getHeight();
110     }
111
112     /**
113      * Returns the X coordinate of the center of the framing
114      * rectangle of the <code>Shape</code> in <code>double</code>
115      * precision.
116      * @return the x coordinate of the framing rectangle
117      * of the <code>Shape</code> object's center.
118      */

119     public double getCenterX() {
120     return getX() + getWidth() / 2.0;
121     }
122
123     /**
124      * Returns the Y coordinate of the center of the framing
125      * rectangle of the <code>Shape</code> in <code>double</code>
126      * precision.
127      * @return the y coordinate of the framing rectangle
128      * of the <code>Shape</code> object's center.
129      */

130     public double getCenterY() {
131     return getY() + getHeight() / 2.0;
132     }
133
134     /**
135      * Returns the framing {@link Rectangle2D}
136      * that defines the overall shape of this object.
137      * @return a <code>Rectangle2D</code>, specified in
138      * <code>double</code> coordinates.
139      * @see #setFrame(double, double, double, double)
140      * @see #setFrame(Point2D, Dimension2D)
141      * @see #setFrame(Rectangle2D)
142      */

143     public Rectangle2D JavaDoc getFrame() {
144     return new Rectangle2D.Double JavaDoc(getX(), getY(), getWidth(), getHeight());
145     }
146
147     /**
148      * Determines whether the <code>RectangularShape</code> is empty.
149      * When the <code>RectangularShape</code> is empty, it encloses no
150      * area.
151      * @return <code>true</code> if the <code>RectangularShape</code> is empty;
152      * <code>false</code> otherwise.
153      */

154     public abstract boolean isEmpty();
155
156     /**
157      * Sets the location and size of the framing rectangle of this
158      * <code>Shape</code> to the specified rectangular values.
159      * The framing rectangle is used by the subclasses of
160      * <code>RectangularShape</code> to define their geometry.
161      * @param x,&nbsp;y the coordinates of the upper-left corner of the
162      * specified rectangular shape
163      * @param w the width of the specified rectangular shape
164      * @param h the height of the specified rectangular shape
165      * @see #getFrame
166      */

167     public abstract void setFrame(double x, double y, double w, double h);
168
169     /**
170      * Sets the location and size of the framing rectangle of this
171      * <code>Shape</code> to the specified {@link Point2D} and
172      * {@link Dimension2D}, respectively. The framing rectangle is used
173      * by the subclasses of <code>RectangularShape</code> to define
174      * their geometry.
175      * @param loc the specified <code>Point2D</code>
176      * @param size the specified <code>Dimension2D</code>
177      * @see #getFrame
178      */

179     public void setFrame(Point2D JavaDoc loc, Dimension2D JavaDoc size) {
180     setFrame(loc.getX(), loc.getY(), size.getWidth(), size.getHeight());
181     }
182
183     /**
184      * Sets the framing rectangle of this <code>Shape</code> to
185      * be the specified <code>Rectangle2D</code>. The framing rectangle is
186      * used by the subclasses of <code>RectangularShape</code> to define
187      * their geometry.
188      * @param r the specified <code>Rectangle2D</code>
189      * @see #getFrame
190      */

191     public void setFrame(Rectangle2D JavaDoc r) {
192     setFrame(r.getX(), r.getY(), r.getWidth(), r.getHeight());
193     }
194
195     /**
196      * Sets the diagonal of the framing rectangle of this <code>Shape</code>
197      * based on the two specified coordinates. The framing rectangle is
198      * used by the subclasses of <code>RectangularShape</code> to define
199      * their geometry.
200      * @param x1,&nbsp;y1 the first specified coordinates
201      * @param x2,&nbsp;y2 the second specified coordinates
202      */

203     public void setFrameFromDiagonal(double x1, double y1,
204                      double x2, double y2) {
205     if (x2 < x1) {
206         double t = x1;
207         x1 = x2;
208         x2 = t;
209     }
210     if (y2 < y1) {
211         double t = y1;
212         y1 = y2;
213         y2 = t;
214     }
215     setFrame(x1, y1, x2 - x1, y2 - y1);
216     }
217
218     /**
219      * Sets the diagonal of the framing rectangle of this <code>Shape</code>
220      * based on two specified <code>Point2D</code> objects. The framing
221      * rectangle is used by the subclasses of <code>RectangularShape</code>
222      * to define their geometry.
223      * @param p1,&nbsp;p2 the two specified <code>Point2D</code> objects
224      */

225     public void setFrameFromDiagonal(Point2D JavaDoc p1, Point2D JavaDoc p2) {
226     setFrameFromDiagonal(p1.getX(), p1.getY(), p2.getX(), p2.getY());
227     }
228
229     /**
230      * Sets the framing rectangle of this <code>Shape</code>
231      * based on the specified center point coordinates and corner point
232      * coordinates. The framing rectangle is used by the subclasses of
233      * <code>RectangularShape</code> to define their geometry.
234      * @param centerX,&nbsp;centerY the center point coordinates
235      * @param cornerX,&nbsp;cornerY the corner point coordinates
236      */

237     public void setFrameFromCenter(double centerX, double centerY,
238                    double cornerX, double cornerY) {
239     double halfW = Math.abs(cornerX - centerX);
240     double halfH = Math.abs(cornerY - centerY);
241     setFrame(centerX - halfW, centerY - halfH, halfW * 2.0, halfH * 2.0);
242     }
243
244     /**
245      * Sets the framing rectangle of this <code>Shape</code> based on a
246      * specified center <code>Point2D</code> and corner
247      * <code>Point2D</code>. The framing rectangle is used by the subclasses
248      * of <code>RectangularShape</code> to define their geometry.
249      * @param center the specified center <code>Point2D</code>
250      * @param corner the specified corner <code>Point2D</code>
251      */

252     public void setFrameFromCenter(Point2D JavaDoc center, Point2D JavaDoc corner) {
253     setFrameFromCenter(center.getX(), center.getY(),
254                corner.getX(), corner.getY());
255     }
256
257     /**
258      * Tests if a specified <code>Point2D</code> is inside the boundary
259      * of the <code>Shape</code>.
260      * @param p the specified <code>Point2D</code>
261      * @return <code>true</code> if the <code>Point2D</code> is inside the
262      * <code>Shape</code> object's boundary;
263      * <code>false</code> otherwise.
264      */

265     public boolean contains(Point2D JavaDoc p) {
266     return contains(p.getX(), p.getY());
267     }
268
269     /**
270      * Tests if the interior of the<code>Shape</code> intersects the
271      * interior of a specified <code>Rectangle2D</code>.
272      * @param r the specified <code>Rectangle2D</code>
273      * @return <code>true</code> if the <code>Shape</code> and the
274      * specified <code>Rectangle2D</code> intersect each other;
275      * <code>false</code> otherwise.
276      */

277     public boolean intersects(Rectangle2D JavaDoc r) {
278     return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
279     }
280
281     /**
282      * Tests if the interior of the <code>Shape</code> entirely contains the
283      * specified <code>Rectangle2D</code>.
284      * @param r the specified <code>Rectangle2D</code>
285      * @return <code>true</code> if the <code>Shape</code> entirely contains
286      * the specified <code>Rectangle2D</code>;
287      * <code>false</code> otherwise.
288      */

289     public boolean contains(Rectangle2D JavaDoc r) {
290     return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
291     }
292
293     /**
294      * Returns the bounding box of the <code>Shape</code>.
295      * @return a {@link Rectangle} object that bounds the
296      * <code>Shape</code>.
297      */

298     public Rectangle JavaDoc getBounds() {
299     double width = getWidth();
300     double height = getHeight();
301     if (width < 0 || height < 0) {
302         return new Rectangle JavaDoc();
303     }
304     double x = getX();
305     double y = getY();
306     double x1 = Math.floor(x);
307     double y1 = Math.floor(y);
308     double x2 = Math.ceil(x + width);
309     double y2 = Math.ceil(y + height);
310     return new Rectangle JavaDoc((int) x1, (int) y1,
311                       (int) (x2 - x1), (int) (y2 - y1));
312     }
313
314     /**
315      * Returns an iterator object that iterates along the
316      * <code>Shape</code> object's boundary and provides access to a
317      * flattened view of the outline of the <code>Shape</code>
318      * object's geometry.
319      * <p>
320      * Only SEG_MOVETO, SEG_LINETO, and SEG_CLOSE point types will
321      * be returned by the iterator.
322      * <p>
323      * The amount of subdivision of the curved segments is controlled
324      * by the <code>flatness</code> parameter, which specifies the
325      * maximum distance that any point on the unflattened transformed
326      * curve can deviate from the returned flattened path segments.
327      * An optional {@link AffineTransform} can
328      * be specified so that the coordinates returned in the iteration are
329      * transformed accordingly.
330      * @param at an optional <code>AffineTransform</code> to be applied to the
331      * coordinates as they are returned in the iteration,
332      * or <code>null</code> if untransformed coordinates are desired.
333      * @param flatness the maximum distance that the line segments used to
334      * approximate the curved segments are allowed to deviate
335      * from any point on the original curve
336      * @return a <code>PathIterator</code> object that provides access to
337      * the <code>Shape</code> object's flattened geometry.
338      */

339     public PathIterator JavaDoc getPathIterator(AffineTransform JavaDoc at, double flatness) {
340     return new FlatteningPathIterator JavaDoc(getPathIterator(at), flatness);
341     }
342
343     /**
344      * Creates a new object of the same class and with the same
345      * contents as this object.
346      * @return a clone of this instance.
347      * @exception OutOfMemoryError if there is not enough memory.
348      * @see java.lang.Cloneable
349      * @since 1.2
350      */

351     public Object JavaDoc clone() {
352     try {
353         return super.clone();
354     } catch (CloneNotSupportedException JavaDoc e) {
355         // this shouldn't happen, since we are Cloneable
356
throw new InternalError JavaDoc();
357     }
358     }
359 }
360
Popular Tags