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: * MeterNeedle.java
029: * ----------------
030: * (C) Copyright 2002-2006, by the Australian Antarctic Division and
031: * Contributors.
032: *
033: * Original Author: Bryan Scott (for the Australian Antarctic Division);
034: * Contributor(s): David Gilbert (for Object Refinery Limited);
035: * Nicolas Brodu (for Astrium and EADS Corporate Research
036: * Center);
037: *
038: * $Id: MeterNeedle.java,v 1.4.2.3 2006/08/02 10:40:57 mungady Exp $
039: *
040: * Changes:
041: * --------
042: * 25-Sep-2002 : Version 1, contributed by Bryan Scott (DG);
043: * 07-Nov-2002 : Fixed errors reported by Checkstyle (DG);
044: * 01-Sep-2003 : Implemented Serialization (NB);
045: * 16-Mar-2004 : Changed transform from private to protected (BRS);
046: * 08-Jun-2005 : Fixed equals() method to handle GradientPaint (DG);
047: *
048: */
049:
050: package org.jfree.chart.needle;
051:
052: import java.awt.BasicStroke;
053: import java.awt.Color;
054: import java.awt.Graphics2D;
055: import java.awt.Paint;
056: import java.awt.Shape;
057: import java.awt.Stroke;
058: import java.awt.geom.AffineTransform;
059: import java.awt.geom.Point2D;
060: import java.awt.geom.Rectangle2D;
061: import java.io.IOException;
062: import java.io.ObjectInputStream;
063: import java.io.ObjectOutputStream;
064: import java.io.Serializable;
065:
066: import org.jfree.io.SerialUtilities;
067: import org.jfree.util.ObjectUtilities;
068: import org.jfree.util.PaintUtilities;
069:
070: /**
071: * The base class used to represent the needle on a
072: * {@link org.jfree.chart.plot.CompassPlot}.
073: */
074: public abstract class MeterNeedle implements Serializable {
075:
076: /** For serialization. */
077: private static final long serialVersionUID = 5203064851510951052L;
078:
079: /** The outline paint. */
080: private transient Paint outlinePaint = Color.black;
081:
082: /** The outline stroke. */
083: private transient Stroke outlineStroke = new BasicStroke(2);
084:
085: /** The fill paint. */
086: private transient Paint fillPaint = null;
087:
088: /** The highlight paint. */
089: private transient Paint highlightPaint = null;
090:
091: /** The size. */
092: private int size = 5;
093:
094: /** Scalar to aply to locate the rotation x point. */
095: private double rotateX = 0.5;
096:
097: /** Scalar to aply to locate the rotation y point. */
098: private double rotateY = 0.5;
099:
100: /** A transform. */
101: protected static AffineTransform transform = new AffineTransform();
102:
103: /**
104: * Creates a new needle.
105: */
106: public MeterNeedle() {
107: this (null, null, null);
108: }
109:
110: /**
111: * Creates a new needle.
112: *
113: * @param outline the outline paint (<code>null</code> permitted).
114: * @param fill the fill paint (<code>null</code> permitted).
115: * @param highlight the highlight paint (<code>null</code> permitted).
116: */
117: public MeterNeedle(Paint outline, Paint fill, Paint highlight) {
118: this .fillPaint = fill;
119: this .highlightPaint = highlight;
120: this .outlinePaint = outline;
121: }
122:
123: /**
124: * Returns the outline paint.
125: *
126: * @return The outline paint.
127: */
128: public Paint getOutlinePaint() {
129: return this .outlinePaint;
130: }
131:
132: /**
133: * Sets the outline paint.
134: *
135: * @param p the new paint.
136: */
137: public void setOutlinePaint(Paint p) {
138: if (p != null) {
139: this .outlinePaint = p;
140: }
141: }
142:
143: /**
144: * Returns the outline stroke.
145: *
146: * @return The outline stroke.
147: */
148: public Stroke getOutlineStroke() {
149: return this .outlineStroke;
150: }
151:
152: /**
153: * Sets the outline stroke.
154: *
155: * @param s the new stroke.
156: */
157: public void setOutlineStroke(Stroke s) {
158: if (s != null) {
159: this .outlineStroke = s;
160: }
161: }
162:
163: /**
164: * Returns the fill paint.
165: *
166: * @return The fill paint.
167: */
168: public Paint getFillPaint() {
169: return this .fillPaint;
170: }
171:
172: /**
173: * Sets the fill paint.
174: *
175: * @param p the fill paint.
176: */
177: public void setFillPaint(Paint p) {
178: if (p != null) {
179: this .fillPaint = p;
180: }
181: }
182:
183: /**
184: * Returns the highlight paint.
185: *
186: * @return The highlight paint.
187: */
188: public Paint getHighlightPaint() {
189: return this .highlightPaint;
190: }
191:
192: /**
193: * Sets the highlight paint.
194: *
195: * @param p the highlight paint.
196: */
197: public void setHighlightPaint(Paint p) {
198: if (p != null) {
199: this .highlightPaint = p;
200: }
201: }
202:
203: /**
204: * Returns the scalar used for determining the rotation x value.
205: *
206: * @return The x rotate scalar.
207: */
208: public double getRotateX() {
209: return this .rotateX;
210: }
211:
212: /**
213: * Sets the rotateX value.
214: *
215: * @param x the new value.
216: */
217: public void setRotateX(double x) {
218: this .rotateX = x;
219: }
220:
221: /**
222: * Sets the rotateY value.
223: *
224: * @param y the new value.
225: */
226: public void setRotateY(double y) {
227: this .rotateY = y;
228: }
229:
230: /**
231: * Returns the scalar used for determining the rotation y value.
232: *
233: * @return The y rotate scalar.
234: */
235: public double getRotateY() {
236: return this .rotateY;
237: }
238:
239: /**
240: * Draws the needle.
241: *
242: * @param g2 the graphics device.
243: * @param plotArea the plot area.
244: */
245: public void draw(Graphics2D g2, Rectangle2D plotArea) {
246: draw(g2, plotArea, 0);
247: }
248:
249: /**
250: * Draws the needle.
251: *
252: * @param g2 the graphics device.
253: * @param plotArea the plot area.
254: * @param angle the angle.
255: */
256: public void draw(Graphics2D g2, Rectangle2D plotArea, double angle) {
257:
258: Point2D.Double pt = new Point2D.Double();
259: pt.setLocation(plotArea.getMinX() + this .rotateX
260: * plotArea.getWidth(), plotArea.getMinY()
261: + this .rotateY * plotArea.getHeight());
262: draw(g2, plotArea, pt, angle);
263:
264: }
265:
266: /**
267: * Draws the needle.
268: *
269: * @param g2 the graphics device.
270: * @param plotArea the plot area.
271: * @param rotate the rotation point.
272: * @param angle the angle.
273: */
274: public void draw(Graphics2D g2, Rectangle2D plotArea,
275: Point2D rotate, double angle) {
276:
277: Paint savePaint = g2.getColor();
278: Stroke saveStroke = g2.getStroke();
279:
280: drawNeedle(g2, plotArea, rotate, Math.toRadians(angle));
281:
282: g2.setStroke(saveStroke);
283: g2.setPaint(savePaint);
284:
285: }
286:
287: /**
288: * Draws the needle.
289: *
290: * @param g2 the graphics device.
291: * @param plotArea the plot area.
292: * @param rotate the rotation point.
293: * @param angle the angle.
294: */
295: protected abstract void drawNeedle(Graphics2D g2,
296: Rectangle2D plotArea, Point2D rotate, double angle);
297:
298: /**
299: * Displays a shape.
300: *
301: * @param g2 the graphics device.
302: * @param shape the shape.
303: */
304: protected void defaultDisplay(Graphics2D g2, Shape shape) {
305:
306: if (this .fillPaint != null) {
307: g2.setPaint(this .fillPaint);
308: g2.fill(shape);
309: }
310:
311: if (this .outlinePaint != null) {
312: g2.setStroke(this .outlineStroke);
313: g2.setPaint(this .outlinePaint);
314: g2.draw(shape);
315: }
316:
317: }
318:
319: /**
320: * Returns the size.
321: *
322: * @return The size.
323: */
324: public int getSize() {
325: return this .size;
326: }
327:
328: /**
329: * Sets the size.
330: *
331: * @param pixels the new size.
332: */
333: public void setSize(int pixels) {
334: this .size = pixels;
335: }
336:
337: /**
338: * Returns the transform.
339: *
340: * @return The transform.
341: */
342: public AffineTransform getTransform() {
343: return MeterNeedle.transform;
344: }
345:
346: /**
347: * Tests another object for equality with this object.
348: *
349: * @param obj the object to test (<code>null</code> permitted).
350: *
351: * @return A boolean.
352: */
353: public boolean equals(Object obj) {
354: if (obj == this ) {
355: return true;
356: }
357: if (!(obj instanceof MeterNeedle)) {
358: return false;
359: }
360: MeterNeedle that = (MeterNeedle) obj;
361: if (!PaintUtilities.equal(this .outlinePaint, that.outlinePaint)) {
362: return false;
363: }
364: if (!ObjectUtilities.equal(this .outlineStroke,
365: that.outlineStroke)) {
366: return false;
367: }
368: if (!PaintUtilities.equal(this .fillPaint, that.fillPaint)) {
369: return false;
370: }
371: if (!PaintUtilities.equal(this .highlightPaint,
372: that.highlightPaint)) {
373: return false;
374: }
375: if (this .size != that.size) {
376: return false;
377: }
378: if (this .rotateX != that.rotateX) {
379: return false;
380: }
381: if (this .rotateY != that.rotateY) {
382: return false;
383: }
384: return true;
385: }
386:
387: /**
388: * Provides serialization support.
389: *
390: * @param stream the output stream.
391: *
392: * @throws IOException if there is an I/O error.
393: */
394: private void writeObject(ObjectOutputStream stream)
395: throws IOException {
396: stream.defaultWriteObject();
397: SerialUtilities.writeStroke(this .outlineStroke, stream);
398: SerialUtilities.writePaint(this .outlinePaint, stream);
399: SerialUtilities.writePaint(this .fillPaint, stream);
400: SerialUtilities.writePaint(this .highlightPaint, stream);
401: }
402:
403: /**
404: * Provides serialization support.
405: *
406: * @param stream the input stream.
407: *
408: * @throws IOException if there is an I/O error.
409: * @throws ClassNotFoundException if there is a classpath problem.
410: */
411: private void readObject(ObjectInputStream stream)
412: throws IOException, ClassNotFoundException {
413: stream.defaultReadObject();
414: this.outlineStroke = SerialUtilities.readStroke(stream);
415: this.outlinePaint = SerialUtilities.readPaint(stream);
416: this.fillPaint = SerialUtilities.readPaint(stream);
417: this.highlightPaint = SerialUtilities.readPaint(stream);
418: }
419:
420: }
|