KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DialCap.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: DialCap.java,v 1.1.2.2 2006/11/06 16:26:06 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.Ellipse2D JavaDoc;
51 import java.awt.geom.Rectangle2D JavaDoc;
52 import java.io.IOException JavaDoc;
53 import java.io.ObjectInputStream JavaDoc;
54 import java.io.ObjectOutputStream JavaDoc;
55 import java.io.Serializable JavaDoc;
56
57 import org.jfree.chart.HashUtilities;
58 import org.jfree.io.SerialUtilities;
59 import org.jfree.util.PaintUtilities;
60 import org.jfree.util.PublicCloneable;
61
62 /**
63  * A regular dial layer that can be used to draw a cap over the center of
64  * the dial (the base of the dial pointer(s)).
65  */

66 public class DialCap extends AbstractDialLayer implements DialLayer, Cloneable JavaDoc,
67         PublicCloneable, Serializable JavaDoc {
68     
69     /**
70      * The radius of the cap, as a percentage of the framing rectangle.
71      */

72     private double radius;
73     
74     /**
75      * The fill paint. This field is transient because it requires special
76      * handling for serialization.
77      */

78     private transient Paint JavaDoc fillPaint;
79     
80     /**
81      * The paint used to draw the cap outline (this should never be
82      * <code>null</code>). This field is transient because it requires
83      * special handling for serialization.
84      */

85     private transient Paint JavaDoc outlinePaint;
86     
87     /**
88      * The stroke used to draw the cap outline (this should never be
89      * <code>null</code>). This field is transient because it requires
90      * special handling for serialization.
91      */

92     private transient Stroke JavaDoc outlineStroke;
93     
94     /**
95      * Creates a new instance of <code>StandardDialBackground</code>. The
96      * default background paint is <code>Color.white</code>.
97      */

98     public DialCap() {
99         this.radius = 0.05;
100         this.fillPaint = Color.white;
101         this.outlinePaint = Color.black;
102         this.outlineStroke = new BasicStroke JavaDoc(2.0f);
103     }
104     
105     /**
106      * Returns the radius of the cap, as a percentage of the dial's framing
107      * rectangle.
108      *
109      * @return The radius.
110      *
111      * @see #setRadius(double)
112      */

113     public double getRadius() {
114         return this.radius;
115     }
116     
117     /**
118      * Sets the radius of the cap, as a percentage of the dial's framing
119      * rectangle.
120      *
121      * @param radius the radius.
122      *
123      * @see #getRadius()
124      */

125     public void setRadius(double radius) {
126         // TODO: validation
127
this.radius = radius;
128         notifyListeners(new DialLayerChangeEvent(this));
129     }
130     
131     /**
132      * Returns the paint used to fill the cap.
133      *
134      * @return The paint (never <code>null</code>).
135      *
136      * @see #setFillPaint(Paint)
137      */

138     public Paint JavaDoc getFillPaint() {
139         return this.fillPaint;
140     }
141     
142     /**
143      * Sets the paint for the cap background.
144      *
145      * @param paint the paint (<code>null</code> not permitted).
146      *
147      * @see #getFillPaint()
148      */

149     public void setFillPaint(Paint JavaDoc paint) {
150         if (paint == null) {
151             throw new IllegalArgumentException JavaDoc("Null 'paint' argument.");
152         }
153         this.fillPaint = paint;
154         notifyListeners(new DialLayerChangeEvent(this));
155     }
156         
157     /**
158      * Returns the paint used to draw the outline of the cap.
159      *
160      * @return The paint (never <code>null</code>).
161      *
162      * @see #setOutlinePaint(Paint)
163      */

164     public Paint JavaDoc getOutlinePaint() {
165         return this.outlinePaint;
166     }
167     
168     /**
169      * Sets the paint used to draw the outline of the cap.
170      *
171      * @param paint the paint (<code>null</code> not permitted).
172      *
173      * @see #getOutlinePaint()
174      */

175     public void setOutlinePaint(Paint JavaDoc paint) {
176         if (paint == null) {
177             throw new IllegalArgumentException JavaDoc("Null 'paint' argument.");
178         }
179         this.outlinePaint = paint;
180         notifyListeners(new DialLayerChangeEvent(this));
181     }
182         
183     /**
184      * Returns the stroke used to draw the outline of the cap.
185      *
186      * @return The stroke (never <code>null</code>).
187      *
188      * @see #setOutlineStroke(Stroke)
189      */

190     public Stroke JavaDoc getOutlineStroke() {
191         return this.outlineStroke;
192     }
193     
194     /**
195      * Sets the stroke used to draw the outline of the cap and sends a
196      * {@link DialLayerChangeEvent} to all registered listeners.
197      *
198      * @param stroke the stroke (<code>null</code> not permitted).
199      *
200      * @see #getOutlineStroke()
201      */

202     public void setOutlineStroke(Stroke JavaDoc stroke) {
203         if (stroke == null) {
204             throw new IllegalArgumentException JavaDoc("Null 'stroke' argument.");
205         }
206         this.outlineStroke = stroke;
207         notifyListeners(new DialLayerChangeEvent(this));
208     }
209     
210     /**
211      * Returns <code>true</code> to indicate that this layer should be
212      * clipped within the dial window.
213      *
214      * @return <code>true</code>.
215      */

216     public boolean isClippedToWindow() {
217         return true;
218     }
219     
220     /**
221      * Draws the background to the specified graphics device. If the dial
222      * frame specifies a window, the clipping region will already have been
223      * set to this window before this method is called.
224      *
225      * @param g2 the graphics device (<code>null</code> not permitted).
226      * @param plot the plot (ignored here).
227      * @param frame the dial frame (ignored here).
228      * @param view the view rectangle (<code>null</code> not permitted).
229      */

230     public void draw(Graphics2D JavaDoc g2, DialPlot plot, Rectangle2D JavaDoc frame,
231             Rectangle2D JavaDoc view) {
232
233         g2.setPaint(this.fillPaint);
234         
235         Rectangle2D JavaDoc f = DialPlot.rectangleByRadius(frame, this.radius,
236                 this.radius);
237         Ellipse2D JavaDoc e = new Ellipse2D.Double JavaDoc(f.getX(), f.getY(), f.getWidth(),
238                 f.getHeight());
239         g2.fill(e);
240         g2.setPaint(this.outlinePaint);
241         g2.setStroke(this.outlineStroke);
242         g2.draw(e);
243         
244     }
245     
246     /**
247      * Tests this instance for equality with an arbitrary object.
248      *
249      * @param obj the object (<code>null</code> permitted).
250      *
251      * @return A boolean.
252      */

253     public boolean equals(Object JavaDoc obj) {
254         if (obj == this) {
255             return true;
256         }
257         if (!(obj instanceof DialCap)) {
258             return false;
259         }
260         DialCap that = (DialCap) obj;
261         if (this.radius != that.radius) {
262             return false;
263         }
264         if (!PaintUtilities.equal(this.fillPaint, that.fillPaint)) {
265             return false;
266         }
267         if (!PaintUtilities.equal(this.outlinePaint, that.outlinePaint)) {
268             return false;
269         }
270         if (!this.outlineStroke.equals(that.outlineStroke)) {
271             return false;
272         }
273         return true;
274     }
275     
276     /**
277      * Returns a hash code for this instance.
278      *
279      * @return The hash code.
280      */

281     public int hashCode() {
282         int result = 193;
283         result = 37 * result + HashUtilities.hashCodeForPaint(this.fillPaint);
284         result = 37 * result + HashUtilities.hashCodeForPaint(
285                 this.outlinePaint);
286         result = 37 * result + this.outlineStroke.hashCode();
287         return result;
288     }
289     
290     /**
291      * Returns a clone of this instance.
292      *
293      * @return A clone.
294      *
295      * @throws CloneNotSupportedException if some attribute of the cap cannot
296      * be cloned.
297      */

298     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
299         return super.clone();
300     }
301     
302     /**
303      * Provides serialization support.
304      *
305      * @param stream the output stream.
306      *
307      * @throws IOException if there is an I/O error.
308      */

309     private void writeObject(ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
310         stream.defaultWriteObject();
311         SerialUtilities.writePaint(this.fillPaint, stream);
312         SerialUtilities.writePaint(this.outlinePaint, stream);
313         SerialUtilities.writeStroke(this.outlineStroke, stream);
314     }
315
316     /**
317      * Provides serialization support.
318      *
319      * @param stream the input stream.
320      *
321      * @throws IOException if there is an I/O error.
322      * @throws ClassNotFoundException if there is a classpath problem.
323      */

324     private void readObject(ObjectInputStream JavaDoc stream)
325             throws IOException JavaDoc, ClassNotFoundException JavaDoc {
326         stream.defaultReadObject();
327         this.fillPaint = SerialUtilities.readPaint(stream);
328         this.outlinePaint = SerialUtilities.readPaint(stream);
329         this.outlineStroke = SerialUtilities.readStroke(stream);
330     }
331     
332 }
333
334
Popular Tags