1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2004, 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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * ----------------
  25. * MeterNeedle.java
  26. * ----------------
  27. * (C) Copyright 2002, 2003, by the Australian Antarctic Division and Contributors.
  28. *
  29. * Original Author: Bryan Scott (for the Australian Antarctic Division);
  30. * Contributor(s): David Gilbert (for Object Refinery Limited);
  31. * Nicolas Brodu (for Astrium and EADS Corporate Research Center);
  32. *
  33. * $Id: MeterNeedle.java,v 1.2 2004/11/27 17:13:47 mungady Exp $
  34. *
  35. * Changes:
  36. * --------
  37. * 25-Sep-2002 : Version 1, contributed by Bryan Scott (DG);
  38. * 07-Nov-2002 : Fixed errors reported by Checkstyle (DG);
  39. * 01-Sep-2003 : Implemented Serialization (NB);
  40. * 16-Mar-2004 : Changed transform from private to protected (BRS);
  41. */
  42. package org.jfree.chart.needle;
  43. import java.awt.BasicStroke;
  44. import java.awt.Color;
  45. import java.awt.Graphics2D;
  46. import java.awt.Paint;
  47. import java.awt.Shape;
  48. import java.awt.Stroke;
  49. import java.awt.geom.AffineTransform;
  50. import java.awt.geom.Point2D;
  51. import java.awt.geom.Rectangle2D;
  52. import java.io.IOException;
  53. import java.io.ObjectInputStream;
  54. import java.io.ObjectOutputStream;
  55. import java.io.Serializable;
  56. import org.jfree.io.SerialUtilities;
  57. import org.jfree.util.ObjectUtilities;
  58. /**
  59. * The base class used to represent the needle on a {@link org.jfree.chart.plot.CompassPlot}.
  60. *
  61. * @author Bryan Scott
  62. */
  63. public abstract class MeterNeedle implements Serializable {
  64. /** The outline paint. */
  65. private transient Paint outlinePaint = Color.black;
  66. /** The outline stroke. */
  67. private transient Stroke outlineStroke = new BasicStroke(2);
  68. /** The fill paint. */
  69. private transient Paint fillPaint = null;
  70. /** The highlight paint. */
  71. private transient Paint highlightPaint = null;
  72. /** The size. */
  73. private int size = 5;
  74. /** Scalar to aply to locate the rotation x point. */
  75. private double rotateX = 0.5;
  76. /** Scalar to aply to locate the rotation y point. */
  77. private double rotateY = 0.5;
  78. /** A transform. */
  79. protected static AffineTransform transform = new AffineTransform();
  80. /**
  81. * Creates a new needle.
  82. */
  83. public MeterNeedle() {
  84. this(null, null, null);
  85. }
  86. /**
  87. * Creates a new needle.
  88. *
  89. * @param outline the outline paint.
  90. * @param fill the fill paint.
  91. * @param highlight the highlight paint.
  92. */
  93. public MeterNeedle(Paint outline, Paint fill, Paint highlight) {
  94. this.fillPaint = fill;
  95. this.highlightPaint = highlight;
  96. this.outlinePaint = outline;
  97. }
  98. /**
  99. * Returns the outline paint.
  100. *
  101. * @return the outline paint.
  102. */
  103. public Paint getOutlinePaint() {
  104. return this.outlinePaint;
  105. }
  106. /**
  107. * Sets the outline paint.
  108. *
  109. * @param p the new paint.
  110. */
  111. public void setOutlinePaint(Paint p) {
  112. if (p != null) {
  113. this.outlinePaint = p;
  114. }
  115. }
  116. /**
  117. * Returns the outline stroke.
  118. *
  119. * @return the outline stroke.
  120. */
  121. public Stroke getOutlineStroke() {
  122. return this.outlineStroke;
  123. }
  124. /**
  125. * Sets the outline stroke.
  126. *
  127. * @param s the new stroke.
  128. */
  129. public void setOutlineStroke(Stroke s) {
  130. if (s != null) {
  131. this.outlineStroke = s;
  132. }
  133. }
  134. /**
  135. * Returns the fill paint.
  136. *
  137. * @return the fill paint.
  138. */
  139. public Paint getFillPaint() {
  140. return this.fillPaint;
  141. }
  142. /**
  143. * Sets the fill paint.
  144. *
  145. * @param p the fill paint.
  146. */
  147. public void setFillPaint(Paint p) {
  148. if (p != null) {
  149. this.fillPaint = p;
  150. }
  151. }
  152. /**
  153. * Returns the highlight paint.
  154. *
  155. * @return the highlight paint.
  156. */
  157. public Paint getHighlightPaint() {
  158. return this.highlightPaint;
  159. }
  160. /**
  161. * Sets the highlight paint.
  162. *
  163. * @param p the highlight paint.
  164. */
  165. public void setHighlightPaint(Paint p) {
  166. if (p != null) {
  167. this.highlightPaint = p;
  168. }
  169. }
  170. /**
  171. * Returns the scalar used for determining the rotation x value.
  172. *
  173. * @return the x rotate scalar.
  174. */
  175. public double getRotateX() {
  176. return this.rotateX;
  177. }
  178. /**
  179. * Sets the rotateX value.
  180. *
  181. * @param x the new value.
  182. */
  183. public void setRotateX(double x) {
  184. this.rotateX = x;
  185. }
  186. /**
  187. * Sets the rotateY value.
  188. *
  189. * @param y the new value.
  190. */
  191. public void setRotateY(double y) {
  192. this.rotateY = y;
  193. }
  194. /**
  195. * Returns the scalar used for determining the rotation y value.
  196. *
  197. * @return the y rotate scalar.
  198. */
  199. public double getRotateY() {
  200. return this.rotateY;
  201. }
  202. /**
  203. * Draws the needle.
  204. *
  205. * @param g2 the graphics device.
  206. * @param plotArea the plot area.
  207. */
  208. public void draw(Graphics2D g2, Rectangle2D plotArea) {
  209. draw(g2, plotArea, 0);
  210. }
  211. /**
  212. * Draws the needle.
  213. *
  214. * @param g2 the graphics device.
  215. * @param plotArea the plot area.
  216. * @param angle the angle.
  217. */
  218. public void draw(Graphics2D g2, Rectangle2D plotArea, double angle) {
  219. Point2D.Double pt = new Point2D.Double();
  220. pt.setLocation(plotArea.getMinX() + this.rotateX * plotArea.getWidth(),
  221. plotArea.getMinY() + this.rotateY * plotArea.getHeight());
  222. draw(g2, plotArea, pt, angle);
  223. }
  224. /**
  225. * Draws the needle.
  226. *
  227. * @param g2 the graphics device.
  228. * @param plotArea the plot area.
  229. * @param rotate the rotation point.
  230. * @param angle the angle.
  231. */
  232. public void draw(Graphics2D g2, Rectangle2D plotArea, Point2D rotate, double angle) {
  233. Paint savePaint = g2.getColor();
  234. Stroke saveStroke = g2.getStroke();
  235. drawNeedle(g2, plotArea, rotate, Math.toRadians(angle));
  236. g2.setStroke(saveStroke);
  237. g2.setPaint(savePaint);
  238. }
  239. /**
  240. * Draws the needle.
  241. *
  242. * @param g2 the graphics device.
  243. * @param plotArea the plot area.
  244. * @param rotate the rotation point.
  245. * @param angle the angle.
  246. */
  247. protected abstract void drawNeedle(Graphics2D g2,
  248. Rectangle2D plotArea, Point2D rotate, double angle);
  249. /**
  250. * Displays a shape.
  251. *
  252. * @param g2 the graphics device.
  253. * @param shape the shape.
  254. */
  255. protected void defaultDisplay(Graphics2D g2, Shape shape) {
  256. if (this.fillPaint != null) {
  257. g2.setPaint(this.fillPaint);
  258. g2.fill(shape);
  259. }
  260. if (this.outlinePaint != null) {
  261. g2.setStroke(this.outlineStroke);
  262. g2.setPaint(this.outlinePaint);
  263. g2.draw(shape);
  264. }
  265. }
  266. /**
  267. * Returns the size.
  268. *
  269. * @return the size.
  270. */
  271. public int getSize() {
  272. return this.size;
  273. }
  274. /**
  275. * Sets the size.
  276. *
  277. * @param pixels the new size.
  278. */
  279. public void setSize(int pixels) {
  280. this.size = pixels;
  281. }
  282. /**
  283. * Returns the transform.
  284. *
  285. * @return the transform.
  286. */
  287. public AffineTransform getTransform() {
  288. return MeterNeedle.transform;
  289. }
  290. /**
  291. * Tests another object for equality with this object.
  292. *
  293. * @param object the object to test.
  294. *
  295. * @return A boolean.
  296. */
  297. public boolean equals(Object object) {
  298. if (object == this) {
  299. return true;
  300. }
  301. if (!(object instanceof MeterNeedle)) {
  302. return false;
  303. }
  304. MeterNeedle that = (MeterNeedle) object;
  305. if (!ObjectUtilities.equal(this.outlinePaint, that.outlinePaint)) {
  306. return false;
  307. }
  308. if (!ObjectUtilities.equal(this.outlineStroke, that.outlineStroke)) {
  309. return false;
  310. }
  311. if (!ObjectUtilities.equal(this.fillPaint, that.fillPaint)) {
  312. return false;
  313. }
  314. if (!ObjectUtilities.equal(this.highlightPaint, that.highlightPaint)) {
  315. return false;
  316. }
  317. if (this.size != that.size) {
  318. return false;
  319. }
  320. if (this.rotateX != that.rotateX) {
  321. return false;
  322. }
  323. if (this.rotateY != that.rotateY) {
  324. return false;
  325. }
  326. return true;
  327. }
  328. /**
  329. * Provides serialization support.
  330. *
  331. * @param stream the output stream.
  332. *
  333. * @throws IOException if there is an I/O error.
  334. */
  335. private void writeObject(ObjectOutputStream stream) throws IOException {
  336. stream.defaultWriteObject();
  337. SerialUtilities.writeStroke(this.outlineStroke, stream);
  338. SerialUtilities.writePaint(this.outlinePaint, stream);
  339. SerialUtilities.writePaint(this.fillPaint, stream);
  340. SerialUtilities.writePaint(this.highlightPaint, stream);
  341. }
  342. /**
  343. * Provides serialization support.
  344. *
  345. * @param stream the input stream.
  346. *
  347. * @throws IOException if there is an I/O error.
  348. * @throws ClassNotFoundException if there is a classpath problem.
  349. */
  350. private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
  351. stream.defaultReadObject();
  352. this.outlineStroke = SerialUtilities.readStroke(stream);
  353. this.outlinePaint = SerialUtilities.readPaint(stream);
  354. this.fillPaint = SerialUtilities.readPaint(stream);
  355. this.highlightPaint = SerialUtilities.readPaint(stream);
  356. }
  357. }