1:
37:
38: package ;
39:
40: import ;
41:
42:
43:
47: public abstract class RoundRectangle2D extends RectangularShape
48: {
49:
50: public abstract double getArcHeight();
51:
52:
53: public abstract double getArcWidth();
54:
55:
63: public abstract void setRoundRect(double x, double y, double w, double h,
64: double arcWidth, double arcHeight);
65:
66:
69: protected RoundRectangle2D()
70: {
71: }
72:
73:
77: public boolean contains(double x, double y)
78: {
79: double mx = getX();
80: double mw = getWidth();
81: if (x < mx || x >= mx + mw)
82: return false;
83: double my = getY();
84: double mh = getHeight();
85: if (y < my || y >= my + mh)
86: return false;
87:
88:
89: double dy = Math.min(Math.abs(my - y), Math.abs(my + mh - y));
90: double dx = Math.min(Math.abs(mx - x), Math.abs(mx + mw - x));
91:
92:
93:
94: double aw = getArcWidth() / 2.0;
95: double ah = getArcHeight() / 2.0;
96: if (dx > aw || dy > ah)
97: return true;
98:
99:
100:
101:
102:
103: dy = (ah - dy) / ah;
104: dx = (aw - dx) / aw;
105:
106: return dx * dx + dy * dy <= 1.0;
107: }
108:
109:
115: public boolean contains(double x, double y, double w, double h)
116: {
117:
118:
119: return (contains(x, y) && contains(x, y + h) && contains(x + w, y + h)
120: && contains(x + w, y));
121: }
122:
123:
126: public PathIterator getPathIterator(final AffineTransform at)
127: {
128: final double minx = getX();
129: final double miny = getY();
130: final double maxx = minx + getWidth();
131: final double maxy = miny + getHeight();
132: final double arcwidth = getArcWidth();
133: final double archeight = getArcHeight();
134: return new PathIterator()
135: {
136:
139: private int current = 0;
140:
141:
142: private PathIterator corner;
143:
144:
146: private Arc2D arc = new Arc2D.Double();
147:
148:
149: private double[] temp = new double[2];
150:
151: public int getWindingRule()
152: {
153: return WIND_NON_ZERO;
154: }
155:
156: public boolean isDone()
157: {
158: return current > 9;
159: }
160:
161: private void getPoint(int val)
162: {
163: switch (val)
164: {
165: case 0:
166: case 8:
167: temp[0] = maxx;
168: temp[1] = miny + archeight;
169: break;
170: case 7:
171: temp[0] = maxx;
172: temp[1] = maxy - archeight;
173: break;
174: case 6:
175: temp[0] = maxx - arcwidth;
176: temp[1] = maxy;
177: break;
178: case 5:
179: temp[0] = minx + arcwidth;
180: temp[1] = maxy;
181: break;
182: case 4:
183: temp[0] = minx;
184: temp[1] = maxy - archeight;
185: break;
186: case 3:
187: temp[0] = minx;
188: temp[1] = miny + archeight;
189: break;
190: case 2:
191: temp[0] = minx + arcwidth;
192: temp[1] = miny;
193: break;
194: case 1:
195: temp[0] = maxx - arcwidth;
196: temp[1] = miny;
197: break;
198: }
199: }
200:
201: public void next()
202: {
203: if (current >= 8)
204: ++current;
205: else if (corner != null)
206: {
207:
208:
209:
210: corner.next();
211: if (corner.isDone())
212: {
213: corner = null;
214: ++current;
215: }
216: }
217: else
218: {
219:
220:
221: getPoint(current);
222: double x1 = temp[0];
223: double y1 = temp[1];
224: getPoint(current + 1);
225: Rectangle2D.Double r = new Rectangle2D.Double(Math.min(x1,
226: temp[0]),
227: Math.min(y1,
228: temp[1]),
229: Math.abs(x1
230: - temp[0]),
231: Math.abs(y1
232: - temp[1]));
233: arc.setArc(r, (current >> 1) * 90.0, 90.0, Arc2D.OPEN);
234: corner = arc.getPathIterator(at);
235: }
236: }
237:
238: public int currentSegment(float[] coords)
239: {
240: if (corner != null)
241: {
242: int r = corner.currentSegment(coords);
243: if (r == SEG_MOVETO)
244: r = SEG_LINETO;
245: return r;
246: }
247:
248: if (current < 9)
249: {
250: getPoint(current);
251: coords[0] = (float) temp[0];
252: coords[1] = (float) temp[1];
253: }
254: else if (current == 9)
255: return SEG_CLOSE;
256: else
257: throw new NoSuchElementException("rect iterator out of bounds");
258:
259: if (at != null)
260: at.transform(coords, 0, coords, 0, 1);
261: return current == 0 ? SEG_MOVETO : SEG_LINETO;
262: }
263:
264: public int currentSegment(double[] coords)
265: {
266: if (corner != null)
267: {
268: int r = corner.currentSegment(coords);
269: if (r == SEG_MOVETO)
270: r = SEG_LINETO;
271: return r;
272: }
273:
274: if (current < 9)
275: {
276: getPoint(current);
277: coords[0] = temp[0];
278: coords[1] = temp[1];
279: }
280: else if (current == 9)
281: return SEG_CLOSE;
282: else
283: throw new NoSuchElementException("rect iterator out of bounds");
284:
285: if (at != null)
286: at.transform(coords, 0, coords, 0, 1);
287: return current == 0 ? SEG_MOVETO : SEG_LINETO;
288: }
289: };
290: }
291:
292:
298: public boolean intersects(double x, double y, double w, double h)
299: {
300:
301: return (contains(x, y) || contains(x, y + h) || contains(x + w, y + h)
302: || contains(x + w, y));
303: }
304:
305:
311: public void setFrame(double x, double y, double w, double h)
312: {
313:
314: setRoundRect(x, y, w, h, getArcWidth(), getArcHeight());
315: }
316:
317:
321: public void setRoundRect(RoundRectangle2D rr)
322: {
323: setRoundRect(rr.getX(), rr.getY(), rr.getWidth(), rr.getHeight(),
324: rr.getArcWidth(), rr.getArcHeight());
325: }
326:
327:
329: public static class Double extends RoundRectangle2D
330: {
331:
332: public double archeight;
333:
334:
335: public double arcwidth;
336:
337:
338: public double x;
339:
340:
341: public double y;
342:
343:
344: public double width;
345:
346:
347: public double height;
348:
349:
350: public Double()
351: {
352: }
353:
354:
362: public Double(double x, double y, double w, double h, double arcWidth,
363: double arcHeight)
364: {
365: this.x = x;
366: this.y = y;
367: this.width = w;
368: this.height = h;
369: this.arcwidth = arcWidth;
370: this.archeight = arcHeight;
371: }
372:
373: public double getArcHeight()
374: {
375: return archeight;
376: }
377:
378: public double getArcWidth()
379: {
380: return arcwidth;
381: }
382:
383: public Rectangle2D getBounds2D()
384: {
385: return new Rectangle2D.Double(x, y, width, height);
386: }
387:
388: public double getX()
389: {
390: return x;
391: }
392:
393: public double getY()
394: {
395: return y;
396: }
397:
398: public double getWidth()
399: {
400: return width;
401: }
402:
403: public double getHeight()
404: {
405: return height;
406: }
407:
408: public boolean isEmpty()
409: {
410: return width <= 0 || height <= 0;
411: }
412:
413: public void setRoundRect(double x, double y, double w, double h,
414: double arcWidth, double arcHeight)
415: {
416: this.x = x;
417: this.y = y;
418: this.width = w;
419: this.height = h;
420: this.arcwidth = arcWidth;
421: this.archeight = arcHeight;
422: }
423: }
424:
425:
427: public static class Float extends RoundRectangle2D
428: {
429:
430: public float archeight;
431:
432:
433: public float arcwidth;
434:
435:
436: public float x;
437:
438:
439: public float y;
440:
441:
442: public float width;
443:
444:
445: public float height;
446:
447:
448: public Float()
449: {
450: }
451:
452:
460: public Float(float x, float y, float w, float h, float arcWidth,
461: float arcHeight)
462: {
463: this.x = x;
464: this.y = y;
465: this.width = w;
466: this.height = h;
467: this.arcwidth = arcWidth;
468: this.archeight = arcHeight;
469: }
470:
471: public double getArcHeight()
472: {
473: return archeight;
474: }
475:
476: public double getArcWidth()
477: {
478: return arcwidth;
479: }
480:
481: public Rectangle2D getBounds2D()
482: {
483: return new Rectangle2D.Float(x, y, width, height);
484: }
485:
486: public double getX()
487: {
488: return x;
489: }
490:
491: public double getY()
492: {
493: return y;
494: }
495:
496: public double getWidth()
497: {
498: return width;
499: }
500:
501: public double getHeight()
502: {
503: return height;
504: }
505:
506: public boolean isEmpty()
507: {
508: return width <= 0 || height <= 0;
509: }
510:
511: public void setRoundRect(float x, float y, float w, float h,
512: float arcWidth, float arcHeight)
513: {
514: this.x = x;
515: this.y = y;
516: this.width = w;
517: this.height = h;
518: this.arcwidth = arcWidth;
519: this.archeight = arcHeight;
520: }
521:
522: public void setRoundRect(double x, double y, double w, double h,
523: double arcWidth, double arcHeight)
524: {
525: this.x = (float) x;
526: this.y = (float) y;
527: this.width = (float) w;
528: this.height = (float) h;
529: this.arcwidth = (float) arcWidth;
530: this.archeight = (float) arcHeight;
531: }
532: }
533: }