KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > experimental > chart > plot > dial > DialPointer


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ----------------
28  * DialPointer.java
29  * ----------------
30  * (C) Copyright 2006, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: DialPointer.java,v 1.1.2.3 2006/11/07 16:11:12 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 03-Nov-2006 : Version 1 (DG);
40  *
41  */

42
43 package org.jfree.experimental.chart.plot.dial;
44
45 import java.awt.BasicStroke JavaDoc;
46 import java.awt.Color JavaDoc;
47 import java.awt.Graphics2D JavaDoc;
48 import java.awt.Paint JavaDoc;
49 import java.awt.Stroke JavaDoc;
50 import java.awt.geom.Arc2D JavaDoc;
51 import java.awt.geom.GeneralPath JavaDoc;
52 import java.awt.geom.Line2D JavaDoc;
53 import java.awt.geom.Point2D JavaDoc;
54 import java.awt.geom.Rectangle2D JavaDoc;
55 import java.io.Serializable JavaDoc;
56
57 import org.jfree.util.PublicCloneable;
58
59 /**
60  * A base class for the pointer in a {@link DialPlot}.
61  */

62 public abstract class DialPointer extends AbstractDialLayer
63         implements DialLayer, Cloneable JavaDoc, Serializable JavaDoc {
64     
65     /** The needle radius. */
66     double radius;
67     
68     /**
69      * The dataset index for the needle.
70      */

71     int datasetIndex;
72     
73     /**
74      * Creates a new <code>DialNeedle</code> instance.
75      */

76     public DialPointer() {
77         this(0);
78     }
79     
80     /**
81      * Creates a new pointer for the specified dataset.
82      *
83      * @param datasetIndex the dataset index.
84      */

85     public DialPointer(int datasetIndex) {
86         this.radius = 0.675;
87         this.datasetIndex = datasetIndex;
88     }
89     
90     /**
91      * Returns the dataset index that the pointer maps to.
92      *
93      * @return The dataset index.
94      */

95     public int getDatasetIndex() {
96         return this.datasetIndex;
97     }
98     
99     /**
100      * Sets the dataset index for the pointer.
101      *
102      * @param index the index.
103      */

104     public void setDatasetIndex(int index) {
105         this.datasetIndex = index;
106         notifyListeners(new DialLayerChangeEvent(this));
107     }
108     
109     /**
110      * Returns the radius of the pointer.
111      *
112      * @return The radius.
113      *
114      * @see #setRadius(double)
115      */

116     public double getRadius() {
117         return this.radius;
118     }
119     
120     /**
121      * Sets the radius of the pointer.
122      *
123      * @param radius the radius.
124      *
125      * @see #getRadius()
126      */

127     public void setRadius(double radius) {
128         this.radius = radius;
129         notifyListeners(new DialLayerChangeEvent(this));
130     }
131     
132     /**
133      * Returns <code>true</code> to indicate that this layer should be
134      * clipped within the dial window.
135      *
136      * @return <code>true</code>.
137      */

138     public boolean isClippedToWindow() {
139         return true;
140     }
141     
142     /**
143      * A dial pointer that draws a thin line (like a pin).
144      */

145     public static class Pin extends DialPointer implements PublicCloneable {
146     
147         /** The paint. */
148         private transient Paint JavaDoc paint;
149     
150         /** The stroke. */
151         private transient Stroke JavaDoc stroke;
152         
153         /**
154          * Creates a new instance.
155          */

156         public Pin() {
157             this(0);
158         }
159         
160         /**
161          * Creates a new instance.
162          *
163          * @param datasetIndex the dataset index.
164          */

165         public Pin(int datasetIndex) {
166             super(datasetIndex);
167             this.paint = Color.red;
168             this.stroke = new BasicStroke JavaDoc(3.0f, BasicStroke.CAP_ROUND,
169                     BasicStroke.JOIN_BEVEL);
170         }
171         
172         /**
173          * Returns the paint.
174          *
175          * @return The paint.
176          */

177         public Paint JavaDoc getPaint() {
178             return this.paint;
179         }
180         
181         /**
182          * Sets the paint.
183          *
184          * @param paint the paint (<code>null</code> not permitted).
185          */

186         public void setPaint(Paint JavaDoc paint) {
187             this.paint = paint;
188             notifyListeners(new DialLayerChangeEvent(this));
189         }
190         
191         /**
192          * Returns the stroke.
193          *
194          * @return The stroke.
195          */

196         public Stroke JavaDoc getStroke() {
197             return this.stroke;
198         }
199         
200         /**
201          * Sets the stroke.
202          *
203          * @param stroke the stroke (<code>null</code> not permitted).
204          */

205         public void setStroke(Stroke JavaDoc stroke) {
206             this.stroke = stroke;
207             notifyListeners(new DialLayerChangeEvent(this));
208         }
209         
210         /**
211          * Draws the pointer.
212          *
213          * @param g2 the graphics target.
214          * @param plot the plot.
215          * @param frame the dial's reference frame.
216          * @param view the dial's view.
217          */

218         public void draw(Graphics2D JavaDoc g2, DialPlot plot, Rectangle2D JavaDoc frame,
219             Rectangle2D JavaDoc view) {
220         
221             g2.setPaint(this.paint);
222             g2.setStroke(this.stroke);
223             Rectangle2D JavaDoc arcRect = DialPlot.rectangleByRadius(frame,
224                     this.radius, this.radius);
225
226             double value = plot.getValue(this.datasetIndex);
227             DialScale scale = plot.getScaleForDataset(this.datasetIndex);
228             double angle = scale.valueToAngle(value);
229         
230             Arc2D JavaDoc arc = new Arc2D.Double JavaDoc(arcRect, angle, 0, Arc2D.OPEN);
231             Point2D JavaDoc pt = arc.getEndPoint();
232         
233             Line2D JavaDoc line = new Line2D.Double JavaDoc(frame.getCenterX(),
234                     frame.getCenterY(), pt.getX(), pt.getY());
235             g2.draw(line);
236         }
237         
238         /**
239          * Returns a clone of the pointer.
240          *
241          * @return a clone.
242          *
243          * @throws CloneNotSupportedException if one of the attributes cannot
244          * be cloned.
245          */

246         public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
247             return super.clone();
248         }
249         
250     }
251     
252     /**
253      * A dial pointer.
254      */

255     public static class Pointer extends DialPointer implements PublicCloneable {
256         
257         /**
258          * The radius that defines the width of the pointer at the base.
259          */

260         private double widthRadius;
261     
262         /**
263          * Creates a new instance.
264          */

265         public Pointer() {
266             this(0);
267         }
268         
269         /**
270          * Creates a new instance.
271          *
272          * @param datasetIndex the dataset index.
273          */

274         public Pointer(int datasetIndex) {
275             super(datasetIndex);
276             this.radius = 0.9;
277             this.widthRadius = 0.05;
278         }
279         
280         /**
281          * Returns the width radius.
282          *
283          * @return The width radius.
284          */

285         public double getWidthRadius() {
286             return this.widthRadius;
287         }
288         
289         /**
290          * Sets the width radius.
291          *
292          * @param radius the radius.
293          */

294         public void setWidthRadius(double radius) {
295             this.widthRadius = radius;
296             notifyListeners(new DialLayerChangeEvent(this));
297         }
298         
299         /**
300          * Draws the pointer.
301          *
302          * @param g2 the graphics target.
303          * @param plot the plot.
304          * @param frame the dial's reference frame.
305          * @param view the dial's view.
306          */

307         public void draw(Graphics2D JavaDoc g2, DialPlot plot, Rectangle2D JavaDoc frame,
308                 Rectangle2D JavaDoc view) {
309         
310             g2.setPaint(Color.blue);
311             g2.setStroke(new BasicStroke JavaDoc(1.0f));
312             Rectangle2D JavaDoc lengthRect = DialPlot.rectangleByRadius(frame,
313                     this.radius, this.radius);
314             Rectangle2D JavaDoc widthRect = DialPlot.rectangleByRadius(frame,
315                     this.widthRadius, this.widthRadius);
316             double value = plot.getValue(this.datasetIndex);
317             DialScale scale = plot.getScaleForDataset(this.datasetIndex);
318             double angle = scale.valueToAngle(value);
319         
320             Arc2D JavaDoc arc1 = new Arc2D.Double JavaDoc(lengthRect, angle, 0, Arc2D.OPEN);
321             Point2D JavaDoc pt1 = arc1.getEndPoint();
322             Arc2D JavaDoc arc2 = new Arc2D.Double JavaDoc(widthRect, angle - 90.0, 180.0,
323                     Arc2D.OPEN);
324             Point2D JavaDoc pt2 = arc2.getStartPoint();
325             Point2D JavaDoc pt3 = arc2.getEndPoint();
326             Arc2D JavaDoc arc3 = new Arc2D.Double JavaDoc(widthRect, angle - 180.0, 0.0,
327                     Arc2D.OPEN);
328             Point2D JavaDoc pt4 = arc3.getStartPoint();
329         
330             GeneralPath JavaDoc gp = new GeneralPath JavaDoc();
331             gp.moveTo((float) pt1.getX(), (float) pt1.getY());
332             gp.lineTo((float) pt2.getX(), (float) pt2.getY());
333             gp.lineTo((float) pt4.getX(), (float) pt4.getY());
334             gp.lineTo((float) pt3.getX(), (float) pt3.getY());
335             gp.closePath();
336             g2.setPaint(Color.gray);
337             g2.fill(gp);
338         
339             g2.setPaint(Color.black);
340             Line2D JavaDoc line = new Line2D.Double JavaDoc(frame.getCenterX(),
341                     frame.getCenterY(), pt1.getX(), pt1.getY());
342             g2.draw(line);
343         
344             line.setLine(pt2, pt3);
345             g2.draw(line);
346         
347             line.setLine(pt3, pt1);
348             g2.draw(line);
349         
350             line.setLine(pt2, pt1);
351             g2.draw(line);
352         
353             line.setLine(pt2, pt4);
354             g2.draw(line);
355
356             line.setLine(pt3, pt4);
357             g2.draw(line);
358         }
359         
360         public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
361             return super.clone();
362         }
363         
364     }
365
366 }
367
Popular Tags