1 42 43 package org.jfree.experimental.chart.plot.dial; 44 45 import java.awt.BasicStroke ; 46 import java.awt.Color ; 47 import java.awt.Font ; 48 import java.awt.FontMetrics ; 49 import java.awt.Graphics2D ; 50 import java.awt.Paint ; 51 import java.awt.Stroke ; 52 import java.awt.geom.Arc2D ; 53 import java.awt.geom.Point2D ; 54 import java.awt.geom.Rectangle2D ; 55 import java.io.IOException ; 56 import java.io.ObjectInputStream ; 57 import java.io.ObjectOutputStream ; 58 import java.io.Serializable ; 59 import java.text.DecimalFormat ; 60 import java.text.NumberFormat ; 61 62 import org.jfree.chart.HashUtilities; 63 import org.jfree.io.SerialUtilities; 64 import org.jfree.text.TextUtilities; 65 import org.jfree.ui.RectangleAnchor; 66 import org.jfree.ui.RectangleInsets; 67 import org.jfree.ui.Size2D; 68 import org.jfree.ui.TextAnchor; 69 import org.jfree.util.PaintUtilities; 70 import org.jfree.util.PublicCloneable; 71 72 75 public class DialValueIndicator extends AbstractDialLayer implements DialLayer, 76 Cloneable , PublicCloneable, Serializable { 77 78 79 private int datasetIndex; 80 81 82 private double angle; 83 84 85 private double radius; 86 87 88 private RectangleAnchor frameAnchor; 89 90 91 private Number templateValue; 92 93 94 private NumberFormat formatter; 95 96 97 private Font font; 98 99 100 private transient Paint paint; 101 102 103 private transient Paint backgroundPaint; 104 105 106 private transient Stroke outlineStroke; 107 108 109 private transient Paint outlinePaint; 110 111 112 private RectangleInsets insets; 113 114 115 private RectangleAnchor valueAnchor; 116 117 118 private TextAnchor textAnchor; 119 120 126 public DialValueIndicator(int datasetIndex, String label) { 127 this.datasetIndex = datasetIndex; 128 this.angle = -90.0; 129 this.radius = 0.3; 130 this.frameAnchor = RectangleAnchor.CENTER; 131 this.templateValue = new Double (100.0); 132 this.formatter = new DecimalFormat ("0.0"); 133 this.font = new Font ("Dialog", Font.BOLD, 14); 134 this.paint = Color.black; 135 this.backgroundPaint = Color.white; 136 this.outlineStroke = new BasicStroke (1.0f); 137 this.outlinePaint = Color.blue; 138 this.insets = new RectangleInsets(4, 4, 4, 4); 139 this.valueAnchor = RectangleAnchor.RIGHT; 140 this.textAnchor = TextAnchor.CENTER_RIGHT; 141 } 142 143 149 public int getDatasetIndex() { 150 return this.datasetIndex; 151 } 152 153 158 public void setDatasetIndex(int index) { 159 this.datasetIndex = index; 160 } 161 162 168 public double getAngle() { 169 return this.angle; 170 } 171 172 178 public void setAngle(double angle) { 179 this.angle = angle; 180 notifyListeners(new DialLayerChangeEvent(this)); 181 } 182 183 188 public double getRadius() { 189 return this.radius; 190 } 191 192 197 public void setRadius(double radius) { 198 this.radius = radius; 200 notifyListeners(new DialLayerChangeEvent(this)); 201 } 202 203 208 public RectangleAnchor getFrameAnchor() { 209 return this.frameAnchor; 210 } 211 212 218 public void setFrameAnchor(RectangleAnchor anchor) { 219 if (anchor == null) { 220 throw new IllegalArgumentException ("Null 'anchor' argument."); 221 } 222 this.frameAnchor = anchor; 223 notifyListeners(new DialLayerChangeEvent(this)); 224 } 225 226 231 public Number getTemplateValue() { 232 return this.templateValue; 233 } 234 235 241 public void setTemplateValue(Number value) { 242 this.templateValue = value; 243 notifyListeners(new DialLayerChangeEvent(this)); 244 } 245 246 251 public NumberFormat getNumberFormat() { 252 return this.formatter; 253 } 254 255 261 public void setNumberFormat(NumberFormat formatter) { 262 if (formatter == null) { 263 throw new IllegalArgumentException ("Null 'formatter' argument."); 264 } 265 this.formatter = formatter; 266 notifyListeners(new DialLayerChangeEvent(this)); 267 } 268 269 274 public Font getFont() { 275 return this.font; 276 } 277 278 284 public void setFont(Font font) { 285 if (font == null) { 286 throw new IllegalArgumentException ("Null 'font' argument."); 287 } 288 this.font = font; 289 notifyListeners(new DialLayerChangeEvent(this)); 290 } 291 292 297 public Paint getPaint() { 298 return this.paint; 299 } 300 301 307 public void setPaint(Paint paint) { 308 if (paint == null) { 309 throw new IllegalArgumentException ("Null 'paint' argument."); 310 } 311 this.paint = paint; 312 notifyListeners(new DialLayerChangeEvent(this)); 313 } 314 315 320 public Paint getBackgroundPaint() { 321 return this.backgroundPaint; 322 } 323 324 329 public void setBackgroundPaint(Paint paint) { 330 if (paint == null) { 331 throw new IllegalArgumentException ("Null 'paint' argument."); 332 } 333 this.backgroundPaint = paint; 334 notifyListeners(new DialLayerChangeEvent(this)); 335 } 336 337 342 public Stroke getOutlineStroke() { 343 return this.outlineStroke; 344 } 345 346 351 public void setOutlineStroke(Stroke stroke) { 352 if (stroke == null) { 353 throw new IllegalArgumentException ("Null 'stroke' argument."); 354 } 355 this.outlineStroke = stroke; 356 notifyListeners(new DialLayerChangeEvent(this)); 357 } 358 359 364 public Paint getOutlinePaint() { 365 return this.outlinePaint; 366 } 367 368 374 public void setOutlinePaint(Paint paint) { 375 if (paint == null) { 376 throw new IllegalArgumentException ("Null 'paint' argument."); 377 } 378 this.outlinePaint = paint; 379 notifyListeners(new DialLayerChangeEvent(this)); 380 } 381 382 387 public RectangleInsets getInsets() { 388 return this.insets; 389 } 390 391 396 public void setInsets(RectangleInsets insets) { 397 if (insets == null) { 398 throw new IllegalArgumentException ("Null 'insets' argument."); 399 } 400 this.insets = insets; 401 notifyListeners(new DialLayerChangeEvent(this)); 402 } 403 404 409 public RectangleAnchor getValueAnchor() { 410 return this.valueAnchor; 411 } 412 413 418 public void setValueAnchor(RectangleAnchor anchor) { 419 if (anchor == null) { 420 throw new IllegalArgumentException ("Null 'anchor' argument."); 421 } 422 this.valueAnchor = anchor; 423 notifyListeners(new DialLayerChangeEvent(this)); 424 } 425 426 431 public TextAnchor getTextAnchor() { 432 return this.textAnchor; 433 } 434 435 440 public void setTextAnchor(TextAnchor anchor) { 441 if (anchor == null) { 442 throw new IllegalArgumentException ("Null 'anchor' argument."); 443 } 444 this.textAnchor = anchor; 445 notifyListeners(new DialLayerChangeEvent(this)); 446 } 447 448 454 public boolean isClippedToWindow() { 455 return true; 456 } 457 458 468 public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame, 469 Rectangle2D view) { 470 471 Rectangle2D f = DialPlot.rectangleByRadius(frame, this.radius, 473 this.radius); 474 Arc2D arc = new Arc2D.Double (f, this.angle, 0.0, Arc2D.OPEN); 475 Point2D pt = arc.getStartPoint(); 476 477 FontMetrics fm = g2.getFontMetrics(this.font); 479 String s = this.formatter.format(this.templateValue); 480 Rectangle2D tb = TextUtilities.getTextBounds(s, g2, fm); 481 482 Rectangle2D bounds = RectangleAnchor.createRectangle(new Size2D( 484 tb.getWidth(), tb.getHeight()), pt.getX(), pt.getY(), 485 this.frameAnchor); 486 487 Rectangle2D fb = this.insets.createOutsetRectangle(bounds); 489 490 g2.setPaint(this.backgroundPaint); 492 g2.fill(fb); 493 494 g2.setStroke(this.outlineStroke); 496 g2.setPaint(this.outlinePaint); 497 g2.draw(fb); 498 499 500 double value = plot.getValue(this.datasetIndex); 502 String valueStr = this.formatter.format(value); 503 Point2D pt2 = RectangleAnchor.coordinates(bounds, this.valueAnchor); 504 g2.setPaint(this.paint); 505 g2.setFont(this.font); 506 TextUtilities.drawAlignedString(valueStr, g2, (float) pt2.getX(), 507 (float) pt2.getY(), this.textAnchor); 508 509 } 510 511 518 public boolean equals(Object obj) { 519 if (obj == this) { 520 return true; 521 } 522 if (!(obj instanceof DialValueIndicator)) { 523 return false; 524 } 525 DialValueIndicator that = (DialValueIndicator) obj; 526 if (this.datasetIndex != that.datasetIndex) { 527 return false; 528 } 529 if (this.angle != that.angle) { 530 return false; 531 } 532 if (this.radius != that.radius) { 533 return false; 534 } 535 if (!this.frameAnchor.equals(that.frameAnchor)) { 536 return false; 537 } 538 if (!this.templateValue.equals(that.templateValue)) { 539 return false; 540 } 541 if (!this.font.equals(that.font)) { 542 return false; 543 } 544 if (!PaintUtilities.equal(this.paint, that.paint)) { 545 return false; 546 } 547 if (!PaintUtilities.equal(this.backgroundPaint, that.backgroundPaint)) { 548 return false; 549 } 550 if (!this.outlineStroke.equals(that.outlineStroke)) { 551 return false; 552 } 553 if (!PaintUtilities.equal(this.outlinePaint, that.outlinePaint)) { 554 return false; 555 } 556 if (!this.insets.equals(that.insets)) { 557 return false; 558 } 559 if (!this.valueAnchor.equals(that.valueAnchor)) { 560 return false; 561 } 562 if (!this.textAnchor.equals(that.textAnchor)) { 563 return false; 564 } 565 566 return true; 567 } 568 569 574 public int hashCode() { 575 int result = 193; 576 result = 37 * result + HashUtilities.hashCodeForPaint(this.paint); 577 result = 37 * result + HashUtilities.hashCodeForPaint( 578 this.backgroundPaint); 579 result = 37 * result + HashUtilities.hashCodeForPaint( 580 this.outlinePaint); 581 result = 37 * result + this.outlineStroke.hashCode(); 582 return result; 583 } 584 585 593 public Object clone() throws CloneNotSupportedException { 594 return super.clone(); 595 } 596 597 604 private void writeObject(ObjectOutputStream stream) throws IOException { 605 stream.defaultWriteObject(); 606 SerialUtilities.writePaint(this.paint, stream); 607 SerialUtilities.writePaint(this.backgroundPaint, stream); 608 SerialUtilities.writePaint(this.outlinePaint, stream); 609 SerialUtilities.writeStroke(this.outlineStroke, stream); 610 } 611 612 620 private void readObject(ObjectInputStream stream) 621 throws IOException , ClassNotFoundException { 622 stream.defaultReadObject(); 623 this.paint = SerialUtilities.readPaint(stream); 624 this.backgroundPaint = SerialUtilities.readPaint(stream); 625 this.outlinePaint = SerialUtilities.readPaint(stream); 626 this.outlineStroke = SerialUtilities.readStroke(stream); 627 } 628 629 } 630 | Popular Tags |