1 | /* RoundRectangle2D.java -- represents a rectangle with rounded corners
|
---|
2 | Copyright (C) 2000, 2002 Free Software Foundation
|
---|
3 |
|
---|
4 | This file is part of GNU Classpath.
|
---|
5 |
|
---|
6 | GNU Classpath is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2, or (at your option)
|
---|
9 | any later version.
|
---|
10 |
|
---|
11 | GNU Classpath is distributed in the hope that it will be useful, but
|
---|
12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
---|
18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 USA.
|
---|
20 |
|
---|
21 | Linking this library statically or dynamically with other modules is
|
---|
22 | making a combined work based on this library. Thus, the terms and
|
---|
23 | conditions of the GNU General Public License cover the whole
|
---|
24 | combination.
|
---|
25 |
|
---|
26 | As a special exception, the copyright holders of this library give you
|
---|
27 | permission to link this library with independent modules to produce an
|
---|
28 | executable, regardless of the license terms of these independent
|
---|
29 | modules, and to copy and distribute the resulting executable under
|
---|
30 | terms of your choice, provided that you also meet, for each linked
|
---|
31 | independent module, the terms and conditions of the license of that
|
---|
32 | module. An independent module is a module which is not derived from
|
---|
33 | or based on this library. If you modify this library, you may extend
|
---|
34 | this exception to your version of the library, but you are not
|
---|
35 | obligated to do so. If you do not wish to do so, delete this
|
---|
36 | exception statement from your version. */
|
---|
37 |
|
---|
38 | package java.awt.geom;
|
---|
39 |
|
---|
40 | /** This class implements a rectangle with rounded corners.
|
---|
41 | * @author Tom Tromey <tromey@cygnus.com>
|
---|
42 | * @date December 3, 2000
|
---|
43 | */
|
---|
44 | public abstract class RoundRectangle2D extends RectangularShape
|
---|
45 | {
|
---|
46 | /** Return the arc height of this round rectangle. */
|
---|
47 | public abstract double getArcHeight();
|
---|
48 |
|
---|
49 | /** Return the arc width of this round rectangle. */
|
---|
50 | public abstract double getArcWidth();
|
---|
51 |
|
---|
52 | /** Set the values of this round rectangle
|
---|
53 | * @param x The x coordinate
|
---|
54 | * @param y The y coordinate
|
---|
55 | * @param w The width
|
---|
56 | * @param h The height
|
---|
57 | * @param arcWidth The arc width
|
---|
58 | * @param arcHeight The arc height
|
---|
59 | */
|
---|
60 | public abstract void setRoundRect(double x, double y, double w, double h,
|
---|
61 | double arcWidth, double arcHeight);
|
---|
62 |
|
---|
63 | /** Create a RoundRectangle2D. This is protected because this class
|
---|
64 | * is abstract and cannot be instantiated.
|
---|
65 | */
|
---|
66 | protected RoundRectangle2D()
|
---|
67 | {
|
---|
68 | }
|
---|
69 |
|
---|
70 | /** Return true if this object contains the specified point.
|
---|
71 | * @param x The x coordinate
|
---|
72 | * @param y The y coordinate
|
---|
73 | */
|
---|
74 | public boolean contains(double x, double y)
|
---|
75 | {
|
---|
76 | double mx = getX();
|
---|
77 | double mw = getWidth();
|
---|
78 | if (x < mx || x >= mx + mw)
|
---|
79 | return false;
|
---|
80 | double my = getY();
|
---|
81 | double mh = getHeight();
|
---|
82 | if (y < my || y >= my + mh)
|
---|
83 | return false;
|
---|
84 |
|
---|
85 | // Now check to see if the point is in range of an arc.
|
---|
86 | double dy = Math.min(Math.abs(my - y), Math.abs(my + mh - y));
|
---|
87 | double dx = Math.min(Math.abs(mx - x), Math.abs(mx + mw - x));
|
---|
88 | double aw = getArcWidth();
|
---|
89 | double ah = getArcHeight();
|
---|
90 | if (dx > aw || dy > ah)
|
---|
91 | return true;
|
---|
92 |
|
---|
93 | // At this point DX represents the distance from the nearest edge
|
---|
94 | // of the rectangle. But we want to transform it to represent the
|
---|
95 | // scaled distance from the center of the ellipse that forms the
|
---|
96 | // arc. Hence this code:
|
---|
97 | dy = (ah - dy) / ah;
|
---|
98 | dx = (aw - dx) / aw;
|
---|
99 |
|
---|
100 | return dx * dx + dy * dy <= 1.0;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /** Return true if this object contains the specified rectangle
|
---|
104 | * @param x The x coordinate
|
---|
105 | * @param y The y coordinate
|
---|
106 | * @param w The width
|
---|
107 | * @param h The height
|
---|
108 | */
|
---|
109 | public boolean contains(double x, double y, double w, double h)
|
---|
110 | {
|
---|
111 | // We have to check all four points here (for ordinary rectangles
|
---|
112 | // we can just check opposing corners).
|
---|
113 | return (contains(x, y) && contains(x + w, h)
|
---|
114 | && contains(x, y + h) && contains(x + w, y + h));
|
---|
115 | }
|
---|
116 |
|
---|
117 | /** Return a new path iterator which iterates over this rectangle.
|
---|
118 | * @param at An affine transform to apply to the object
|
---|
119 | */
|
---|
120 | public PathIterator getPathIterator(AffineTransform at)
|
---|
121 | {
|
---|
122 | // FIXME.
|
---|
123 | return null;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /** Return true if the given rectangle intersects this shape.
|
---|
127 | * @param x The x coordinate
|
---|
128 | * @param y The y coordinate
|
---|
129 | * @param w The width
|
---|
130 | * @param h The height
|
---|
131 | */
|
---|
132 | public boolean intersects(double x, double y, double w, double h)
|
---|
133 | {
|
---|
134 | // Here we can use the same code we use for an ordinary rectangle.
|
---|
135 | double mx = getX();
|
---|
136 | double mw = getWidth();
|
---|
137 | if (x < mx || x >= mx + mw || x + w < mx || x + w >= mx + mw)
|
---|
138 | return false;
|
---|
139 | double my = getY();
|
---|
140 | double mh = getHeight();
|
---|
141 | return y >= my && y < my + mh && y + h >= my && y + h < my + mh;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /** Set the boundary of this round rectangle.
|
---|
145 | * @param x The x coordinate
|
---|
146 | * @param y The y coordinate
|
---|
147 | * @param w The width
|
---|
148 | * @param h The height
|
---|
149 | */
|
---|
150 | public void setFrame(double x, double y, double w, double h)
|
---|
151 | {
|
---|
152 | // This is a bit lame.
|
---|
153 | setRoundRect(x, y, w, h, getArcWidth(), getArcHeight());
|
---|
154 | }
|
---|
155 |
|
---|
156 | /** Set the values of this round rectangle to be the same as those
|
---|
157 | * of the argument.
|
---|
158 | * @param rr The round rectangle to copy
|
---|
159 | */
|
---|
160 | public void setRoundRect(RoundRectangle2D rr)
|
---|
161 | {
|
---|
162 | setRoundRect(rr.getX(), rr.getY(), rr.getWidth(), rr.getHeight(),
|
---|
163 | rr.getArcWidth(), rr.getArcHeight());
|
---|
164 | }
|
---|
165 |
|
---|
166 | /** A subclass of RoundRectangle which keeps its parameters as
|
---|
167 | * doubles. */
|
---|
168 | public static class Double extends RoundRectangle2D
|
---|
169 | {
|
---|
170 | /** The height of the corner arc. */
|
---|
171 | public double archeight;
|
---|
172 |
|
---|
173 | /** The width of the corner arc. */
|
---|
174 | public double arcwidth;
|
---|
175 |
|
---|
176 | /** The x coordinate of this object. */
|
---|
177 | public double x;
|
---|
178 |
|
---|
179 | /** The y coordinate of this object. */
|
---|
180 | public double y;
|
---|
181 |
|
---|
182 | /** The width of this object. */
|
---|
183 | public double width;
|
---|
184 |
|
---|
185 | /** The height of this object. */
|
---|
186 | public double height;
|
---|
187 |
|
---|
188 | /** Construct a new instance, with all parameters set to 0. */
|
---|
189 | public Double()
|
---|
190 | {
|
---|
191 | }
|
---|
192 |
|
---|
193 | /** Construct a new instance with the given arguments.
|
---|
194 | * @param x The x coordinate
|
---|
195 | * @param y The y coordinate
|
---|
196 | * @param w The width
|
---|
197 | * @param h The height
|
---|
198 | * @param arcWidth The arc width
|
---|
199 | * @param arcHeight The arc height
|
---|
200 | */
|
---|
201 | public Double(double x, double y, double w, double h,
|
---|
202 | double arcWidth, double arcHeight)
|
---|
203 | {
|
---|
204 | this.x = x;
|
---|
205 | this.y = y;
|
---|
206 | this.width = w;
|
---|
207 | this.height = h;
|
---|
208 | this.arcwidth = arcWidth;
|
---|
209 | this.archeight = arcHeight;
|
---|
210 | }
|
---|
211 |
|
---|
212 | public double getArcHeight()
|
---|
213 | {
|
---|
214 | return archeight;
|
---|
215 | }
|
---|
216 |
|
---|
217 | public double getArcWidth()
|
---|
218 | {
|
---|
219 | return arcwidth;
|
---|
220 | }
|
---|
221 |
|
---|
222 | public Rectangle2D getBounds2D()
|
---|
223 | {
|
---|
224 | return new Rectangle2D.Double(x, y, width, height);
|
---|
225 | }
|
---|
226 |
|
---|
227 | public double getX()
|
---|
228 | {
|
---|
229 | return x;
|
---|
230 | }
|
---|
231 |
|
---|
232 | public double getY()
|
---|
233 | {
|
---|
234 | return y;
|
---|
235 | }
|
---|
236 |
|
---|
237 | public double getWidth()
|
---|
238 | {
|
---|
239 | return width;
|
---|
240 | }
|
---|
241 |
|
---|
242 | public double getHeight()
|
---|
243 | {
|
---|
244 | return height;
|
---|
245 | }
|
---|
246 |
|
---|
247 | public boolean isEmpty()
|
---|
248 | {
|
---|
249 | return width <= 0 || height <= 0;
|
---|
250 | }
|
---|
251 |
|
---|
252 | public void setRoundRect(double x, double y, double w, double h,
|
---|
253 | double arcWidth, double arcHeight)
|
---|
254 | {
|
---|
255 | this.x = x;
|
---|
256 | this.y = y;
|
---|
257 | this.width = w;
|
---|
258 | this.height = h;
|
---|
259 | this.arcwidth = arcWidth;
|
---|
260 | this.archeight = arcHeight;
|
---|
261 | }
|
---|
262 | } // class Double
|
---|
263 |
|
---|
264 | /** A subclass of RoundRectangle which keeps its parameters as
|
---|
265 | * floats. */
|
---|
266 | public static class Float extends RoundRectangle2D
|
---|
267 | {
|
---|
268 | /** The height of the corner arc. */
|
---|
269 | public float archeight;
|
---|
270 |
|
---|
271 | /** The width of the corner arc. */
|
---|
272 | public float arcwidth;
|
---|
273 |
|
---|
274 | /** The x coordinate of this object. */
|
---|
275 | public float x;
|
---|
276 |
|
---|
277 | /** The y coordinate of this object. */
|
---|
278 | public float y;
|
---|
279 |
|
---|
280 | /** The width of this object. */
|
---|
281 | public float width;
|
---|
282 |
|
---|
283 | /** The height of this object. */
|
---|
284 | public float height;
|
---|
285 |
|
---|
286 | /** Construct a new instance, with all parameters set to 0. */
|
---|
287 | public Float()
|
---|
288 | {
|
---|
289 | }
|
---|
290 |
|
---|
291 | /** Construct a new instance with the given arguments.
|
---|
292 | * @param x The x coordinate
|
---|
293 | * @param y The y coordinate
|
---|
294 | * @param w The width
|
---|
295 | * @param h The height
|
---|
296 | * @param arcWidth The arc width
|
---|
297 | * @param arcHeight The arc height
|
---|
298 | */
|
---|
299 | public Float(float x, float y, float w, float h,
|
---|
300 | float arcWidth, float arcHeight)
|
---|
301 | {
|
---|
302 | this.x = x;
|
---|
303 | this.y = y;
|
---|
304 | this.width = w;
|
---|
305 | this.height = h;
|
---|
306 | this.arcwidth = arcWidth;
|
---|
307 | this.archeight = arcHeight;
|
---|
308 | }
|
---|
309 |
|
---|
310 | public double getArcHeight()
|
---|
311 | {
|
---|
312 | return archeight;
|
---|
313 | }
|
---|
314 |
|
---|
315 | public double getArcWidth()
|
---|
316 | {
|
---|
317 | return arcwidth;
|
---|
318 | }
|
---|
319 |
|
---|
320 | public Rectangle2D getBounds2D()
|
---|
321 | {
|
---|
322 | return new Rectangle2D.Float(x, y, width, height);
|
---|
323 | }
|
---|
324 |
|
---|
325 | public double getX()
|
---|
326 | {
|
---|
327 | return x;
|
---|
328 | }
|
---|
329 |
|
---|
330 | public double getY()
|
---|
331 | {
|
---|
332 | return y;
|
---|
333 | }
|
---|
334 |
|
---|
335 | public double getWidth()
|
---|
336 | {
|
---|
337 | return width;
|
---|
338 | }
|
---|
339 |
|
---|
340 | public double getHeight()
|
---|
341 | {
|
---|
342 | return height;
|
---|
343 | }
|
---|
344 |
|
---|
345 | public boolean isEmpty()
|
---|
346 | {
|
---|
347 | return width <= 0 || height <= 0;
|
---|
348 | }
|
---|
349 |
|
---|
350 | public void setRoundRect(float x, float y, float w, float h,
|
---|
351 | float arcWidth, float arcHeight)
|
---|
352 | {
|
---|
353 | this.x = x;
|
---|
354 | this.y = y;
|
---|
355 | this.width = w;
|
---|
356 | this.height = h;
|
---|
357 | this.arcwidth = arcWidth;
|
---|
358 | this.archeight = arcHeight;
|
---|
359 | }
|
---|
360 |
|
---|
361 | public void setRoundRect(double x, double y, double w, double h,
|
---|
362 | double arcWidth, double arcHeight)
|
---|
363 | {
|
---|
364 | this.x = (float) x;
|
---|
365 | this.y = (float) y;
|
---|
366 | this.width = (float) w;
|
---|
367 | this.height = (float) h;
|
---|
368 | this.arcwidth = (float) arcWidth;
|
---|
369 | this.archeight = (float) arcHeight;
|
---|
370 | }
|
---|
371 | } // class Float
|
---|
372 | } // class RoundRectangle2D
|
---|