1 43 44 package org.jfree.experimental.chart.plot.dial; 45 46 import java.awt.BasicStroke ; 47 import java.awt.Color ; 48 import java.awt.Font ; 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.Line2D ; 54 import java.awt.geom.Point2D ; 55 import java.awt.geom.Rectangle2D ; 56 import java.io.IOException ; 57 import java.io.ObjectInputStream ; 58 import java.io.ObjectOutputStream ; 59 import java.io.Serializable ; 60 61 import org.jfree.io.SerialUtilities; 62 import org.jfree.text.TextUtilities; 63 import org.jfree.ui.TextAnchor; 64 import org.jfree.util.PaintUtilities; 65 import org.jfree.util.PublicCloneable; 66 67 70 public class StandardDialScale extends AbstractDialLayer implements DialScale, 71 DialLayer, Cloneable , PublicCloneable, Serializable { 72 73 74 private double lowerBound; 75 76 77 private double upperBound; 78 79 83 private double startAngle; 84 85 86 private double extent; 87 88 92 private double tickRadius; 93 94 97 private double majorTickIncrement; 98 99 103 private double majorTickLength; 104 105 109 private transient Paint majorTickPaint; 110 111 115 private transient Stroke majorTickStroke; 116 117 120 private int minorTickCount; 121 122 126 private double minorTickLength; 127 128 131 private double tickLabelOffset; 132 133 136 private Font tickLabelFont; 137 138 142 private boolean tickLabelsVisible; 143 144 148 private boolean firstTickLabelVisible; 149 150 154 private transient Paint tickLabelPaint; 155 156 159 public StandardDialScale() { 160 this(0.0, 100.0, 175, -170); 161 } 162 163 172 public StandardDialScale(double lowerBound, double upperBound, 173 double startAngle, double extent) { 174 this.startAngle = startAngle; 175 this.extent = extent; 176 this.lowerBound = lowerBound; 177 this.upperBound = upperBound; 178 this.majorTickPaint = Color.black; 179 this.majorTickStroke = new BasicStroke (3.0f); 180 this.tickLabelFont = new Font ("Dialog", Font.BOLD, 16); 181 this.tickLabelPaint = Color.blue; 182 this.minorTickCount = 4; 183 this.minorTickLength = 0.02; 184 this.tickLabelOffset = 0.10; 185 this.majorTickIncrement = 10.0; 186 this.tickRadius = 0.70; 187 this.tickLabelsVisible = true; 188 this.firstTickLabelVisible = true; 189 } 190 191 199 public double getStartAngle() { 200 return this.startAngle; 201 } 202 203 210 public void setStartAngle(double angle) { 211 this.startAngle = angle; 212 notifyListeners(new DialLayerChangeEvent(this)); 213 } 214 215 220 public double getExtent() { 221 return this.extent; 222 } 223 224 231 public void setExtent(double extent) { 232 this.extent = extent; 233 notifyListeners(new DialLayerChangeEvent(this)); 234 } 235 236 244 public double getTickRadius() { 245 return this.tickRadius; 246 } 247 248 255 public void setTickRadius(double radius) { 256 this.tickRadius = radius; 258 notifyListeners(new DialLayerChangeEvent(this)); 259 } 260 261 268 public double getMajorTickIncrement() { 269 return this.majorTickIncrement; 270 } 271 272 279 public void setMajorTickIncrement(double increment) { 280 this.majorTickIncrement = increment; 282 notifyListeners(new DialLayerChangeEvent(this)); 283 } 284 285 294 public double getMajorTickLength() { 295 return this.majorTickLength; 296 } 297 298 305 public void setMajorTickLength(double length) { 306 this.majorTickLength = length; 308 notifyListeners(new DialLayerChangeEvent(this)); 309 } 310 311 318 public Paint getMajorTickPaint() { 319 return this.majorTickPaint; 320 } 321 322 329 public void setMajorTickPaint(Paint paint) { 330 if (paint == null) { 331 throw new IllegalArgumentException ("Null 'paint' argument."); 332 } 333 this.majorTickPaint = paint; 334 notifyListeners(new DialLayerChangeEvent(this)); 335 } 336 337 344 public Stroke getMajorTickStroke() { 345 return this.majorTickStroke; 346 } 347 348 355 public void setMajorTickStroke(Stroke stroke) { 356 if (stroke == null) { 357 throw new IllegalArgumentException ("Null 'stroke' argument."); 358 } 359 this.majorTickStroke = stroke; 360 notifyListeners(new DialLayerChangeEvent(this)); 361 } 362 363 370 public int getMinorTickCount() { 371 return this.minorTickCount; 372 } 373 374 381 public void setMinorTickCount(int count) { 382 this.minorTickCount = count; 384 notifyListeners(new DialLayerChangeEvent(this)); 385 } 386 387 396 public double getMinorTickLength() { 397 return this.minorTickLength; 398 } 399 400 407 public void setMinorTickLength(double length) { 408 this.minorTickLength = length; 410 notifyListeners(new DialLayerChangeEvent(this)); 411 } 412 413 420 public double getTickLabelOffset() { 421 return this.tickLabelOffset; 422 } 423 424 431 public void setTickLabelOffset(double offset) { 432 this.tickLabelOffset = offset; 433 notifyListeners(new DialLayerChangeEvent(this)); 434 } 435 436 443 public Font getTickLabelFont() { 444 return this.tickLabelFont; 445 } 446 447 454 public void setTickLabelFont(Font font) { 455 if (font == null) { 456 throw new IllegalArgumentException ("Null 'font' argument."); 457 } 458 this.tickLabelFont = font; 459 notifyListeners(new DialLayerChangeEvent(this)); 460 } 461 462 467 public Paint getTickLabelPaint() { 468 return this.tickLabelPaint; 469 } 470 471 476 public void setTickLabelPaint(Paint paint) { 477 this.tickLabelPaint = paint; 479 notifyListeners(new DialLayerChangeEvent(this)); 480 } 481 482 488 public boolean getTickLabelsVisible() { 489 return this.tickLabelsVisible; 490 } 491 492 499 public void setTickLabelsVisible(boolean visible) { 500 this.tickLabelsVisible = visible; 501 notifyListeners(new DialLayerChangeEvent(this)); 502 } 503 504 510 public boolean getFirstTickLabelVisible() { 511 return this.firstTickLabelVisible; 512 } 513 514 521 public void setFirstTickLabelVisible(boolean visible) { 522 this.firstTickLabelVisible = visible; 523 notifyListeners(new DialLayerChangeEvent(this)); 524 } 525 526 532 public boolean isClippedToWindow() { 533 return true; 534 } 535 536 546 public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame, 547 Rectangle2D view) { 548 549 Rectangle2D arcRect = DialPlot.rectangleByRadius(frame, 550 this.tickRadius, this.tickRadius); 551 Rectangle2D arcRectInner = DialPlot.rectangleByRadius(frame, 552 this.tickRadius - this.minorTickLength, 553 this.tickRadius - this.minorTickLength); 554 Rectangle2D arcRectForLabels = DialPlot.rectangleByRadius(frame, 555 this.tickRadius - this.tickLabelOffset, 556 this.tickRadius - this.tickLabelOffset); 557 558 boolean firstLabel = true; 559 560 Arc2D arc = new Arc2D.Double (); 561 for (double v = this.lowerBound; v <= this.upperBound; 562 v += this.majorTickIncrement) { 563 arc.setArc(arcRect, this.startAngle, valueToAngle(v) 564 - this.startAngle, Arc2D.OPEN); 565 Point2D pt0 = arc.getEndPoint(); 566 arc.setArc(arcRectInner, this.startAngle, valueToAngle(v) 567 - this.startAngle, Arc2D.OPEN); 568 Point2D pt1 = arc.getEndPoint(); 569 g2.setPaint(this.majorTickPaint); 570 g2.setStroke(this.majorTickStroke); 571 g2.draw(new Line2D.Double (pt0, pt1)); 572 arc.setArc(arcRectForLabels, this.startAngle, valueToAngle(v) 573 - this.startAngle, Arc2D.OPEN); 574 Point2D pt2 = arc.getEndPoint(); 575 576 if (tickLabelsVisible) { 577 if (!firstLabel || this.firstTickLabelVisible) { 578 g2.setFont(this.tickLabelFont); 579 TextUtilities.drawAlignedString(String.valueOf(v), g2, 580 (float) pt2.getX(), (float) pt2.getY(), 581 TextAnchor.CENTER); 582 } 583 } 584 firstLabel = false; 585 586 if (this.minorTickCount > 0) { 588 double minorTickIncrement = this.majorTickIncrement 589 / (this.minorTickCount + 1); 590 for (int i = 0; i < this.minorTickCount; i++) { 591 double vv = v + ((i + 1) * minorTickIncrement); 592 if (vv >= this.upperBound) { 593 break; 594 } 595 double angle = valueToAngle(vv); 596 597 arc.setArc(arcRect, this.startAngle, angle 598 - this.startAngle, Arc2D.OPEN); 599 pt0 = arc.getEndPoint(); 600 arc.setArc(arcRectInner, this.startAngle, angle 601 - this.startAngle, Arc2D.OPEN); 602 Point2D pt3 = arc.getEndPoint(); 603 g2.setStroke(new BasicStroke (1.0f)); 604 g2.draw(new Line2D.Double (pt0, pt3)); 605 } 606 } 607 608 } 609 } 610 611 619 public double valueToAngle(double value) { 620 double range = this.upperBound - this.lowerBound; 621 double unit = this.extent / range; 622 return this.startAngle + unit * (value - this.lowerBound); 623 } 624 625 public double angleToValue(double angle) { 626 return Double.NaN; } 628 629 637 public boolean equals(Object obj) { 638 if (obj == this) { 639 return true; 640 } 641 if (!(obj instanceof StandardDialScale)) { 642 return false; 643 } 644 StandardDialScale that = (StandardDialScale) obj; 645 if (this.lowerBound != that.lowerBound) { 646 return false; 647 } 648 if (this.upperBound != that.upperBound) { 649 return false; 650 } 651 if (this.startAngle != that.startAngle) { 652 return false; 653 } 654 if (this.extent != that.extent) { 655 return false; 656 } 657 if (this.tickRadius != that.tickRadius) { 658 return false; 659 } 660 if (this.majorTickIncrement != that.majorTickIncrement) { 661 return false; 662 } 663 if (this.majorTickLength != that.majorTickLength) { 664 return false; 665 } 666 if (!PaintUtilities.equal(this.majorTickPaint, that.majorTickPaint)) { 667 return false; 668 } 669 if (!this.majorTickStroke.equals(that.majorTickStroke)) { 670 return false; 671 } 672 if (this.minorTickCount != that.minorTickCount) { 673 return false; 674 } 675 if (this.minorTickLength != that.minorTickLength) { 676 return false; 677 } 678 if (this.tickLabelOffset != that.tickLabelOffset) { 679 return false; 680 } 681 if (!this.tickLabelFont.equals(that.tickLabelFont)) { 682 return false; 683 } 684 if (!PaintUtilities.equal(this.tickLabelPaint, that.tickLabelPaint)) { 685 return false; 686 } 687 return true; 688 } 689 690 697 public Object clone() throws CloneNotSupportedException { 698 return super.clone(); 699 } 700 701 708 private void writeObject(ObjectOutputStream stream) throws IOException { 709 stream.defaultWriteObject(); 710 SerialUtilities.writePaint(this.majorTickPaint, stream); 711 SerialUtilities.writeStroke(this.majorTickStroke, stream); 712 SerialUtilities.writePaint(this.tickLabelPaint, stream); 713 } 714 715 723 private void readObject(ObjectInputStream stream) 724 throws IOException , ClassNotFoundException { 725 stream.defaultReadObject(); 726 this.majorTickPaint = SerialUtilities.readPaint(stream); 727 this.majorTickStroke = SerialUtilities.readStroke(stream); 728 this.tickLabelPaint = SerialUtilities.readPaint(stream); 729 } 730 731 } 732 | Popular Tags |