001 /*
002 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.awt.geom;
027
028 import java.awt.Shape;
029 import java.awt.Rectangle;
030
031 /**
032 * <code>RectangularShape</code> is the base class for a number of
033 * {@link Shape} objects whose geometry is defined by a rectangular frame.
034 * This class does not directly specify any specific geometry by
035 * itself, but merely provides manipulation methods inherited by
036 * a whole category of <code>Shape</code> objects.
037 * The manipulation methods provided by this class can be used to
038 * query and modify the rectangular frame, which provides a reference
039 * for the subclasses to define their geometry.
040 *
041 * @version 1.26, 05/05/07
042 * @author Jim Graham
043 * @since 1.2
044 */
045 public abstract class RectangularShape implements Shape, Cloneable {
046
047 /**
048 * This is an abstract class that cannot be instantiated directly.
049 *
050 * @see Arc2D
051 * @see Ellipse2D
052 * @see Rectangle2D
053 * @see RoundRectangle2D
054 * @since 1.2
055 */
056 protected RectangularShape() {
057 }
058
059 /**
060 * Returns the X coordinate of the upper-left corner of
061 * the framing rectangle in <code>double</code> precision.
062 * @return the X coordinate of the upper-left corner of
063 * the framing rectangle.
064 * @since 1.2
065 */
066 public abstract double getX();
067
068 /**
069 * Returns the Y coordinate of the upper-left corner of
070 * the framing rectangle in <code>double</code> precision.
071 * @return the Y coordinate of the upper-left corner of
072 * the framing rectangle.
073 * @since 1.2
074 */
075 public abstract double getY();
076
077 /**
078 * Returns the width of the framing rectangle in
079 * <code>double</code> precision.
080 * @return the width of the framing rectangle.
081 * @since 1.2
082 */
083 public abstract double getWidth();
084
085 /**
086 * Returns the height of the framing rectangle
087 * in <code>double</code> precision.
088 * @return the height of the framing rectangle.
089 * @since 1.2
090 */
091 public abstract double getHeight();
092
093 /**
094 * Returns the smallest X coordinate of the framing
095 * rectangle of the <code>Shape</code> in <code>double</code>
096 * precision.
097 * @return the smallest X coordinate of the framing
098 * rectangle of the <code>Shape</code>.
099 * @since 1.2
100 */
101 public double getMinX() {
102 return getX();
103 }
104
105 /**
106 * Returns the smallest Y coordinate of the framing
107 * rectangle of the <code>Shape</code> in <code>double</code>
108 * precision.
109 * @return the smallest Y coordinate of the framing
110 * rectangle of the <code>Shape</code>.
111 * @since 1.2
112 */
113 public double getMinY() {
114 return getY();
115 }
116
117 /**
118 * Returns the largest X coordinate of the framing
119 * rectangle of the <code>Shape</code> in <code>double</code>
120 * precision.
121 * @return the largest X coordinate of the framing
122 * rectangle of the <code>Shape</code>.
123 * @since 1.2
124 */
125 public double getMaxX() {
126 return getX() + getWidth();
127 }
128
129 /**
130 * Returns the largest Y coordinate of the framing
131 * rectangle of the <code>Shape</code> in <code>double</code>
132 * precision.
133 * @return the largest Y coordinate of the framing
134 * rectangle of the <code>Shape</code>.
135 * @since 1.2
136 */
137 public double getMaxY() {
138 return getY() + getHeight();
139 }
140
141 /**
142 * Returns the X coordinate of the center of the framing
143 * rectangle of the <code>Shape</code> in <code>double</code>
144 * precision.
145 * @return the X coordinate of the center of the framing rectangle
146 * of the <code>Shape</code>.
147 * @since 1.2
148 */
149 public double getCenterX() {
150 return getX() + getWidth() / 2.0;
151 }
152
153 /**
154 * Returns the Y coordinate of the center of the framing
155 * rectangle of the <code>Shape</code> in <code>double</code>
156 * precision.
157 * @return the Y coordinate of the center of the framing rectangle
158 * of the <code>Shape</code>.
159 * @since 1.2
160 */
161 public double getCenterY() {
162 return getY() + getHeight() / 2.0;
163 }
164
165 /**
166 * Returns the framing {@link Rectangle2D}
167 * that defines the overall shape of this object.
168 * @return a <code>Rectangle2D</code>, specified in
169 * <code>double</code> coordinates.
170 * @see #setFrame(double, double, double, double)
171 * @see #setFrame(Point2D, Dimension2D)
172 * @see #setFrame(Rectangle2D)
173 * @since 1.2
174 */
175 public Rectangle2D getFrame() {
176 return new Rectangle2D.Double(getX(), getY(), getWidth(),
177 getHeight());
178 }
179
180 /**
181 * Determines whether the <code>RectangularShape</code> is empty.
182 * When the <code>RectangularShape</code> is empty, it encloses no
183 * area.
184 * @return <code>true</code> if the <code>RectangularShape</code> is empty;
185 * <code>false</code> otherwise.
186 * @since 1.2
187 */
188 public abstract boolean isEmpty();
189
190 /**
191 * Sets the location and size of the framing rectangle of this
192 * <code>Shape</code> to the specified rectangular values.
193 *
194 * @param x the X coordinate of the upper-left corner of the
195 * specified rectangular shape
196 * @param y the Y coordinate of the upper-left corner of the
197 * specified rectangular shape
198 * @param w the width of the specified rectangular shape
199 * @param h the height of the specified rectangular shape
200 * @see #getFrame
201 * @since 1.2
202 */
203 public abstract void setFrame(double x, double y, double w, double h);
204
205 /**
206 * Sets the location and size of the framing rectangle of this
207 * <code>Shape</code> to the specified {@link Point2D} and
208 * {@link Dimension2D}, respectively. The framing rectangle is used
209 * by the subclasses of <code>RectangularShape</code> to define
210 * their geometry.
211 * @param loc the specified <code>Point2D</code>
212 * @param size the specified <code>Dimension2D</code>
213 * @see #getFrame
214 * @since 1.2
215 */
216 public void setFrame(Point2D loc, Dimension2D size) {
217 setFrame(loc.getX(), loc.getY(), size.getWidth(), size
218 .getHeight());
219 }
220
221 /**
222 * Sets the framing rectangle of this <code>Shape</code> to
223 * be the specified <code>Rectangle2D</code>. The framing rectangle is
224 * used by the subclasses of <code>RectangularShape</code> to define
225 * their geometry.
226 * @param r the specified <code>Rectangle2D</code>
227 * @see #getFrame
228 * @since 1.2
229 */
230 public void setFrame(Rectangle2D r) {
231 setFrame(r.getX(), r.getY(), r.getWidth(), r.getHeight());
232 }
233
234 /**
235 * Sets the diagonal of the framing rectangle of this <code>Shape</code>
236 * based on the two specified coordinates. The framing rectangle is
237 * used by the subclasses of <code>RectangularShape</code> to define
238 * their geometry.
239 *
240 * @param x1 the X coordinate of the start point of the specified diagonal
241 * @param y1 the Y coordinate of the start point of the specified diagonal
242 * @param x2 the X coordinate of the end point of the specified diagonal
243 * @param y2 the Y coordinate of the end point of the specified diagonal
244 * @since 1.2
245 */
246 public void setFrameFromDiagonal(double x1, double y1, double x2,
247 double y2) {
248 if (x2 < x1) {
249 double t = x1;
250 x1 = x2;
251 x2 = t;
252 }
253 if (y2 < y1) {
254 double t = y1;
255 y1 = y2;
256 y2 = t;
257 }
258 setFrame(x1, y1, x2 - x1, y2 - y1);
259 }
260
261 /**
262 * Sets the diagonal of the framing rectangle of this <code>Shape</code>
263 * based on two specified <code>Point2D</code> objects. The framing
264 * rectangle is used by the subclasses of <code>RectangularShape</code>
265 * to define their geometry.
266 *
267 * @param p1 the start <code>Point2D</code> of the specified diagonal
268 * @param p2 the end <code>Point2D</code> of the specified diagonal
269 * @since 1.2
270 */
271 public void setFrameFromDiagonal(Point2D p1, Point2D p2) {
272 setFrameFromDiagonal(p1.getX(), p1.getY(), p2.getX(), p2.getY());
273 }
274
275 /**
276 * Sets the framing rectangle of this <code>Shape</code>
277 * based on the specified center point coordinates and corner point
278 * coordinates. The framing rectangle is used by the subclasses of
279 * <code>RectangularShape</code> to define their geometry.
280 *
281 * @param centerX the X coordinate of the specified center point
282 * @param centerY the Y coordinate of the specified center point
283 * @param cornerX the X coordinate of the specified corner point
284 * @param cornerY the Y coordinate of the specified corner point
285 * @since 1.2
286 */
287 public void setFrameFromCenter(double centerX, double centerY,
288 double cornerX, double cornerY) {
289 double halfW = Math.abs(cornerX - centerX);
290 double halfH = Math.abs(cornerY - centerY);
291 setFrame(centerX - halfW, centerY - halfH, halfW * 2.0,
292 halfH * 2.0);
293 }
294
295 /**
296 * Sets the framing rectangle of this <code>Shape</code> based on a
297 * specified center <code>Point2D</code> and corner
298 * <code>Point2D</code>. The framing rectangle is used by the subclasses
299 * of <code>RectangularShape</code> to define their geometry.
300 * @param center the specified center <code>Point2D</code>
301 * @param corner the specified corner <code>Point2D</code>
302 * @since 1.2
303 */
304 public void setFrameFromCenter(Point2D center, Point2D corner) {
305 setFrameFromCenter(center.getX(), center.getY(), corner.getX(),
306 corner.getY());
307 }
308
309 /**
310 * {@inheritDoc}
311 * @since 1.2
312 */
313 public boolean contains(Point2D p) {
314 return contains(p.getX(), p.getY());
315 }
316
317 /**
318 * {@inheritDoc}
319 * @since 1.2
320 */
321 public boolean intersects(Rectangle2D r) {
322 return intersects(r.getX(), r.getY(), r.getWidth(), r
323 .getHeight());
324 }
325
326 /**
327 * {@inheritDoc}
328 * @since 1.2
329 */
330 public boolean contains(Rectangle2D r) {
331 return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
332 }
333
334 /**
335 * {@inheritDoc}
336 * @since 1.2
337 */
338 public Rectangle getBounds() {
339 double width = getWidth();
340 double height = getHeight();
341 if (width < 0 || height < 0) {
342 return new Rectangle();
343 }
344 double x = getX();
345 double y = getY();
346 double x1 = Math.floor(x);
347 double y1 = Math.floor(y);
348 double x2 = Math.ceil(x + width);
349 double y2 = Math.ceil(y + height);
350 return new Rectangle((int) x1, (int) y1, (int) (x2 - x1),
351 (int) (y2 - y1));
352 }
353
354 /**
355 * Returns an iterator object that iterates along the
356 * <code>Shape</code> object's boundary and provides access to a
357 * flattened view of the outline of the <code>Shape</code>
358 * object's geometry.
359 * <p>
360 * Only SEG_MOVETO, SEG_LINETO, and SEG_CLOSE point types will
361 * be returned by the iterator.
362 * <p>
363 * The amount of subdivision of the curved segments is controlled
364 * by the <code>flatness</code> parameter, which specifies the
365 * maximum distance that any point on the unflattened transformed
366 * curve can deviate from the returned flattened path segments.
367 * An optional {@link AffineTransform} can
368 * be specified so that the coordinates returned in the iteration are
369 * transformed accordingly.
370 * @param at an optional <code>AffineTransform</code> to be applied to the
371 * coordinates as they are returned in the iteration,
372 * or <code>null</code> if untransformed coordinates are desired.
373 * @param flatness the maximum distance that the line segments used to
374 * approximate the curved segments are allowed to deviate
375 * from any point on the original curve
376 * @return a <code>PathIterator</code> object that provides access to
377 * the <code>Shape</code> object's flattened geometry.
378 * @since 1.2
379 */
380 public PathIterator getPathIterator(AffineTransform at,
381 double flatness) {
382 return new FlatteningPathIterator(getPathIterator(at), flatness);
383 }
384
385 /**
386 * Creates a new object of the same class and with the same
387 * contents as this object.
388 * @return a clone of this instance.
389 * @exception OutOfMemoryError if there is not enough memory.
390 * @see java.lang.Cloneable
391 * @since 1.2
392 */
393 public Object clone() {
394 try {
395 return super .clone();
396 } catch (CloneNotSupportedException e) {
397 // this shouldn't happen, since we are Cloneable
398 throw new InternalError();
399 }
400 }
401 }
|