1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
/*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.awt.geom;
import java.awt.Shape;
import java.awt.Rectangle;
import java.beans.Transient;
/**
* {@code RectangularShape} is the base class for a number of
* {@link Shape} objects whose geometry is defined by a rectangular frame.
* This class does not directly specify any specific geometry by
* itself, but merely provides manipulation methods inherited by
* a whole category of {@code Shape} objects.
* The manipulation methods provided by this class can be used to
* query and modify the rectangular frame, which provides a reference
* for the subclasses to define their geometry.
*
* @author Jim Graham
* @since 1.2
*/
public abstract class RectangularShape implements Shape, Cloneable {
/**
* This is an abstract class that cannot be instantiated directly.
*
* @see Arc2D
* @see Ellipse2D
* @see Rectangle2D
* @see RoundRectangle2D
* @since 1.2
*/
protected RectangularShape() {
}
/**
* Returns the X coordinate of the upper-left corner of
* the framing rectangle in {@code double} precision.
* @return the X coordinate of the upper-left corner of
* the framing rectangle.
* @since 1.2
*/
public abstract double getX();
/**
* Returns the Y coordinate of the upper-left corner of
* the framing rectangle in {@code double} precision.
* @return the Y coordinate of the upper-left corner of
* the framing rectangle.
* @since 1.2
*/
public abstract double getY();
/**
* Returns the width of the framing rectangle in
* {@code double} precision.
* @return the width of the framing rectangle.
* @since 1.2
*/
public abstract double getWidth();
/**
* Returns the height of the framing rectangle
* in {@code double} precision.
* @return the height of the framing rectangle.
* @since 1.2
*/
public abstract double getHeight();
/**
* Returns the smallest X coordinate of the framing
* rectangle of the {@code Shape} in {@code double}
* precision.
* @return the smallest X coordinate of the framing
* rectangle of the {@code Shape}.
* @since 1.2
*/
public double getMinX() {
return getX();
}
/**
* Returns the smallest Y coordinate of the framing
* rectangle of the {@code Shape} in {@code double}
* precision.
* @return the smallest Y coordinate of the framing
* rectangle of the {@code Shape}.
* @since 1.2
*/
public double getMinY() {
return getY();
}
/**
* Returns the largest X coordinate of the framing
* rectangle of the {@code Shape} in {@code double}
* precision.
* @return the largest X coordinate of the framing
* rectangle of the {@code Shape}.
* @since 1.2
*/
public double getMaxX() {
return getX() + getWidth();
}
/**
* Returns the largest Y coordinate of the framing
* rectangle of the {@code Shape} in {@code double}
* precision.
* @return the largest Y coordinate of the framing
* rectangle of the {@code Shape}.
* @since 1.2
*/
public double getMaxY() {
return getY() + getHeight();
}
/**
* Returns the X coordinate of the center of the framing
* rectangle of the {@code Shape} in {@code double}
* precision.
* @return the X coordinate of the center of the framing rectangle
* of the {@code Shape}.
* @since 1.2
*/
public double getCenterX() {
return getX() + getWidth() / 2.0;
}
/**
* Returns the Y coordinate of the center of the framing
* rectangle of the {@code Shape} in {@code double}
* precision.
* @return the Y coordinate of the center of the framing rectangle
* of the {@code Shape}.
* @since 1.2
*/
public double getCenterY() {
return getY() + getHeight() / 2.0;
}
/**
* Returns the framing {@link Rectangle2D}
* that defines the overall shape of this object.
* @return a {@code Rectangle2D}, specified in
* {@code double} coordinates.
* @see #setFrame(double, double, double, double)
* @see #setFrame(Point2D, Dimension2D)
* @see #setFrame(Rectangle2D)
* @since 1.2
*/
@Transient
public Rectangle2D getFrame() {
return new Rectangle2D.Double(getX(), getY(), getWidth(), getHeight());
}
/**
* Determines whether the {@code RectangularShape} is empty.
* When the {@code RectangularShape} is empty, it encloses no
* area.
* @return {@code true} if the {@code RectangularShape} is empty;
* {@code false} otherwise.
* @since 1.2
*/
public abstract boolean isEmpty();
/**
* Sets the location and size of the framing rectangle of this
* {@code Shape} to the specified rectangular values.
*
* @param x the X coordinate of the upper-left corner of the
* specified rectangular shape
* @param y the Y coordinate of the upper-left corner of the
* specified rectangular shape
* @param w the width of the specified rectangular shape
* @param h the height of the specified rectangular shape
* @see #getFrame
* @since 1.2
*/
public abstract void setFrame(double x, double y, double w, double h);
/**
* Sets the location and size of the framing rectangle of this
* {@code Shape} to the specified {@link Point2D} and
* {@link Dimension2D}, respectively. The framing rectangle is used
* by the subclasses of {@code RectangularShape} to define
* their geometry.
* @param loc the specified {@code Point2D}
* @param size the specified {@code Dimension2D}
* @see #getFrame
* @since 1.2
*/
public void setFrame(Point2D loc, Dimension2D size) {
setFrame(loc.getX(), loc.getY(), size.getWidth(), size.getHeight());
}
/**
* Sets the framing rectangle of this {@code Shape} to
* be the specified {@code Rectangle2D}. The framing rectangle is
* used by the subclasses of {@code RectangularShape} to define
* their geometry.
* @param r the specified {@code Rectangle2D}
* @see #getFrame
* @since 1.2
*/
public void setFrame(Rectangle2D r) {
setFrame(r.getX(), r.getY(), r.getWidth(), r.getHeight());
}
/**
* Sets the diagonal of the framing rectangle of this {@code Shape}
* based on the two specified coordinates. The framing rectangle is
* used by the subclasses of {@code RectangularShape} to define
* their geometry.
*
* @param x1 the X coordinate of the start point of the specified diagonal
* @param y1 the Y coordinate of the start point of the specified diagonal
* @param x2 the X coordinate of the end point of the specified diagonal
* @param y2 the Y coordinate of the end point of the specified diagonal
* @since 1.2
*/
public void setFrameFromDiagonal(double x1, double y1,
double x2, double y2) {
if (x2 < x1) {
double t = x1;
x1 = x2;
x2 = t;
}
if (y2 < y1) {
double t = y1;
y1 = y2;
y2 = t;
}
setFrame(x1, y1, x2 - x1, y2 - y1);
}
/**
* Sets the diagonal of the framing rectangle of this {@code Shape}
* based on two specified {@code Point2D} objects. The framing
* rectangle is used by the subclasses of {@code RectangularShape}
* to define their geometry.
*
* @param p1 the start {@code Point2D} of the specified diagonal
* @param p2 the end {@code Point2D} of the specified diagonal
* @since 1.2
*/
public void setFrameFromDiagonal(Point2D p1, Point2D p2) {
setFrameFromDiagonal(p1.getX(), p1.getY(), p2.getX(), p2.getY());
}
/**
* Sets the framing rectangle of this {@code Shape}
* based on the specified center point coordinates and corner point
* coordinates. The framing rectangle is used by the subclasses of
* {@code RectangularShape} to define their geometry.
*
* @param centerX the X coordinate of the specified center point
* @param centerY the Y coordinate of the specified center point
* @param cornerX the X coordinate of the specified corner point
* @param cornerY the Y coordinate of the specified corner point
* @since 1.2
*/
public void setFrameFromCenter(double centerX, double centerY,
double cornerX, double cornerY) {
double halfW = Math.abs(cornerX - centerX);
double halfH = Math.abs(cornerY - centerY);
setFrame(centerX - halfW, centerY - halfH, halfW * 2.0, halfH * 2.0);
}
/**
* Sets the framing rectangle of this {@code Shape} based on a
* specified center {@code Point2D} and corner
* {@code Point2D}. The framing rectangle is used by the subclasses
* of {@code RectangularShape} to define their geometry.
* @param center the specified center {@code Point2D}
* @param corner the specified corner {@code Point2D}
* @since 1.2
*/
public void setFrameFromCenter(Point2D center, Point2D corner) {
setFrameFromCenter(center.getX(), center.getY(),
corner.getX(), corner.getY());
}
/**
* {@inheritDoc}
* @since 1.2
*/
public boolean contains(Point2D p) {
return contains(p.getX(), p.getY());
}
/**
* {@inheritDoc}
* @since 1.2
*/
public boolean intersects(Rectangle2D r) {
return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
}
/**
* {@inheritDoc}
* @since 1.2
*/
public boolean contains(Rectangle2D r) {
return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
}
/**
* {@inheritDoc}
* @since 1.2
*/
public Rectangle getBounds() {
double width = getWidth();
double height = getHeight();
if (width < 0 || height < 0) {
return new Rectangle();
}
double x = getX();
double y = getY();
double x1 = Math.floor(x);
double y1 = Math.floor(y);
double x2 = Math.ceil(x + width);
double y2 = Math.ceil(y + height);
return new Rectangle((int) x1, (int) y1,
(int) (x2 - x1), (int) (y2 - y1));
}
/**
* Returns an iterator object that iterates along the
* {@code Shape} object's boundary and provides access to a
* flattened view of the outline of the {@code Shape}
* object's geometry.
* <p>
* Only SEG_MOVETO, SEG_LINETO, and SEG_CLOSE point types will
* be returned by the iterator.
* <p>
* The amount of subdivision of the curved segments is controlled
* by the {@code flatness} parameter, which specifies the
* maximum distance that any point on the unflattened transformed
* curve can deviate from the returned flattened path segments.
* An optional {@link AffineTransform} can
* be specified so that the coordinates returned in the iteration are
* transformed accordingly.
* @param at an optional {@code AffineTransform} to be applied to the
* coordinates as they are returned in the iteration,
* or {@code null} if untransformed coordinates are desired.
* @param flatness the maximum distance that the line segments used to
* approximate the curved segments are allowed to deviate
* from any point on the original curve
* @return a {@code PathIterator} object that provides access to
* the {@code Shape} object's flattened geometry.
* @since 1.2
*/
public PathIterator getPathIterator(AffineTransform at, double flatness) {
return new FlatteningPathIterator(getPathIterator(at), flatness);
}
/**
* Creates a new object of the same class and with the same
* contents as this object.
* @return a clone of this instance.
* @exception OutOfMemoryError if there is not enough memory.
* @see java.lang.Cloneable
* @since 1.2
*/
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
// this shouldn't happen, since we are Cloneable
throw new InternalError(e);
}
}
}
|