KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SimpleDialFrame.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: SimpleDialFrame.java,v 1.1.2.2 2006/11/06 16:26:07 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.Shape JavaDoc;
50 import java.awt.Stroke JavaDoc;
51 import java.awt.geom.Area JavaDoc;
52 import java.awt.geom.Ellipse2D JavaDoc;
53 import java.awt.geom.Rectangle2D JavaDoc;
54 import java.io.IOException JavaDoc;
55 import java.io.ObjectInputStream JavaDoc;
56 import java.io.ObjectOutputStream JavaDoc;
57 import java.io.Serializable JavaDoc;
58
59 import org.jfree.chart.HashUtilities;
60 import org.jfree.io.SerialUtilities;
61 import org.jfree.util.PaintUtilities;
62 import org.jfree.util.PublicCloneable;
63
64 /**
65  * A simple circular frame for the {@link DialPlot} class.
66  */

67 public class SimpleDialFrame extends AbstractDialLayer implements DialFrame,
68         Cloneable JavaDoc, PublicCloneable, Serializable JavaDoc {
69     
70     /** The outer radius, relative to the framing rectangle. */
71     private double radius;
72     
73     /**
74      * The color used for the front of the panel. This field is transient
75      * because it requires special handling for serialization.
76      */

77     private transient Paint JavaDoc backgroundPaint;
78     
79     /**
80      * The color used for the border around the window. This field is transient
81      * because it requires special handling for serialization.
82      */

83     private transient Paint JavaDoc foregroundPaint;
84     
85     /**
86      * The stroke for drawing the frame outline. This field is transient
87      * because it requires special handling for serialization.
88      */

89     private transient Stroke JavaDoc stroke;
90     
91     /**
92      * Creates a new instance of <code>SimpleDialFrame</code>.
93      */

94     public SimpleDialFrame() {
95         this.backgroundPaint = Color.gray;
96         this.foregroundPaint = Color.black;
97         this.stroke = new BasicStroke JavaDoc(2.0f);
98         this.radius = 0.95;
99     }
100     
101     /**
102      * Returns the radius, relative to the framing rectangle.
103      *
104      * @return The radius.
105      */

106     public double getRadius() {
107         return this.radius;
108     }
109     
110     /**
111      * Sets the radius.
112      *
113      * @param radius the radius.
114      */

115     public void setRadius(double radius) {
116         // TODO: validation
117
this.radius = radius;
118         notifyListeners(new DialLayerChangeEvent(this));
119     }
120
121     /**
122      * Returns the background paint.
123      *
124      * @return The background paint (never <code>null</code>).
125      *
126      * @see #setBackgroundPaint(Paint)
127      */

128     public Paint JavaDoc getBackgroundPaint() {
129         return this.backgroundPaint;
130     }
131     
132     /**
133      * Sets the background paint.
134      *
135      * @param paint the paint (<code>null</code> not permitted).
136      *
137      * @see #getBackgroundPaint()
138      */

139     public void setBackgroundPaint(Paint JavaDoc paint) {
140         if (paint == null) {
141             throw new IllegalArgumentException JavaDoc("Null 'paint' argument.");
142         }
143         this.backgroundPaint = paint;
144         notifyListeners(new DialLayerChangeEvent(this));
145     }
146     
147     /**
148      * Returns the foreground paint.
149      *
150      * @return The foreground paint (never <code>null</code>).
151      *
152      * @see #setForegroundPaint(Paint)
153      */

154     public Paint JavaDoc getForegroundPaint() {
155         return this.foregroundPaint;
156     }
157     
158     /**
159      * Sets the foreground paint.
160      *
161      * @param paint the paint (<code>null</code> not permitted).
162      *
163      * @see #getForegroundPaint()
164      */

165     public void setForegroundPaint(Paint JavaDoc paint) {
166         if (paint == null) {
167             throw new IllegalArgumentException JavaDoc("Null 'paint' argument.");
168         }
169         this.foregroundPaint = paint;
170         notifyListeners(new DialLayerChangeEvent(this));
171     }
172     
173     /**
174      * Returns the stroke for the frame.
175      *
176      * @return The stroke (never <code>null</code>).
177      *
178      * @see #setStroke(Stroke)
179      */

180     public Stroke JavaDoc getStroke() {
181         return this.stroke;
182     }
183     
184     /**
185      * Sets the stroke.
186      *
187      * @param stroke the stroke (<code>null</code> not permitted).
188      *
189      * @see #getStroke()
190      */

191     public void setStroke(Stroke JavaDoc stroke) {
192         if (stroke == null) {
193             throw new IllegalArgumentException JavaDoc("Null 'stroke' argument.");
194         }
195         this.stroke = stroke;
196         notifyListeners(new DialLayerChangeEvent(this));
197     }
198         
199     /**
200      * Returns the shape for the window for this dial. Some dial layers will
201      * request that their drawing be clipped within this window.
202      *
203      * @param frame the reference frame (<code>null</code> not permitted).
204      *
205      * @return The shape of the dial's window.
206      */

207     public Shape JavaDoc getWindow(Rectangle2D JavaDoc frame) {
208         Rectangle2D JavaDoc f = DialPlot.rectangleByRadius(frame, this.radius,
209                 this.radius);
210         return new Ellipse2D.Double JavaDoc(f.getX(), f.getY(), f.getWidth(),
211                 f.getHeight());
212     }
213      
214     /**
215      * Returns <code>false</code> to indicate that this dial layer is not
216      * clipped to the dial window.
217      *
218      * @return A boolean.
219      */

220     public boolean isClippedToWindow() {
221         return false;
222     }
223     
224     /**
225      * Draws the frame. This method is called by the {@link DialPlot} class,
226      * you shouldn't need to call it directly.
227      *
228      * @param g2 the graphics target (<code>null</code> not permitted).
229      * @param plot the plot (<code>null</code> not permitted).
230      * @param frame the frame (<code>null</code> not permitted).
231      * @param view the view (<code>null</code> not permitted).
232      */

233     public void draw(Graphics2D JavaDoc g2, DialPlot plot, Rectangle2D JavaDoc frame,
234             Rectangle2D JavaDoc view) {
235         
236         Shape JavaDoc window = getWindow(frame);
237         
238         Rectangle2D JavaDoc f = DialPlot.rectangleByRadius(frame, this.radius + 0.02,
239                 this.radius + 0.02);
240         Ellipse2D JavaDoc e = new Ellipse2D.Double JavaDoc(f.getX(), f.getY(), f.getWidth(),
241                 f.getHeight());
242         
243         Area JavaDoc area = new Area JavaDoc(e);
244         Area JavaDoc area2 = new Area JavaDoc(window);
245         area.subtract(area2);
246         g2.setPaint(this.backgroundPaint);
247         g2.fill(area);
248         
249         g2.setStroke(this.stroke);
250         g2.setPaint(this.foregroundPaint);
251         g2.draw(window);
252         g2.draw(e);
253     }
254
255     /**
256      * Tests this instance for equality with an arbitrary object.
257      *
258      * @param obj the object (<code>null</code> permitted).
259      *
260      * @return A boolean.
261      */

262     public boolean equals(Object JavaDoc obj) {
263         if (obj == this) {
264             return true;
265         }
266         if (!(obj instanceof SimpleDialFrame)) {
267             return false;
268         }
269         SimpleDialFrame that = (SimpleDialFrame) obj;
270         if (!PaintUtilities.equal(this.backgroundPaint, that.backgroundPaint)) {
271             return false;
272         }
273         if (!PaintUtilities.equal(this.foregroundPaint, that.foregroundPaint)) {
274             return false;
275         }
276         if (this.radius != that.radius) {
277             return false;
278         }
279         if (!this.stroke.equals(that.stroke)) {
280             return false;
281         }
282         return true;
283     }
284     
285     /**
286      * Returns a hash code for this instance.
287      *
288      * @return The hash code.
289      */

290     public int hashCode() {
291         int result = 193;
292         long temp = Double.doubleToLongBits(this.radius);
293         result = (int) (temp ^ (temp >>> 32));
294         result = 37 * result + HashUtilities.hashCodeForPaint(
295                 this.backgroundPaint);
296         result = 37 * result + HashUtilities.hashCodeForPaint(
297                 this.foregroundPaint);
298         result = 37 * result + this.stroke.hashCode();
299         return result;
300     }
301     
302     /**
303      * Returns a clone of this instance.
304      *
305      * @return A clone.
306      *
307      * @throws CloneNotSupportedException if any of the frame's attributes
308      * cannot be cloned.
309      */

310     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
311         return super.clone();
312     }
313     
314     /**
315      * Provides serialization support.
316      *
317      * @param stream the output stream.
318      *
319      * @throws IOException if there is an I/O error.
320      */

321     private void writeObject(ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
322         stream.defaultWriteObject();
323         SerialUtilities.writePaint(this.backgroundPaint, stream);
324         SerialUtilities.writePaint(this.foregroundPaint, stream);
325         SerialUtilities.writeStroke(this.stroke, stream);
326     }
327
328     /**
329      * Provides serialization support.
330      *
331      * @param stream the input stream.
332      *
333      * @throws IOException if there is an I/O error.
334      * @throws ClassNotFoundException if there is a classpath problem.
335      */

336     private void readObject(ObjectInputStream JavaDoc stream)
337             throws IOException JavaDoc, ClassNotFoundException JavaDoc {
338         stream.defaultReadObject();
339         this.backgroundPaint = SerialUtilities.readPaint(stream);
340         this.foregroundPaint = SerialUtilities.readPaint(stream);
341         this.stroke = SerialUtilities.readStroke(stream);
342     }
343     
344 }
345
Popular Tags