1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2013, 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  * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
25  * Other names may be trademarks of their respective owners.]
26  *
27  * -------------------
28  * DialBackground.java
29  * -------------------
30  * (C) Copyright 2006-2013, by Object Refinery Limited.
31  *
32  * Original Author:  David Gilbert (for Object Refinery Limited);
33  * Contributor(s):   -;
34  *
35  * Changes
36  * -------
37  * 03-Nov-2006 : Version 1 (DG);
38  * 16-Oct-2007 : The equals() method needs to call super.equals() (DG);
39  * 03-Jul-2013 : Use ParamChecks (DG);
40  *
41  */
42 
43 package org.jfree.chart.plot.dial;
44 
45 import java.awt.Color;
46 import java.awt.GradientPaint;
47 import java.awt.Graphics2D;
48 import java.awt.Paint;
49 import java.awt.geom.Rectangle2D;
50 import java.io.IOException;
51 import java.io.ObjectInputStream;
52 import java.io.ObjectOutputStream;
53 import java.io.Serializable;
54 
55 import org.jfree.chart.HashUtilities;
56 import org.jfree.chart.util.ParamChecks;
57 import org.jfree.io.SerialUtilities;
58 import org.jfree.ui.GradientPaintTransformer;
59 import org.jfree.ui.StandardGradientPaintTransformer;
60 import org.jfree.util.PaintUtilities;
61 import org.jfree.util.PublicCloneable;
62 
63 /**
64  * A regular dial layer that can be used to draw the background for a dial.
65  *
66  * @since 1.0.7
67  */
68 public class DialBackground extends AbstractDialLayer implements DialLayer,
69         Cloneable, PublicCloneable, Serializable {
70 
71     /** For serialization. */
72     static final long serialVersionUID = -9019069533317612375L;
73 
74     /**
75      * The background paint.  This field is transient because serialization
76      * requires special handling.
77      */
78     private transient Paint paint;
79 
80     /**
81      * The transformer used when the background paint is an instance of
82      * <code>GradientPaint</code>.
83      */
84     private GradientPaintTransformer gradientPaintTransformer;
85 
86     /**
87      * Creates a new instance of <code>DialBackground</code>.  The
88      * default background paint is <code>Color.white</code>.
89      */
DialBackground()90     public DialBackground() {
91         this(Color.white);
92     }
93 
94     /**
95      * Creates a new instance of <code>DialBackground</code>.  The
96      *
97      * @param paint  the paint (<code>null</code> not permitted).
98      *
99      * @throws IllegalArgumentException if <code>paint</code> is
100      *     <code>null</code>.
101      */
DialBackground(Paint paint)102     public DialBackground(Paint paint) {
103         ParamChecks.nullNotPermitted(paint, "paint");
104         this.paint = paint;
105         this.gradientPaintTransformer = new StandardGradientPaintTransformer();
106     }
107 
108     /**
109      * Returns the paint used to fill the background.
110      *
111      * @return The paint (never <code>null</code>).
112      *
113      * @see #setPaint(Paint)
114      */
getPaint()115     public Paint getPaint() {
116         return this.paint;
117     }
118 
119     /**
120      * Sets the paint for the dial background and sends a
121      * {@link DialLayerChangeEvent} to all registered listeners.
122      *
123      * @param paint  the paint (<code>null</code> not permitted).
124      *
125      * @see #getPaint()
126      */
setPaint(Paint paint)127     public void setPaint(Paint paint) {
128         ParamChecks.nullNotPermitted(paint, "paint");
129         this.paint = paint;
130         notifyListeners(new DialLayerChangeEvent(this));
131     }
132 
133     /**
134      * Returns the transformer used to adjust the coordinates of any
135      * <code>GradientPaint</code> instance used for the background paint.
136      *
137      * @return The transformer (never <code>null</code>).
138      *
139      * @see #setGradientPaintTransformer(GradientPaintTransformer)
140      */
getGradientPaintTransformer()141     public GradientPaintTransformer getGradientPaintTransformer() {
142         return this.gradientPaintTransformer;
143     }
144 
145     /**
146      * Sets the transformer used to adjust the coordinates of any
147      * <code>GradientPaint</code> instance used for the background paint, and
148      * sends a {@link DialLayerChangeEvent} to all registered listeners.
149      *
150      * @param t  the transformer (<code>null</code> not permitted).
151      *
152      * @see #getGradientPaintTransformer()
153      */
setGradientPaintTransformer(GradientPaintTransformer t)154     public void setGradientPaintTransformer(GradientPaintTransformer t) {
155         ParamChecks.nullNotPermitted(t, "t");
156         this.gradientPaintTransformer = t;
157         notifyListeners(new DialLayerChangeEvent(this));
158     }
159 
160     /**
161      * Returns <code>true</code> to indicate that this layer should be
162      * clipped within the dial window.
163      *
164      * @return <code>true</code>.
165      */
166     @Override
isClippedToWindow()167     public boolean isClippedToWindow() {
168         return true;
169     }
170 
171     /**
172      * Draws the background to the specified graphics device.  If the dial
173      * frame specifies a window, the clipping region will already have been
174      * set to this window before this method is called.
175      *
176      * @param g2  the graphics device (<code>null</code> not permitted).
177      * @param plot  the plot (ignored here).
178      * @param frame  the dial frame (ignored here).
179      * @param view  the view rectangle (<code>null</code> not permitted).
180      */
181     @Override
draw(Graphics2D g2, DialPlot plot, Rectangle2D frame, Rectangle2D view)182     public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame,
183             Rectangle2D view) {
184 
185         Paint p = this.paint;
186         if (p instanceof GradientPaint) {
187             p = this.gradientPaintTransformer.transform((GradientPaint) p,
188                     view);
189         }
190         g2.setPaint(p);
191         g2.fill(view);
192     }
193 
194     /**
195      * Tests this instance for equality with an arbitrary object.
196      *
197      * @param obj  the object (<code>null</code> permitted).
198      *
199      * @return A boolean.
200      */
201     @Override
equals(Object obj)202     public boolean equals(Object obj) {
203         if (obj == this) {
204             return true;
205         }
206         if (!(obj instanceof DialBackground)) {
207             return false;
208         }
209         DialBackground that = (DialBackground) obj;
210         if (!PaintUtilities.equal(this.paint, that.paint)) {
211             return false;
212         }
213         if (!this.gradientPaintTransformer.equals(
214                 that.gradientPaintTransformer)) {
215             return false;
216         }
217         return super.equals(obj);
218     }
219 
220     /**
221      * Returns a hash code for this instance.
222      *
223      * @return The hash code.
224      */
225     @Override
hashCode()226     public int hashCode() {
227         int result = 193;
228         result = 37 * result + HashUtilities.hashCodeForPaint(this.paint);
229         result = 37 * result + this.gradientPaintTransformer.hashCode();
230         return result;
231     }
232 
233     /**
234      * Returns a clone of this instance.
235      *
236      * @return The clone.
237      *
238      * @throws CloneNotSupportedException if some attribute of this instance
239      *     cannot be cloned.
240      */
241     @Override
clone()242     public Object clone() throws CloneNotSupportedException {
243         return super.clone();
244     }
245 
246     /**
247      * Provides serialization support.
248      *
249      * @param stream  the output stream.
250      *
251      * @throws IOException  if there is an I/O error.
252      */
writeObject(ObjectOutputStream stream)253     private void writeObject(ObjectOutputStream stream) throws IOException {
254         stream.defaultWriteObject();
255         SerialUtilities.writePaint(this.paint, stream);
256     }
257 
258     /**
259      * Provides serialization support.
260      *
261      * @param stream  the input stream.
262      *
263      * @throws IOException  if there is an I/O error.
264      * @throws ClassNotFoundException  if there is a classpath problem.
265      */
readObject(ObjectInputStream stream)266     private void readObject(ObjectInputStream stream)
267             throws IOException, ClassNotFoundException {
268         stream.defaultReadObject();
269         this.paint = SerialUtilities.readPaint(stream);
270     }
271 
272 }
273