KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DialBackground.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: DialBackground.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.Color JavaDoc;
46 import java.awt.GradientPaint JavaDoc;
47 import java.awt.Graphics2D JavaDoc;
48 import java.awt.Paint JavaDoc;
49 import java.awt.geom.Rectangle2D JavaDoc;
50 import java.io.IOException JavaDoc;
51 import java.io.ObjectInputStream JavaDoc;
52 import java.io.ObjectOutputStream JavaDoc;
53 import java.io.Serializable JavaDoc;
54
55 import org.jfree.chart.HashUtilities;
56 import org.jfree.io.SerialUtilities;
57 import org.jfree.ui.GradientPaintTransformer;
58 import org.jfree.ui.StandardGradientPaintTransformer;
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 the background for a dial.
64  */

65 public class DialBackground extends AbstractDialLayer implements DialLayer,
66         Cloneable JavaDoc, PublicCloneable, Serializable JavaDoc {
67     
68     /**
69      * The background paint. This field is transient because serialization
70      * requires special handling.
71      */

72     private transient Paint JavaDoc paint;
73     
74     /**
75      * The transformer used when the background paint is an instance of
76      * <code>GradientPaint</code>.
77      */

78     private GradientPaintTransformer gradientPaintTransformer;
79     
80     /**
81      * Creates a new instance of <code>DialBackground</code>. The
82      * default background paint is <code>Color.white</code>.
83      */

84     public DialBackground() {
85         this(Color.white);
86     }
87     
88     /**
89      * Creates a new instance of <code>DialBackground</code>. The
90      *
91      * @param paint the paint (<code>null</code> not permitted).
92      *
93      * @throws IllegalArgumentException if <code>paint</code> is
94      * <code>null</code>.
95      */

96     public DialBackground(Paint JavaDoc paint) {
97         if (paint == null) {
98             throw new IllegalArgumentException JavaDoc("Null 'paint' argument.");
99         }
100         this.paint = paint;
101         this.gradientPaintTransformer = new StandardGradientPaintTransformer();
102     }
103     
104     /**
105      * Returns the paint used to fill the background.
106      *
107      * @return The paint (never <code>null</code>).
108      *
109      * @see #setPaint(Paint)
110      */

111     public Paint JavaDoc getPaint() {
112         return this.paint;
113     }
114     
115     /**
116      * Sets the paint for the dial background.
117      *
118      * @param paint the paint (<code>null</code> not permitted).
119      *
120      * @see #getPaint()
121      */

122     public void setPaint(Paint JavaDoc paint) {
123         if (paint == null) {
124             throw new IllegalArgumentException JavaDoc("Null 'paint' argument.");
125         }
126         this.paint = paint;
127         notifyListeners(new DialLayerChangeEvent(this));
128     }
129     
130     /**
131      * Returns the transformer used to adjust the coordinates of any
132      * <code>GradientPaint</code> instance used for the background paint.
133      *
134      * @return The transformer (never <code>null</code>).
135      *
136      * @see #setGradientPaintTransformer(GradientPaintTransformer)
137      */

138     public GradientPaintTransformer getGradientPaintTransformer() {
139         return this.gradientPaintTransformer;
140     }
141     
142     /**
143      * Sets the transformer used to adjust the coordinates of any
144      * <code>GradientPaint</code> instance used for the background paint.
145      *
146      * @param t the transformer (<code>null</code> not permitted).
147      *
148      * @see #getGradientPaintTransformer()
149      */

150     public void setGradientPaintTransformer(GradientPaintTransformer t) {
151         if (t == null) {
152             throw new IllegalArgumentException JavaDoc("Null 't' argument.");
153         }
154         this.gradientPaintTransformer = t;
155         notifyListeners(new DialLayerChangeEvent(this));
156     }
157     
158     /**
159      * Returns <code>true</code> to indicate that this layer should be
160      * clipped within the dial window.
161      *
162      * @return <code>true</code>.
163      */

164     public boolean isClippedToWindow() {
165         return true;
166     }
167     
168     /**
169      * Draws the background to the specified graphics device. If the dial
170      * frame specifies a window, the clipping region will already have been
171      * set to this window before this method is called.
172      *
173      * @param g2 the graphics device (<code>null</code> not permitted).
174      * @param plot the plot (ignored here).
175      * @param frame the dial frame (ignored here).
176      * @param view the view rectangle (<code>null</code> not permitted).
177      */

178     public void draw(Graphics2D JavaDoc g2, DialPlot plot, Rectangle2D JavaDoc frame,
179             Rectangle2D JavaDoc view) {
180
181         Paint JavaDoc p = this.paint;
182         if (p instanceof GradientPaint JavaDoc) {
183             p = this.gradientPaintTransformer.transform((GradientPaint JavaDoc) p, view);
184         }
185         g2.setPaint(p);
186         g2.fill(view);
187     }
188     
189     /**
190      * Tests this instance for equality with an arbitrary object.
191      *
192      * @param obj the object (<code>null</code> permitted).
193      *
194      * @return A boolean.
195      */

196     public boolean equals(Object JavaDoc obj) {
197         if (obj == this) {
198             return true;
199         }
200         if (!(obj instanceof DialBackground)) {
201             return false;
202         }
203         DialBackground that = (DialBackground) obj;
204         if (!PaintUtilities.equal(this.paint, that.paint)) {
205             return false;
206         }
207         if (!this.gradientPaintTransformer.equals(
208                 that.gradientPaintTransformer)) {
209             return false;
210         }
211         return true;
212     }
213     
214     /**
215      * Returns a hash code for this instance.
216      *
217      * @return The hash code.
218      */

219     public int hashCode() {
220         int result = 193;
221         result = 37 * result + HashUtilities.hashCodeForPaint(paint);
222         result = 37 * result + this.gradientPaintTransformer.hashCode();
223         return result;
224     }
225     
226     /**
227      * Returns a clone of this instance.
228      *
229      * @return The clone.
230      *
231      * @throws CloneNotSupportedException if some attribute of this instance
232      * cannot be cloned.
233      */

234     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
235         return super.clone();
236     }
237     
238     /**
239      * Provides serialization support.
240      *
241      * @param stream the output stream.
242      *
243      * @throws IOException if there is an I/O error.
244      */

245     private void writeObject(ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
246         stream.defaultWriteObject();
247         SerialUtilities.writePaint(this.paint, stream);
248     }
249
250     /**
251      * Provides serialization support.
252      *
253      * @param stream the input stream.
254      *
255      * @throws IOException if there is an I/O error.
256      * @throws ClassNotFoundException if there is a classpath problem.
257      */

258     private void readObject(ObjectInputStream JavaDoc stream)
259             throws IOException JavaDoc, ClassNotFoundException JavaDoc {
260         stream.defaultReadObject();
261         this.paint = SerialUtilities.readPaint(stream);
262     }
263     
264 }
265
Popular Tags