KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > needle > MeterNeedle


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, 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 License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ----------------
27  * MeterNeedle.java
28  * ----------------
29  * (C) Copyright 2002-2005, by the Australian Antarctic Division and
30  * Contributors.
31  *
32  * Original Author: Bryan Scott (for the Australian Antarctic Division);
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  * Nicolas Brodu (for Astrium and EADS Corporate Research
35  * Center);
36  *
37  * $Id: MeterNeedle.java,v 1.4 2005/05/19 15:42:54 mungady Exp $
38  *
39  * Changes:
40  * --------
41  * 25-Sep-2002 : Version 1, contributed by Bryan Scott (DG);
42  * 07-Nov-2002 : Fixed errors reported by Checkstyle (DG);
43  * 01-Sep-2003 : Implemented Serialization (NB);
44  * 16-Mar-2004 : Changed transform from private to protected (BRS);
45  */

46
47 package org.jfree.chart.needle;
48
49 import java.awt.BasicStroke JavaDoc;
50 import java.awt.Color JavaDoc;
51 import java.awt.Graphics2D JavaDoc;
52 import java.awt.Paint JavaDoc;
53 import java.awt.Shape JavaDoc;
54 import java.awt.Stroke JavaDoc;
55 import java.awt.geom.AffineTransform JavaDoc;
56 import java.awt.geom.Point2D JavaDoc;
57 import java.awt.geom.Rectangle2D JavaDoc;
58 import java.io.IOException JavaDoc;
59 import java.io.ObjectInputStream JavaDoc;
60 import java.io.ObjectOutputStream JavaDoc;
61 import java.io.Serializable JavaDoc;
62
63 import org.jfree.io.SerialUtilities;
64 import org.jfree.util.ObjectUtilities;
65
66 /**
67  * The base class used to represent the needle on a
68  * {@link org.jfree.chart.plot.CompassPlot}.
69  *
70  * @author Bryan Scott
71  */

72 public abstract class MeterNeedle implements Serializable JavaDoc {
73
74     /** For serialization. */
75     private static final long serialVersionUID = 5203064851510951052L;
76     
77     /** The outline paint. */
78     private transient Paint JavaDoc outlinePaint = Color.black;
79
80     /** The outline stroke. */
81     private transient Stroke JavaDoc outlineStroke = new BasicStroke JavaDoc(2);
82
83     /** The fill paint. */
84     private transient Paint JavaDoc fillPaint = null;
85
86     /** The highlight paint. */
87     private transient Paint JavaDoc highlightPaint = null;
88
89     /** The size. */
90     private int size = 5;
91
92     /** Scalar to aply to locate the rotation x point. */
93     private double rotateX = 0.5;
94
95     /** Scalar to aply to locate the rotation y point. */
96     private double rotateY = 0.5;
97
98     /** A transform. */
99     protected static AffineTransform JavaDoc transform = new AffineTransform JavaDoc();
100
101     /**
102      * Creates a new needle.
103      */

104     public MeterNeedle() {
105         this(null, null, null);
106     }
107
108     /**
109      * Creates a new needle.
110      *
111      * @param outline the outline paint.
112      * @param fill the fill paint.
113      * @param highlight the highlight paint.
114      */

115     public MeterNeedle(Paint JavaDoc outline, Paint JavaDoc fill, Paint JavaDoc highlight) {
116         this.fillPaint = fill;
117         this.highlightPaint = highlight;
118         this.outlinePaint = outline;
119     }
120
121     /**
122      * Returns the outline paint.
123      *
124      * @return The outline paint.
125      */

126     public Paint JavaDoc getOutlinePaint() {
127         return this.outlinePaint;
128     }
129
130     /**
131      * Sets the outline paint.
132      *
133      * @param p the new paint.
134      */

135     public void setOutlinePaint(Paint JavaDoc p) {
136         if (p != null) {
137             this.outlinePaint = p;
138         }
139     }
140
141     /**
142      * Returns the outline stroke.
143      *
144      * @return The outline stroke.
145      */

146     public Stroke JavaDoc getOutlineStroke() {
147         return this.outlineStroke;
148     }
149
150     /**
151      * Sets the outline stroke.
152      *
153      * @param s the new stroke.
154      */

155     public void setOutlineStroke(Stroke JavaDoc s) {
156         if (s != null) {
157             this.outlineStroke = s;
158         }
159     }
160
161     /**
162      * Returns the fill paint.
163      *
164      * @return The fill paint.
165      */

166     public Paint JavaDoc getFillPaint() {
167         return this.fillPaint;
168     }
169
170     /**
171      * Sets the fill paint.
172      *
173      * @param p the fill paint.
174      */

175     public void setFillPaint(Paint JavaDoc p) {
176         if (p != null) {
177             this.fillPaint = p;
178         }
179     }
180
181     /**
182      * Returns the highlight paint.
183      *
184      * @return The highlight paint.
185      */

186     public Paint JavaDoc getHighlightPaint() {
187         return this.highlightPaint;
188     }
189
190     /**
191      * Sets the highlight paint.
192      *
193      * @param p the highlight paint.
194      */

195     public void setHighlightPaint(Paint JavaDoc p) {
196         if (p != null) {
197             this.highlightPaint = p;
198         }
199     }
200
201     /**
202      * Returns the scalar used for determining the rotation x value.
203      *
204      * @return The x rotate scalar.
205      */

206     public double getRotateX() {
207         return this.rotateX;
208     }
209
210     /**
211      * Sets the rotateX value.
212      *
213      * @param x the new value.
214      */

215     public void setRotateX(double x) {
216         this.rotateX = x;
217     }
218
219     /**
220      * Sets the rotateY value.
221      *
222      * @param y the new value.
223      */

224     public void setRotateY(double y) {
225         this.rotateY = y;
226     }
227
228     /**
229      * Returns the scalar used for determining the rotation y value.
230      *
231      * @return The y rotate scalar.
232      */

233     public double getRotateY() {
234         return this.rotateY;
235     }
236
237     /**
238      * Draws the needle.
239      *
240      * @param g2 the graphics device.
241      * @param plotArea the plot area.
242      */

243     public void draw(Graphics2D JavaDoc g2, Rectangle2D JavaDoc plotArea) {
244         draw(g2, plotArea, 0);
245     }
246
247     /**
248      * Draws the needle.
249      *
250      * @param g2 the graphics device.
251      * @param plotArea the plot area.
252      * @param angle the angle.
253      */

254     public void draw(Graphics2D JavaDoc g2, Rectangle2D JavaDoc plotArea, double angle) {
255
256         Point2D.Double JavaDoc pt = new Point2D.Double JavaDoc();
257         pt.setLocation(
258             plotArea.getMinX() + this.rotateX * plotArea.getWidth(),
259             plotArea.getMinY() + this.rotateY * plotArea.getHeight()
260         );
261         draw(g2, plotArea, pt, angle);
262
263     }
264
265     /**
266      * Draws the needle.
267      *
268      * @param g2 the graphics device.
269      * @param plotArea the plot area.
270      * @param rotate the rotation point.
271      * @param angle the angle.
272      */

273     public void draw(Graphics2D JavaDoc g2, Rectangle2D JavaDoc plotArea, Point2D JavaDoc rotate,
274                      double angle) {
275
276         Paint JavaDoc savePaint = g2.getColor();
277         Stroke JavaDoc saveStroke = g2.getStroke();
278
279         drawNeedle(g2, plotArea, rotate, Math.toRadians(angle));
280
281         g2.setStroke(saveStroke);
282         g2.setPaint(savePaint);
283
284     }
285
286     /**
287      * Draws the needle.
288      *
289      * @param g2 the graphics device.
290      * @param plotArea the plot area.
291      * @param rotate the rotation point.
292      * @param angle the angle.
293      */

294     protected abstract void drawNeedle(Graphics2D JavaDoc g2,
295                                        Rectangle2D JavaDoc plotArea, Point2D JavaDoc rotate,
296                                        double angle);
297
298     /**
299      * Displays a shape.
300      *
301      * @param g2 the graphics device.
302      * @param shape the shape.
303      */

304     protected void defaultDisplay(Graphics2D JavaDoc g2, Shape JavaDoc shape) {
305
306         if (this.fillPaint != null) {
307             g2.setPaint(this.fillPaint);
308             g2.fill(shape);
309         }
310
311         if (this.outlinePaint != null) {
312             g2.setStroke(this.outlineStroke);
313             g2.setPaint(this.outlinePaint);
314             g2.draw(shape);
315         }
316
317     }
318
319     /**
320      * Returns the size.
321      *
322      * @return The size.
323      */

324     public int getSize() {
325         return this.size;
326     }
327
328     /**
329      * Sets the size.
330      *
331      * @param pixels the new size.
332      */

333     public void setSize(int pixels) {
334         this.size = pixels;
335     }
336
337     /**
338      * Returns the transform.
339      *
340      * @return The transform.
341      */

342     public AffineTransform JavaDoc getTransform() {
343         return MeterNeedle.transform;
344     }
345
346     /**
347      * Tests another object for equality with this object.
348      *
349      * @param object the object to test.
350      *
351      * @return A boolean.
352      */

353     public boolean equals(Object JavaDoc object) {
354         if (object == this) {
355             return true;
356         }
357         if (!(object instanceof MeterNeedle)) {
358             return false;
359         }
360
361         MeterNeedle that = (MeterNeedle) object;
362         if (!ObjectUtilities.equal(this.outlinePaint, that.outlinePaint)) {
363             return false;
364         }
365         if (!ObjectUtilities.equal(this.outlineStroke, that.outlineStroke)) {
366             return false;
367         }
368         if (!ObjectUtilities.equal(this.fillPaint, that.fillPaint)) {
369             return false;
370         }
371         if (!ObjectUtilities.equal(this.highlightPaint, that.highlightPaint)) {
372             return false;
373         }
374         if (this.size != that.size) {
375             return false;
376         }
377         if (this.rotateX != that.rotateX) {
378             return false;
379         }
380         if (this.rotateY != that.rotateY) {
381             return false;
382         }
383         return true;
384     }
385
386     /**
387      * Provides serialization support.
388      *
389      * @param stream the output stream.
390      *
391      * @throws IOException if there is an I/O error.
392      */

393     private void writeObject(ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
394         stream.defaultWriteObject();
395         SerialUtilities.writeStroke(this.outlineStroke, stream);
396         SerialUtilities.writePaint(this.outlinePaint, stream);
397         SerialUtilities.writePaint(this.fillPaint, stream);
398         SerialUtilities.writePaint(this.highlightPaint, stream);
399     }
400
401     /**
402      * Provides serialization support.
403      *
404      * @param stream the input stream.
405      *
406      * @throws IOException if there is an I/O error.
407      * @throws ClassNotFoundException if there is a classpath problem.
408      */

409     private void readObject(ObjectInputStream JavaDoc stream)
410         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
411         stream.defaultReadObject();
412         this.outlineStroke = SerialUtilities.readStroke(stream);
413         this.outlinePaint = SerialUtilities.readPaint(stream);
414         this.fillPaint = SerialUtilities.readPaint(stream);
415         this.highlightPaint = SerialUtilities.readPaint(stream);
416     }
417
418 }
419
Popular Tags