001: /*
002: * $Id: Star2D.java,v 1.1 2006/10/22 03:26:22 gfx Exp $
003: *
004: * Dual-licensed under LGPL (Sun and Romain Guy) and BSD (Romain Guy).
005: *
006: * Copyright 2005 Sun Microsystems, Inc., 4150 Network Circle,
007: * Santa Clara, California 95054, U.S.A. All rights reserved.
008: *
009: * Copyright (c) 2006 Romain Guy <romain.guy@mac.com>
010: * All rights reserved.
011: *
012: * Redistribution and use in source and binary forms, with or without
013: * modification, are permitted provided that the following conditions
014: * are met:
015: * 1. Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: * 2. Redistributions in binary form must reproduce the above copyright
018: * notice, this list of conditions and the following disclaimer in the
019: * documentation and/or other materials provided with the distribution.
020: * 3. The name of the author may not be used to endorse or promote products
021: * derived from this software without specific prior written permission.
022: *
023: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
024: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
025: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
026: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
027: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
028: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
029: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
030: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
031: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
032: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
033: */
034:
035: package org.jdesktop.swingx.geom;
036:
037: import java.awt.Rectangle;
038: import java.awt.Shape;
039: import java.awt.geom.AffineTransform;
040: import java.awt.geom.GeneralPath;
041: import java.awt.geom.PathIterator;
042: import java.awt.geom.Point2D;
043: import java.awt.geom.Rectangle2D;
044:
045: /**
046: * <p>This class provides a star shape. A star is defined by two radii and a
047: * number of branches. Each branch spans between the two radii. The inner
048: * radius is the distance between the center of the star and the origin of the
049: * branches. The outer radius is the distance between the center of the star
050: * and the tips of the branches.</p>
051: *
052: * @author Romain Guy <romain.guy@mac.com>
053: */
054:
055: public class Star2D implements Shape {
056: private Shape starShape;
057: private double x;
058: private double y;
059: private double innerRadius;
060: private double outerRadius;
061: private int branchesCount;
062:
063: /**
064: * <p>Creates a new star whose center is located at the specified
065: * <code>x</code> and <code>y</code> coordinates. The number of branches
066: * and their length can be specified.</p>
067: *
068: * @param x the location of the star center
069: * @param y the location of the star center
070: * @param innerRadius the distance between the center of the star and the
071: * origin of the branches
072: * @param outerRadius the distance between the center of the star and the
073: * tip of the branches
074: * @param branchesCount the number of branches in this star; must be >= 3
075: * @throws IllegalArgumentException if <code>branchesCount<code> is < 3 or
076: * if <code>innerRadius</code> is >= <code>outerRadius</code>
077: */
078: public Star2D(double x, double y, double innerRadius,
079: double outerRadius, int branchesCount) {
080: if (branchesCount <= 2) {
081: throw new IllegalArgumentException(
082: "The number of branches must" + " be >= 3.");
083: } else if (innerRadius >= outerRadius) {
084: throw new IllegalArgumentException(
085: "The inner radius must be < " + "outer radius.");
086: }
087:
088: this .x = x;
089: this .y = y;
090: this .innerRadius = innerRadius;
091: this .outerRadius = outerRadius;
092: this .branchesCount = branchesCount;
093:
094: starShape = generateStar(x, y, innerRadius, outerRadius,
095: branchesCount);
096: }
097:
098: private static Shape generateStar(double x, double y,
099: double innerRadius, double outerRadius, int branchesCount) {
100: GeneralPath path = new GeneralPath();
101:
102: double outerAngleIncrement = 2 * Math.PI / branchesCount;
103:
104: double outerAngle = 0.0;
105: double innerAngle = outerAngleIncrement / 2.0;
106:
107: x += outerRadius;
108: y += outerRadius;
109:
110: float x1 = (float) (Math.cos(outerAngle) * outerRadius + x);
111: float y1 = (float) (Math.sin(outerAngle) * outerRadius + y);
112:
113: float x2 = (float) (Math.cos(innerAngle) * innerRadius + x);
114: float y2 = (float) (Math.sin(innerAngle) * innerRadius + y);
115:
116: path.moveTo(x1, y1);
117: path.lineTo(x2, y2);
118:
119: outerAngle += outerAngleIncrement;
120: innerAngle += outerAngleIncrement;
121:
122: for (int i = 1; i < branchesCount; i++) {
123: x1 = (float) (Math.cos(outerAngle) * outerRadius + x);
124: y1 = (float) (Math.sin(outerAngle) * outerRadius + y);
125:
126: path.lineTo(x1, y1);
127:
128: x2 = (float) (Math.cos(innerAngle) * innerRadius + x);
129: y2 = (float) (Math.sin(innerAngle) * innerRadius + y);
130:
131: path.lineTo(x2, y2);
132:
133: outerAngle += outerAngleIncrement;
134: innerAngle += outerAngleIncrement;
135: }
136:
137: path.closePath();
138: return path;
139: }
140:
141: /**
142: * <p>Sets the inner radius of the star, that is the distance between its
143: * center and the origin of the branches. The inner radius must always be
144: * lower than the outer radius.</p>
145: *
146: * @param innerRadius the distance between the center of the star and the
147: * origin of the branches
148: * @throws IllegalArgumentException if the inner radius is >= outer radius
149: */
150: public void setInnerRadius(double innerRadius) {
151: if (innerRadius >= outerRadius) {
152: throw new IllegalArgumentException(
153: "The inner radius must be <" + " outer radius.");
154: }
155:
156: this .innerRadius = innerRadius;
157: starShape = generateStar(getX(), getY(), innerRadius,
158: getOuterRadius(), getBranchesCount());
159: }
160:
161: /**
162: * <p>Sets location of the center of the star.</p>
163: *
164: * @param x the x location of the center of the star
165: */
166: public void setX(double x) {
167: this .x = x;
168: starShape = generateStar(x, getY(), getInnerRadius(),
169: getOuterRadius(), getBranchesCount());
170: }
171:
172: /**
173: * <p>Sets the location of the center of the star.</p>
174: *
175: * @param y the x location of the center of the star
176: */
177: public void setY(double y) {
178: this .y = y;
179: starShape = generateStar(getX(), y, getInnerRadius(),
180: getOuterRadius(), getBranchesCount());
181: }
182:
183: /**
184: * <p>Sets the outer radius of the star, that is the distance between its
185: * center and the tips of the branches. The outer radius must always be
186: * greater than the inner radius.</p>
187: *
188: * @param outerRadius the distance between the center of the star and the
189: * tips of the branches
190: * @throws IllegalArgumentException if the inner radius is >= outer radius
191: */
192: public void setOuterRadius(double outerRadius) {
193: if (innerRadius >= outerRadius) {
194: throw new IllegalArgumentException(
195: "The outer radius must be > " + "inner radius.");
196: }
197:
198: this .outerRadius = outerRadius;
199: starShape = generateStar(getX(), getY(), getInnerRadius(),
200: outerRadius, getBranchesCount());
201: }
202:
203: /**
204: * <p>Sets the number branches of the star. A star must always have at least
205: * 3 branches.</p>
206: *
207: * @param branchesCount the number of branches
208: * @throws IllegalArgumentException if <code>branchesCount</code> is <=2
209: */
210: public void setBranchesCount(int branchesCount) {
211: if (branchesCount <= 2) {
212: throw new IllegalArgumentException(
213: "The number of branches must" + " be >= 3.");
214: }
215:
216: this .branchesCount = branchesCount;
217: starShape = generateStar(getX(), getY(), getInnerRadius(),
218: getOuterRadius(), branchesCount);
219: }
220:
221: /**
222: * <p>Returns the location of the center of star.</p>
223: *
224: * @return the x coordinate of the center of the star
225: */
226: public double getX() {
227: return x;
228: }
229:
230: /**
231: * <p>Returns the location of the center of star.</p>
232: *
233: * @return the y coordinate of the center of the star
234: */
235: public double getY() {
236: return y;
237: }
238:
239: /**
240: * <p>Returns the distance between the center of the star and the origin
241: * of the branches.</p>
242: *
243: * @return the inner radius of the star
244: */
245: public double getInnerRadius() {
246: return innerRadius;
247: }
248:
249: /**
250: * <p>Returns the distance between the center of the star and the tips
251: * of the branches.</p>
252: *
253: * @return the outer radius of the star
254: */
255: public double getOuterRadius() {
256: return outerRadius;
257: }
258:
259: /**
260: * <p>Returns the number of branches of the star.</p>
261: *
262: * @return the number of branches, always >= 3
263: */
264: public int getBranchesCount() {
265: return branchesCount;
266: }
267:
268: /**
269: * {@inheritDoc}
270: */
271: public Rectangle getBounds() {
272: return starShape.getBounds();
273: }
274:
275: /**
276: * {@inheritDoc}
277: */
278: public Rectangle2D getBounds2D() {
279: return starShape.getBounds2D();
280: }
281:
282: /**
283: * {@inheritDoc}
284: */
285: public boolean contains(double x, double y) {
286: return starShape.contains(x, y);
287: }
288:
289: /**
290: * {@inheritDoc}
291: */
292: public boolean contains(Point2D p) {
293: return starShape.contains(p);
294: }
295:
296: /**
297: * {@inheritDoc}
298: */
299: public boolean intersects(double x, double y, double w, double h) {
300: return starShape.intersects(x, y, w, h);
301: }
302:
303: /**
304: * {@inheritDoc}
305: */
306: public boolean intersects(Rectangle2D r) {
307: return starShape.intersects(r);
308: }
309:
310: /**
311: * {@inheritDoc}
312: */
313: public boolean contains(double x, double y, double w, double h) {
314: return starShape.contains(x, y, w, h);
315: }
316:
317: /**
318: * {@inheritDoc}
319: */
320: public boolean contains(Rectangle2D r) {
321: return starShape.contains(r);
322: }
323:
324: /**
325: * {@inheritDoc}
326: */
327: public PathIterator getPathIterator(AffineTransform at) {
328: return starShape.getPathIterator(at);
329: }
330:
331: /**
332: * {@inheritDoc}
333: */
334: public PathIterator getPathIterator(AffineTransform at,
335: double flatness) {
336: return starShape.getPathIterator(at, flatness);
337: }
338: }
|