1 46 47 package org.jfree.chart.needle; 48 49 import java.awt.BasicStroke ; 50 import java.awt.Color ; 51 import java.awt.Graphics2D ; 52 import java.awt.Paint ; 53 import java.awt.Shape ; 54 import java.awt.Stroke ; 55 import java.awt.geom.AffineTransform ; 56 import java.awt.geom.Point2D ; 57 import java.awt.geom.Rectangle2D ; 58 import java.io.IOException ; 59 import java.io.ObjectInputStream ; 60 import java.io.ObjectOutputStream ; 61 import java.io.Serializable ; 62 63 import org.jfree.io.SerialUtilities; 64 import org.jfree.util.ObjectUtilities; 65 66 72 public abstract class MeterNeedle implements Serializable { 73 74 75 private static final long serialVersionUID = 5203064851510951052L; 76 77 78 private transient Paint outlinePaint = Color.black; 79 80 81 private transient Stroke outlineStroke = new BasicStroke (2); 82 83 84 private transient Paint fillPaint = null; 85 86 87 private transient Paint highlightPaint = null; 88 89 90 private int size = 5; 91 92 93 private double rotateX = 0.5; 94 95 96 private double rotateY = 0.5; 97 98 99 protected static AffineTransform transform = new AffineTransform (); 100 101 104 public MeterNeedle() { 105 this(null, null, null); 106 } 107 108 115 public MeterNeedle(Paint outline, Paint fill, Paint highlight) { 116 this.fillPaint = fill; 117 this.highlightPaint = highlight; 118 this.outlinePaint = outline; 119 } 120 121 126 public Paint getOutlinePaint() { 127 return this.outlinePaint; 128 } 129 130 135 public void setOutlinePaint(Paint p) { 136 if (p != null) { 137 this.outlinePaint = p; 138 } 139 } 140 141 146 public Stroke getOutlineStroke() { 147 return this.outlineStroke; 148 } 149 150 155 public void setOutlineStroke(Stroke s) { 156 if (s != null) { 157 this.outlineStroke = s; 158 } 159 } 160 161 166 public Paint getFillPaint() { 167 return this.fillPaint; 168 } 169 170 175 public void setFillPaint(Paint p) { 176 if (p != null) { 177 this.fillPaint = p; 178 } 179 } 180 181 186 public Paint getHighlightPaint() { 187 return this.highlightPaint; 188 } 189 190 195 public void setHighlightPaint(Paint p) { 196 if (p != null) { 197 this.highlightPaint = p; 198 } 199 } 200 201 206 public double getRotateX() { 207 return this.rotateX; 208 } 209 210 215 public void setRotateX(double x) { 216 this.rotateX = x; 217 } 218 219 224 public void setRotateY(double y) { 225 this.rotateY = y; 226 } 227 228 233 public double getRotateY() { 234 return this.rotateY; 235 } 236 237 243 public void draw(Graphics2D g2, Rectangle2D plotArea) { 244 draw(g2, plotArea, 0); 245 } 246 247 254 public void draw(Graphics2D g2, Rectangle2D plotArea, double angle) { 255 256 Point2D.Double pt = new Point2D.Double (); 257 pt.setLocation( 258 plotArea.getMinX() + this.rotateX * plotArea.getWidth(), 259 plotArea.getMinY() + this.rotateY * plotArea.getHeight() 260 ); 261 draw(g2, plotArea, pt, angle); 262 263 } 264 265 273 public void draw(Graphics2D g2, Rectangle2D plotArea, Point2D rotate, 274 double angle) { 275 276 Paint savePaint = g2.getColor(); 277 Stroke saveStroke = g2.getStroke(); 278 279 drawNeedle(g2, plotArea, rotate, Math.toRadians(angle)); 280 281 g2.setStroke(saveStroke); 282 g2.setPaint(savePaint); 283 284 } 285 286 294 protected abstract void drawNeedle(Graphics2D g2, 295 Rectangle2D plotArea, Point2D rotate, 296 double angle); 297 298 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 324 public int getSize() { 325 return this.size; 326 } 327 328 333 public void setSize(int pixels) { 334 this.size = pixels; 335 } 336 337 342 public AffineTransform getTransform() { 343 return MeterNeedle.transform; 344 } 345 346 353 public boolean equals(Object object) { 354 if (object == this) { 355 return true; 356 } 357 if (!(object instanceof MeterNeedle)) { 358 return false; 359 } 360 361 MeterNeedle that = (MeterNeedle) object; 362 if (!ObjectUtilities.equal(this.outlinePaint, that.outlinePaint)) { 363 return false; 364 } 365 if (!ObjectUtilities.equal(this.outlineStroke, that.outlineStroke)) { 366 return false; 367 } 368 if (!ObjectUtilities.equal(this.fillPaint, that.fillPaint)) { 369 return false; 370 } 371 if (!ObjectUtilities.equal(this.highlightPaint, that.highlightPaint)) { 372 return false; 373 } 374 if (this.size != that.size) { 375 return false; 376 } 377 if (this.rotateX != that.rotateX) { 378 return false; 379 } 380 if (this.rotateY != that.rotateY) { 381 return false; 382 } 383 return true; 384 } 385 386 393 private void writeObject(ObjectOutputStream stream) throws IOException { 394 stream.defaultWriteObject(); 395 SerialUtilities.writeStroke(this.outlineStroke, stream); 396 SerialUtilities.writePaint(this.outlinePaint, stream); 397 SerialUtilities.writePaint(this.fillPaint, stream); 398 SerialUtilities.writePaint(this.highlightPaint, stream); 399 } 400 401 409 private void readObject(ObjectInputStream stream) 410 throws IOException , ClassNotFoundException { 411 stream.defaultReadObject(); 412 this.outlineStroke = SerialUtilities.readStroke(stream); 413 this.outlinePaint = SerialUtilities.readPaint(stream); 414 this.fillPaint = SerialUtilities.readPaint(stream); 415 this.highlightPaint = SerialUtilities.readPaint(stream); 416 } 417 418 } 419 | Popular Tags |