001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * -------------------
028: * DialBackground.java
029: * -------------------
030: * (C) Copyright 2006, 2007, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: DialBackground.java,v 1.1.2.3 2007/06/11 11:14:02 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 03-Nov-2006 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.experimental.chart.plot.dial;
044:
045: import java.awt.Color;
046: import java.awt.GradientPaint;
047: import java.awt.Graphics2D;
048: import java.awt.Paint;
049: import java.awt.geom.Rectangle2D;
050: import java.io.IOException;
051: import java.io.ObjectInputStream;
052: import java.io.ObjectOutputStream;
053: import java.io.Serializable;
054:
055: import org.jfree.chart.HashUtilities;
056: import org.jfree.io.SerialUtilities;
057: import org.jfree.ui.GradientPaintTransformer;
058: import org.jfree.ui.StandardGradientPaintTransformer;
059: import org.jfree.util.PaintUtilities;
060: import org.jfree.util.PublicCloneable;
061:
062: /**
063: * A regular dial layer that can be used to draw the background for a dial.
064: */
065: public class DialBackground extends AbstractDialLayer implements
066: DialLayer, Cloneable, PublicCloneable, Serializable {
067:
068: /**
069: * The background paint. This field is transient because serialization
070: * requires special handling.
071: */
072: private transient Paint paint;
073:
074: /**
075: * The transformer used when the background paint is an instance of
076: * <code>GradientPaint</code>.
077: */
078: private GradientPaintTransformer gradientPaintTransformer;
079:
080: /**
081: * Creates a new instance of <code>DialBackground</code>. The
082: * default background paint is <code>Color.white</code>.
083: */
084: public DialBackground() {
085: this (Color.white);
086: }
087:
088: /**
089: * Creates a new instance of <code>DialBackground</code>. The
090: *
091: * @param paint the paint (<code>null</code> not permitted).
092: *
093: * @throws IllegalArgumentException if <code>paint</code> is
094: * <code>null</code>.
095: */
096: public DialBackground(Paint paint) {
097: if (paint == null) {
098: throw new IllegalArgumentException("Null 'paint' argument.");
099: }
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 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 paint) {
123: if (paint == null) {
124: throw new IllegalArgumentException("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("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 g2, DialPlot plot, Rectangle2D frame,
179: Rectangle2D view) {
180:
181: Paint p = this .paint;
182: if (p instanceof GradientPaint) {
183: p = this .gradientPaintTransformer.transform(
184: (GradientPaint) p, view);
185: }
186: g2.setPaint(p);
187: g2.fill(view);
188: }
189:
190: /**
191: * Tests this instance for equality with an arbitrary object.
192: *
193: * @param obj the object (<code>null</code> permitted).
194: *
195: * @return A boolean.
196: */
197: public boolean equals(Object obj) {
198: if (obj == this ) {
199: return true;
200: }
201: if (!(obj instanceof DialBackground)) {
202: return false;
203: }
204: DialBackground that = (DialBackground) obj;
205: if (!PaintUtilities.equal(this .paint, that.paint)) {
206: return false;
207: }
208: if (!this .gradientPaintTransformer
209: .equals(that.gradientPaintTransformer)) {
210: return false;
211: }
212: return true;
213: }
214:
215: /**
216: * Returns a hash code for this instance.
217: *
218: * @return The hash code.
219: */
220: public int hashCode() {
221: int result = 193;
222: result = 37 * result
223: + HashUtilities.hashCodeForPaint(this .paint);
224: result = 37 * result + this .gradientPaintTransformer.hashCode();
225: return result;
226: }
227:
228: /**
229: * Returns a clone of this instance.
230: *
231: * @return The clone.
232: *
233: * @throws CloneNotSupportedException if some attribute of this instance
234: * cannot be cloned.
235: */
236: public Object clone() throws CloneNotSupportedException {
237: return super .clone();
238: }
239:
240: /**
241: * Provides serialization support.
242: *
243: * @param stream the output stream.
244: *
245: * @throws IOException if there is an I/O error.
246: */
247: private void writeObject(ObjectOutputStream stream)
248: throws IOException {
249: stream.defaultWriteObject();
250: SerialUtilities.writePaint(this .paint, stream);
251: }
252:
253: /**
254: * Provides serialization support.
255: *
256: * @param stream the input stream.
257: *
258: * @throws IOException if there is an I/O error.
259: * @throws ClassNotFoundException if there is a classpath problem.
260: */
261: private void readObject(ObjectInputStream stream)
262: throws IOException, ClassNotFoundException {
263: stream.defaultReadObject();
264: this.paint = SerialUtilities.readPaint(stream);
265: }
266:
267: }
|