001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jcommon/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * -------------------
028: * ShapeUtilities.java
029: * -------------------
030: * (C)opyright 2003-2005, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: ShapeUtilities.java,v 1.17 2005/11/02 16:31:50 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 13-Aug-2003 : Version 1 (DG);
040: * 16-Mar-2004 : Moved rotateShape() from RefineryUtilities.java to here (DG);
041: * 13-May-2004 : Added new shape creation methods (DG);
042: * 30-Sep-2004 : Added createLineRegion() method (DG);
043: * Moved drawRotatedShape() method from RefineryUtilities class
044: * to this class (DG);
045: * 04-Oct-2004 : Renamed ShapeUtils --> ShapeUtilities (DG);
046: * 26-Oct-2004 : Added a method to test the equality of two Line2D
047: * instances (DG);
048: * 10-Nov-2004 : Added new translateShape() and equal(Ellipse2D, Ellipse2D)
049: * methods (DG);
050: * 11-Nov-2004 : Renamed translateShape() --> createTranslatedShape() (DG);
051: * 07-Jan-2005 : Minor Javadoc fix (DG);
052: * 11-Jan-2005 : Removed deprecated code in preparation for 1.0.0 release (DG);
053: * 21-Jan-2005 : Modified return type of RectangleAnchor.coordinates()
054: * method (DG);
055: * 22-Feb-2005 : Added equality tests for Arc2D and GeneralPath (DG);
056: * 16-Mar-2005 : Fixed bug where equal(Shape, Shape) fails for two Polygon
057: * instances (DG);
058: *
059: */
060:
061: package org.jfree.util;
062:
063: import java.awt.Graphics2D;
064: import java.awt.Polygon;
065: import java.awt.Shape;
066: import java.awt.geom.AffineTransform;
067: import java.awt.geom.Arc2D;
068: import java.awt.geom.Ellipse2D;
069: import java.awt.geom.GeneralPath;
070: import java.awt.geom.Line2D;
071: import java.awt.geom.PathIterator;
072: import java.awt.geom.Point2D;
073: import java.awt.geom.Rectangle2D;
074: import java.util.Arrays;
075:
076: import org.jfree.ui.RectangleAnchor;
077:
078: /**
079: * Utility methods for {@link Shape} objects.
080: *
081: * @author David Gilbert
082: */
083: public class ShapeUtilities {
084:
085: /**
086: * Prevents instantiation.
087: */
088: private ShapeUtilities() {
089: }
090:
091: /**
092: * Returns a clone of the specified shape, or <code>null</code>. At the
093: * current time, this method supports cloning for instances of
094: * <code>Line2D</code>, <code>RectangularShape</code>, <code>Area</code>
095: * and <code>GeneralPath</code>.
096: * <p>
097: * <code>RectangularShape</code> includes <code>Arc2D</code>,
098: * <code>Ellipse2D</code>, <code>Rectangle2D</code>,
099: * <code>RoundRectangle2D</code>.
100: *
101: * @param shape the shape to clone (<code>null</code> permitted,
102: * returns <code>null</code>).
103: *
104: * @return A clone or <code>null</code>.
105: */
106: public static Shape clone(final Shape shape) {
107:
108: if (shape instanceof Cloneable) {
109: try {
110: return (Shape) ObjectUtilities.clone(shape);
111: } catch (CloneNotSupportedException cnse) {
112: }
113: }
114: final Shape result = null;
115: return result;
116: }
117:
118: /**
119: * Tests two shapes for equality. If both shapes are <code>null</code>,
120: * this method will return <code>true</code>.
121: * <p>
122: * In the current implementation, the following shapes are supported:
123: * <code>Ellipse2D</code>, <code>Line2D</code> and <code>Rectangle2D</code>
124: * (implicit).
125: *
126: * @param s1 the first shape (<code>null</code> permitted).
127: * @param s2 the second shape (<code>null</code> permitted).
128: *
129: * @return A boolean.
130: */
131: public static boolean equal(final Shape s1, final Shape s2) {
132: if (s1 instanceof Line2D && s2 instanceof Line2D) {
133: return equal((Line2D) s1, (Line2D) s2);
134: } else if (s1 instanceof Ellipse2D && s2 instanceof Ellipse2D) {
135: return equal((Ellipse2D) s1, (Ellipse2D) s2);
136: } else if (s1 instanceof Arc2D && s2 instanceof Arc2D) {
137: return equal((Arc2D) s1, (Arc2D) s2);
138: } else if (s1 instanceof Polygon && s2 instanceof Polygon) {
139: return equal((Polygon) s1, (Polygon) s2);
140: } else if (s1 instanceof GeneralPath
141: && s2 instanceof GeneralPath) {
142: return equal((GeneralPath) s1, (GeneralPath) s2);
143: } else {
144: // this will handle Rectangle2D...
145: return ObjectUtilities.equal(s1, s2);
146: }
147: }
148:
149: /**
150: * Compares two lines are returns <code>true</code> if they are equal or
151: * both <code>null</code>.
152: *
153: * @param l1 the first line (<code>null</code> permitted).
154: * @param l2 the second line (<code>null</code> permitted).
155: *
156: * @return A boolean.
157: */
158: public static boolean equal(final Line2D l1, final Line2D l2) {
159: if (l1 == null) {
160: return (l2 == null);
161: }
162: if (l2 == null) {
163: return false;
164: }
165: if (!l1.getP1().equals(l2.getP1())) {
166: return false;
167: }
168: if (!l1.getP2().equals(l2.getP2())) {
169: return false;
170: }
171: return true;
172: }
173:
174: /**
175: * Compares two ellipses and returns <code>true</code> if they are equal or
176: * both <code>null</code>.
177: *
178: * @param e1 the first ellipse (<code>null</code> permitted).
179: * @param e2 the second ellipse (<code>null</code> permitted).
180: *
181: * @return A boolean.
182: */
183: public static boolean equal(final Ellipse2D e1, final Ellipse2D e2) {
184: if (e1 == null) {
185: return (e2 == null);
186: }
187: if (e2 == null) {
188: return false;
189: }
190: if (!e1.getFrame().equals(e2.getFrame())) {
191: return false;
192: }
193: return true;
194: }
195:
196: /**
197: * Compares two arcs and returns <code>true</code> if they are equal or
198: * both <code>null</code>.
199: *
200: * @param a1 the first arc (<code>null</code> permitted).
201: * @param a2 the second arc (<code>null</code> permitted).
202: *
203: * @return A boolean.
204: */
205: public static boolean equal(final Arc2D a1, final Arc2D a2) {
206: if (a1 == null) {
207: return (a2 == null);
208: }
209: if (a2 == null) {
210: return false;
211: }
212: if (!a1.getFrame().equals(a2.getFrame())) {
213: return false;
214: }
215: if (a1.getAngleStart() != a2.getAngleStart()) {
216: return false;
217: }
218: if (a1.getAngleExtent() != a2.getAngleExtent()) {
219: return false;
220: }
221: if (a1.getArcType() != a2.getArcType()) {
222: return false;
223: }
224: return true;
225: }
226:
227: /**
228: * Tests two polygons for equality. If both are <code>null</code> this
229: * method returns <code>true</code>.
230: *
231: * @param p1 polygon 1 (<code>null</code> permitted).
232: * @param p2 polygon 2 (<code>null</code> permitted).
233: *
234: * @return A boolean.
235: */
236: public static boolean equal(final Polygon p1, final Polygon p2) {
237: if (p1 == null) {
238: return (p2 == null);
239: }
240: if (p2 == null) {
241: return false;
242: }
243: if (p1.npoints != p2.npoints) {
244: return false;
245: }
246: if (!Arrays.equals(p1.xpoints, p2.xpoints)) {
247: return false;
248: }
249: if (!Arrays.equals(p1.ypoints, p2.ypoints)) {
250: return false;
251: }
252: return true;
253: }
254:
255: /**
256: * Tests two polygons for equality. If both are <code>null</code> this
257: * method returns <code>true</code>.
258: *
259: * @param p1 path 1 (<code>null</code> permitted).
260: * @param p2 path 2 (<code>null</code> permitted).
261: *
262: * @return A boolean.
263: */
264: public static boolean equal(final GeneralPath p1,
265: final GeneralPath p2) {
266: if (p1 == null) {
267: return (p2 == null);
268: }
269: if (p2 == null) {
270: return false;
271: }
272: if (p1.getWindingRule() != p2.getWindingRule()) {
273: return false;
274: }
275: PathIterator iterator1 = p1.getPathIterator(null);
276: PathIterator iterator2 = p1.getPathIterator(null);
277: double[] d1 = new double[6];
278: double[] d2 = new double[6];
279: boolean done = iterator1.isDone() && iterator2.isDone();
280: while (!done) {
281: if (iterator1.isDone() != iterator2.isDone()) {
282: return false;
283: }
284: int seg1 = iterator1.currentSegment(d1);
285: int seg2 = iterator2.currentSegment(d2);
286: if (seg1 != seg2) {
287: return false;
288: }
289: if (!Arrays.equals(d1, d2)) {
290: return false;
291: }
292: iterator1.next();
293: iterator2.next();
294: done = iterator1.isDone() && iterator2.isDone();
295: }
296: return true;
297: }
298:
299: /**
300: * Creates and returns a translated shape.
301: *
302: * @param shape the shape (<code>null</code> not permitted).
303: * @param transX the x translation (in Java2D space).
304: * @param transY the y translation (in Java2D space).
305: *
306: * @return The translated shape.
307: */
308: public static Shape createTranslatedShape(final Shape shape,
309: final double transX, final double transY) {
310: if (shape == null) {
311: throw new IllegalArgumentException("Null 'shape' argument.");
312: }
313: final AffineTransform transform = AffineTransform
314: .getTranslateInstance(transX, transY);
315: return transform.createTransformedShape(shape);
316: }
317:
318: /**
319: * Translates a shape to a new location such that the anchor point
320: * (relative to the rectangular bounds of the shape) aligns with the
321: * specified (x, y) coordinate in Java2D space.
322: *
323: * @param shape the shape (<code>null</code> not permitted).
324: * @param anchor the anchor (<code>null</code> not permitted).
325: * @param locationX the x-coordinate (in Java2D space).
326: * @param locationY the y-coordinate (in Java2D space).
327: *
328: * @return A new and translated shape.
329: */
330: public static Shape createTranslatedShape(final Shape shape,
331: final RectangleAnchor anchor, final double locationX,
332: final double locationY) {
333: if (shape == null) {
334: throw new IllegalArgumentException("Null 'shape' argument.");
335: }
336: if (anchor == null) {
337: throw new IllegalArgumentException(
338: "Null 'anchor' argument.");
339: }
340: Point2D anchorPoint = RectangleAnchor.coordinates(shape
341: .getBounds2D(), anchor);
342: final AffineTransform transform = AffineTransform
343: .getTranslateInstance(locationX - anchorPoint.getX(),
344: locationY - anchorPoint.getY());
345: return transform.createTransformedShape(shape);
346: }
347:
348: /**
349: * Rotates a shape about the specified coordinates.
350: *
351: * @param base the shape (<code>null</code> permitted, returns
352: * <code>null</code>).
353: * @param angle the angle (in radians).
354: * @param x the x coordinate for the rotation point (in Java2D space).
355: * @param y the y coordinate for the rotation point (in Java2D space).
356: *
357: * @return the rotated shape.
358: */
359: public static Shape rotateShape(final Shape base,
360: final double angle, final float x, final float y) {
361: if (base == null) {
362: return null;
363: }
364: final AffineTransform rotate = AffineTransform
365: .getRotateInstance(angle, x, y);
366: final Shape result = rotate.createTransformedShape(base);
367: return result;
368: }
369:
370: /**
371: * Draws a shape with the specified rotation about <code>(x, y)</code>.
372: *
373: * @param g2 the graphics device (<code>null</code> not permitted).
374: * @param shape the shape (<code>null</code> not permitted).
375: * @param angle the angle (in radians).
376: * @param x the x coordinate for the rotation point.
377: * @param y the y coordinate for the rotation point.
378: */
379: public static void drawRotatedShape(final Graphics2D g2,
380: final Shape shape, final double angle, final float x,
381: final float y) {
382:
383: final AffineTransform saved = g2.getTransform();
384: final AffineTransform rotate = AffineTransform
385: .getRotateInstance(angle, x, y);
386: g2.transform(rotate);
387: g2.draw(shape);
388: g2.setTransform(saved);
389:
390: }
391:
392: /** A useful constant used internally. */
393: private static final float SQRT2 = (float) Math.pow(2.0, 0.5);
394:
395: /**
396: * Creates a diagonal cross shape.
397: *
398: * @param l the length of each 'arm'.
399: * @param t the thickness.
400: *
401: * @return A diagonal cross shape.
402: */
403: public static Shape createDiagonalCross(final float l, final float t) {
404: final GeneralPath p0 = new GeneralPath();
405: p0.moveTo(-l - t, -l + t);
406: p0.lineTo(-l + t, -l - t);
407: p0.lineTo(0.0f, -t * SQRT2);
408: p0.lineTo(l - t, -l - t);
409: p0.lineTo(l + t, -l + t);
410: p0.lineTo(t * SQRT2, 0.0f);
411: p0.lineTo(l + t, l - t);
412: p0.lineTo(l - t, l + t);
413: p0.lineTo(0.0f, t * SQRT2);
414: p0.lineTo(-l + t, l + t);
415: p0.lineTo(-l - t, l - t);
416: p0.lineTo(-t * SQRT2, 0.0f);
417: p0.closePath();
418: return p0;
419: }
420:
421: /**
422: * Creates a diagonal cross shape.
423: *
424: * @param l the length of each 'arm'.
425: * @param t the thickness.
426: *
427: * @return A diagonal cross shape.
428: */
429: public static Shape createRegularCross(final float l, final float t) {
430: final GeneralPath p0 = new GeneralPath();
431: p0.moveTo(-l, t);
432: p0.lineTo(-t, t);
433: p0.lineTo(-t, l);
434: p0.lineTo(t, l);
435: p0.lineTo(t, t);
436: p0.lineTo(l, t);
437: p0.lineTo(l, -t);
438: p0.lineTo(t, -t);
439: p0.lineTo(t, -l);
440: p0.lineTo(-t, -l);
441: p0.lineTo(-t, -t);
442: p0.lineTo(-l, -t);
443: p0.closePath();
444: return p0;
445: }
446:
447: /**
448: * Creates a diamond shape.
449: *
450: * @param s the size factor (equal to half the height of the diamond).
451: *
452: * @return A diamond shape.
453: */
454: public static Shape createDiamond(final float s) {
455: final GeneralPath p0 = new GeneralPath();
456: p0.moveTo(0.0f, -s);
457: p0.lineTo(s, 0.0f);
458: p0.lineTo(0.0f, s);
459: p0.lineTo(-s, 0.0f);
460: p0.closePath();
461: return p0;
462: }
463:
464: /**
465: * Creates a triangle shape that points upwards.
466: *
467: * @param s the size factor (equal to half the height of the triangle).
468: *
469: * @return A triangle shape.
470: */
471: public static Shape createUpTriangle(final float s) {
472: final GeneralPath p0 = new GeneralPath();
473: p0.moveTo(0.0f, -s);
474: p0.lineTo(s, s);
475: p0.lineTo(-s, s);
476: p0.closePath();
477: return p0;
478: }
479:
480: /**
481: * Creates a triangle shape that points downwards.
482: *
483: * @param s the size factor (equal to half the height of the triangle).
484: *
485: * @return A triangle shape.
486: */
487: public static Shape createDownTriangle(final float s) {
488: final GeneralPath p0 = new GeneralPath();
489: p0.moveTo(0.0f, s);
490: p0.lineTo(s, -s);
491: p0.lineTo(-s, -s);
492: p0.closePath();
493: return p0;
494: }
495:
496: /**
497: * Creates a region surrounding a line segment by 'widening' the line
498: * segment. A typical use for this method is the creation of a
499: * 'clickable' region for a line that is displayed on-screen.
500: *
501: * @param line the line (<code>null</code> not permitted).
502: * @param width the width of the region.
503: *
504: * @return A region that surrounds the line.
505: */
506: public static Shape createLineRegion(final Line2D line,
507: final float width) {
508: final GeneralPath result = new GeneralPath();
509: final float x1 = (float) line.getX1();
510: final float x2 = (float) line.getX2();
511: final float y1 = (float) line.getY1();
512: final float y2 = (float) line.getY2();
513: if ((x2 - x1) != 0.0) {
514: final double theta = Math.atan((y2 - y1) / (x2 - x1));
515: final float dx = (float) Math.sin(theta) * width;
516: final float dy = (float) Math.cos(theta) * width;
517: result.moveTo(x1 - dx, y1 + dy);
518: result.lineTo(x1 + dx, y1 - dy);
519: result.lineTo(x2 + dx, y2 - dy);
520: result.lineTo(x2 - dx, y2 + dy);
521: result.closePath();
522: } else {
523: // special case, vertical line
524: result.moveTo(x1 - width / 2.0f, y1);
525: result.lineTo(x1 + width / 2.0f, y1);
526: result.lineTo(x2 + width / 2.0f, y2);
527: result.lineTo(x2 - width / 2.0f, y2);
528: result.closePath();
529: }
530: return result;
531: }
532:
533: /**
534: * Returns a point based on (x, y) but constrained to be within the bounds
535: * of a given rectangle.
536: *
537: * @param x the x-coordinate.
538: * @param y the y-coordinate.
539: * @param area the constraining rectangle (<code>null</code> not
540: * permitted).
541: *
542: * @return A point within the rectangle.
543: *
544: * @throws NullPointerException if <code>area</code> is <code>null</code>.
545: */
546: public static Point2D getPointInRectangle(double x, double y,
547: final Rectangle2D area) {
548:
549: x = Math.max(area.getMinX(), Math.min(x, area.getMaxX()));
550: y = Math.max(area.getMinY(), Math.min(y, area.getMaxY()));
551: return new Point2D.Double(x, y);
552:
553: }
554:
555: /**
556: * Checks, whether the given rectangle1 fully contains rectangle 2
557: * (even if rectangle 2 has a height or width of zero!).
558: *
559: * @param rect1 the first rectangle.
560: * @param rect2 the second rectangle.
561: *
562: * @return A boolean.
563: */
564: public static boolean contains(final Rectangle2D rect1,
565: final Rectangle2D rect2) {
566:
567: final double x0 = rect1.getX();
568: final double y0 = rect1.getY();
569: final double x = rect2.getX();
570: final double y = rect2.getY();
571: final double w = rect2.getWidth();
572: final double h = rect2.getHeight();
573:
574: return ((x >= x0) && (y >= y0)
575: && ((x + w) <= (x0 + rect1.getWidth())) && ((y + h) <= (y0 + rect1
576: .getHeight())));
577:
578: }
579:
580: /**
581: * Checks, whether the given rectangle1 fully contains rectangle 2
582: * (even if rectangle 2 has a height or width of zero!).
583: *
584: * @param rect1 the first rectangle.
585: * @param rect2 the second rectangle.
586: *
587: * @return A boolean.
588: */
589: public static boolean intersects(final Rectangle2D rect1,
590: final Rectangle2D rect2) {
591:
592: final double x0 = rect1.getX();
593: final double y0 = rect1.getY();
594:
595: final double x = rect2.getX();
596: final double width = rect2.getWidth();
597: final double y = rect2.getY();
598: final double height = rect2.getHeight();
599: return (x + width >= x0 && y + height >= y0
600: && x <= x0 + rect1.getWidth() && y <= y0
601: + rect1.getHeight());
602: }
603: }
|