001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2006, 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: * DialCap.java
029: * ------------
030: * (C) Copyright 2006, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: DialCap.java,v 1.1.2.2 2006/11/06 16:26:06 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.BasicStroke;
046: import java.awt.Color;
047: import java.awt.Graphics2D;
048: import java.awt.Paint;
049: import java.awt.Stroke;
050: import java.awt.geom.Ellipse2D;
051: import java.awt.geom.Rectangle2D;
052: import java.io.IOException;
053: import java.io.ObjectInputStream;
054: import java.io.ObjectOutputStream;
055: import java.io.Serializable;
056:
057: import org.jfree.chart.HashUtilities;
058: import org.jfree.io.SerialUtilities;
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 a cap over the center of
064: * the dial (the base of the dial pointer(s)).
065: */
066: public class DialCap extends AbstractDialLayer implements DialLayer,
067: Cloneable, PublicCloneable, Serializable {
068:
069: /**
070: * The radius of the cap, as a percentage of the framing rectangle.
071: */
072: private double radius;
073:
074: /**
075: * The fill paint. This field is transient because it requires special
076: * handling for serialization.
077: */
078: private transient Paint fillPaint;
079:
080: /**
081: * The paint used to draw the cap outline (this should never be
082: * <code>null</code>). This field is transient because it requires
083: * special handling for serialization.
084: */
085: private transient Paint outlinePaint;
086:
087: /**
088: * The stroke used to draw the cap outline (this should never be
089: * <code>null</code>). This field is transient because it requires
090: * special handling for serialization.
091: */
092: private transient Stroke outlineStroke;
093:
094: /**
095: * Creates a new instance of <code>StandardDialBackground</code>. The
096: * default background paint is <code>Color.white</code>.
097: */
098: public DialCap() {
099: this .radius = 0.05;
100: this .fillPaint = Color.white;
101: this .outlinePaint = Color.black;
102: this .outlineStroke = new BasicStroke(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 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 paint) {
150: if (paint == null) {
151: throw new IllegalArgumentException("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 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 paint) {
176: if (paint == null) {
177: throw new IllegalArgumentException("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 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 stroke) {
203: if (stroke == null) {
204: throw new IllegalArgumentException(
205: "Null 'stroke' argument.");
206: }
207: this .outlineStroke = stroke;
208: notifyListeners(new DialLayerChangeEvent(this ));
209: }
210:
211: /**
212: * Returns <code>true</code> to indicate that this layer should be
213: * clipped within the dial window.
214: *
215: * @return <code>true</code>.
216: */
217: public boolean isClippedToWindow() {
218: return true;
219: }
220:
221: /**
222: * Draws the background to the specified graphics device. If the dial
223: * frame specifies a window, the clipping region will already have been
224: * set to this window before this method is called.
225: *
226: * @param g2 the graphics device (<code>null</code> not permitted).
227: * @param plot the plot (ignored here).
228: * @param frame the dial frame (ignored here).
229: * @param view the view rectangle (<code>null</code> not permitted).
230: */
231: public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame,
232: Rectangle2D view) {
233:
234: g2.setPaint(this .fillPaint);
235:
236: Rectangle2D f = DialPlot.rectangleByRadius(frame, this .radius,
237: this .radius);
238: Ellipse2D e = new Ellipse2D.Double(f.getX(), f.getY(), f
239: .getWidth(), f.getHeight());
240: g2.fill(e);
241: g2.setPaint(this .outlinePaint);
242: g2.setStroke(this .outlineStroke);
243: g2.draw(e);
244:
245: }
246:
247: /**
248: * Tests this instance for equality with an arbitrary object.
249: *
250: * @param obj the object (<code>null</code> permitted).
251: *
252: * @return A boolean.
253: */
254: public boolean equals(Object obj) {
255: if (obj == this ) {
256: return true;
257: }
258: if (!(obj instanceof DialCap)) {
259: return false;
260: }
261: DialCap that = (DialCap) obj;
262: if (this .radius != that.radius) {
263: return false;
264: }
265: if (!PaintUtilities.equal(this .fillPaint, that.fillPaint)) {
266: return false;
267: }
268: if (!PaintUtilities.equal(this .outlinePaint, that.outlinePaint)) {
269: return false;
270: }
271: if (!this .outlineStroke.equals(that.outlineStroke)) {
272: return false;
273: }
274: return true;
275: }
276:
277: /**
278: * Returns a hash code for this instance.
279: *
280: * @return The hash code.
281: */
282: public int hashCode() {
283: int result = 193;
284: result = 37 * result
285: + HashUtilities.hashCodeForPaint(this .fillPaint);
286: result = 37 * result
287: + HashUtilities.hashCodeForPaint(this .outlinePaint);
288: result = 37 * result + this .outlineStroke.hashCode();
289: return result;
290: }
291:
292: /**
293: * Returns a clone of this instance.
294: *
295: * @return A clone.
296: *
297: * @throws CloneNotSupportedException if some attribute of the cap cannot
298: * be cloned.
299: */
300: public Object clone() throws CloneNotSupportedException {
301: return super .clone();
302: }
303:
304: /**
305: * Provides serialization support.
306: *
307: * @param stream the output stream.
308: *
309: * @throws IOException if there is an I/O error.
310: */
311: private void writeObject(ObjectOutputStream stream)
312: throws IOException {
313: stream.defaultWriteObject();
314: SerialUtilities.writePaint(this .fillPaint, stream);
315: SerialUtilities.writePaint(this .outlinePaint, stream);
316: SerialUtilities.writeStroke(this .outlineStroke, stream);
317: }
318:
319: /**
320: * Provides serialization support.
321: *
322: * @param stream the input stream.
323: *
324: * @throws IOException if there is an I/O error.
325: * @throws ClassNotFoundException if there is a classpath problem.
326: */
327: private void readObject(ObjectInputStream stream)
328: throws IOException, ClassNotFoundException {
329: stream.defaultReadObject();
330: this.fillPaint = SerialUtilities.readPaint(stream);
331: this.outlinePaint = SerialUtilities.readPaint(stream);
332: this.outlineStroke = SerialUtilities.readStroke(stream);
333: }
334:
335: }
|