1 47 48 package org.jfree.chart.title; 49 50 import java.awt.Graphics2D ; 51 import java.awt.Paint ; 52 import java.awt.Shape ; 53 import java.awt.Stroke ; 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 60 import org.jfree.chart.block.AbstractBlock; 61 import org.jfree.chart.block.Block; 62 import org.jfree.chart.block.LengthConstraintType; 63 import org.jfree.chart.block.RectangleConstraint; 64 import org.jfree.io.SerialUtilities; 65 import org.jfree.ui.RectangleAnchor; 66 import org.jfree.ui.Size2D; 67 import org.jfree.util.ObjectUtilities; 68 import org.jfree.util.PaintUtilities; 69 import org.jfree.util.PublicCloneable; 70 import org.jfree.util.ShapeUtilities; 71 72 75 public class LegendGraphic extends AbstractBlock 76 implements Block, PublicCloneable { 77 78 82 private boolean shapeVisible; 83 84 88 private transient Shape shape; 89 90 93 private RectangleAnchor shapeLocation; 94 95 99 private RectangleAnchor shapeAnchor; 100 101 102 private boolean shapeFilled; 103 104 105 private transient Paint fillPaint; 106 107 108 private boolean shapeOutlineVisible; 109 110 111 private transient Paint outlinePaint; 112 113 114 private transient Stroke outlineStroke; 115 116 120 private boolean lineVisible; 121 122 123 private transient Shape line; 124 125 126 private transient Stroke lineStroke; 127 128 129 private transient Paint linePaint; 130 131 137 public LegendGraphic(Shape shape, Paint fillPaint) { 138 if (shape == null) { 139 throw new IllegalArgumentException ("Null 'shape' argument."); 140 } 141 if (fillPaint == null) { 142 throw new IllegalArgumentException ("Null 'fillPaint' argument."); 143 } 144 this.shapeVisible = true; 145 this.shape = shape; 146 this.shapeAnchor = RectangleAnchor.CENTER; 147 this.shapeLocation = RectangleAnchor.CENTER; 148 this.shapeFilled = true; 149 this.fillPaint = fillPaint; 150 setPadding(2.0, 2.0, 2.0, 2.0); 151 } 152 153 159 public boolean isShapeVisible() { 160 return this.shapeVisible; 161 } 162 163 169 public void setShapeVisible(boolean visible) { 170 this.shapeVisible = visible; 171 } 172 173 178 public Shape getShape() { 179 return this.shape; 180 } 181 182 187 public void setShape(Shape shape) { 188 this.shape = shape; 189 } 190 191 197 public boolean isShapeFilled() { 198 return this.shapeFilled; 199 } 200 201 207 public void setShapeFilled(boolean filled) { 208 this.shapeFilled = filled; 209 } 210 211 216 public Paint getFillPaint() { 217 return this.fillPaint; 218 } 219 220 225 public void setFillPaint(Paint paint) { 226 this.fillPaint = paint; 227 } 228 229 234 public boolean isShapeOutlineVisible() { 235 return this.shapeOutlineVisible; 236 } 237 238 244 public void setShapeOutlineVisible(boolean visible) { 245 this.shapeOutlineVisible = visible; 246 } 247 248 253 public Paint getOutlinePaint() { 254 return this.outlinePaint; 255 } 256 257 262 public void setOutlinePaint(Paint paint) { 263 this.outlinePaint = paint; 264 } 265 266 271 public Stroke getOutlineStroke() { 272 return this.outlineStroke; 273 } 274 275 280 public void setOutlineStroke(Stroke stroke) { 281 this.outlineStroke = stroke; 282 } 283 284 289 public RectangleAnchor getShapeAnchor() { 290 return this.shapeAnchor; 291 } 292 293 299 public void setShapeAnchor(RectangleAnchor anchor) { 300 if (anchor == null) { 301 throw new IllegalArgumentException ("Null 'anchor' argument."); 302 } 303 this.shapeAnchor = anchor; 304 } 305 306 311 public RectangleAnchor getShapeLocation() { 312 return this.shapeLocation; 313 } 314 315 321 public void setShapeLocation(RectangleAnchor location) { 322 if (location == null) { 323 throw new IllegalArgumentException ("Null 'location' argument."); 324 } 325 this.shapeLocation = location; 326 } 327 328 333 public boolean isLineVisible() { 334 return this.lineVisible; 335 } 336 337 342 public void setLineVisible(boolean visible) { 343 this.lineVisible = visible; 344 } 345 346 351 public Shape getLine() { 352 return this.line; 353 } 354 355 361 public void setLine(Shape line) { 362 this.line = line; 363 } 364 365 370 public Paint getLinePaint() { 371 return this.linePaint; 372 } 373 374 379 public void setLinePaint(Paint paint) { 380 this.linePaint = paint; 381 } 382 383 388 public Stroke getLineStroke() { 389 return this.lineStroke; 390 } 391 392 397 public void setLineStroke(Stroke stroke) { 398 this.lineStroke = stroke; 399 } 400 401 410 public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) { 411 RectangleConstraint contentConstraint = toContentConstraint(constraint); 412 LengthConstraintType w = contentConstraint.getWidthConstraintType(); 413 LengthConstraintType h = contentConstraint.getHeightConstraintType(); 414 Size2D contentSize = null; 415 if (w == LengthConstraintType.NONE) { 416 if (h == LengthConstraintType.NONE) { 417 contentSize = arrangeNN(g2); 418 } 419 else if (h == LengthConstraintType.RANGE) { 420 throw new RuntimeException ("Not yet implemented."); 421 } 422 else if (h == LengthConstraintType.FIXED) { 423 throw new RuntimeException ("Not yet implemented."); 424 } 425 } 426 else if (w == LengthConstraintType.RANGE) { 427 if (h == LengthConstraintType.NONE) { 428 throw new RuntimeException ("Not yet implemented."); 429 } 430 else if (h == LengthConstraintType.RANGE) { 431 throw new RuntimeException ("Not yet implemented."); 432 } 433 else if (h == LengthConstraintType.FIXED) { 434 throw new RuntimeException ("Not yet implemented."); 435 } 436 } 437 else if (w == LengthConstraintType.FIXED) { 438 if (h == LengthConstraintType.NONE) { 439 throw new RuntimeException ("Not yet implemented."); 440 } 441 else if (h == LengthConstraintType.RANGE) { 442 throw new RuntimeException ("Not yet implemented."); 443 } 444 else if (h == LengthConstraintType.FIXED) { 445 contentSize = new Size2D( 446 contentConstraint.getWidth(), 447 contentConstraint.getHeight() 448 ); 449 } 450 } 451 return new Size2D( 452 calculateTotalWidth(contentSize.getWidth()), 453 calculateTotalHeight(contentSize.getHeight()) 454 ); 455 } 456 457 466 protected Size2D arrangeNN(Graphics2D g2) { 467 Rectangle2D contentSize = new Rectangle2D.Double (); 468 if (this.line != null) { 469 contentSize.setRect(this.line.getBounds2D()); 470 } 471 if (this.shape != null) { 472 contentSize = contentSize.createUnion(this.shape.getBounds2D()); 473 } 474 return new Size2D(contentSize.getWidth(), contentSize.getHeight()); 475 } 476 477 483 public void draw(Graphics2D g2, Rectangle2D area) { 484 485 area = trimMargin(area); 486 drawBorder(g2, area); 487 area = trimBorder(area); 488 area = trimPadding(area); 489 490 if (this.lineVisible) { 491 Point2D location = RectangleAnchor.coordinates( 492 area, this.shapeLocation 493 ); 494 Shape aLine = ShapeUtilities.createTranslatedShape( 495 getLine(), this.shapeAnchor, location.getX(), location.getY() 496 ); 497 g2.setPaint(this.linePaint); 498 g2.setStroke(this.lineStroke); 499 g2.draw(aLine); 500 } 501 502 if (this.shapeVisible) { 503 Point2D location = RectangleAnchor.coordinates( 504 area, this.shapeLocation 505 ); 506 507 Shape s = ShapeUtilities.createTranslatedShape( 508 this.shape, this.shapeAnchor, location.getX(), location.getY() 509 ); 510 if (this.shapeFilled) { 511 g2.setPaint(this.fillPaint); 512 g2.fill(s); 513 } 514 if (this.shapeOutlineVisible) { 515 g2.setPaint(this.outlinePaint); 516 g2.setStroke(this.outlineStroke); 517 g2.draw(s); 518 } 519 } 520 521 } 522 523 532 public Object draw(Graphics2D g2, Rectangle2D area, Object params) { 533 draw(g2, area); 534 return null; 535 } 536 537 545 public boolean equals(Object obj) { 546 if (!(obj instanceof LegendGraphic)) { 547 return false; 548 } 549 LegendGraphic that = (LegendGraphic) obj; 550 if (this.shapeVisible != that.shapeVisible) { 551 return false; 552 } 553 if (!ShapeUtilities.equal(this.shape, that.shape)) { 554 return false; 555 } 556 if (this.shapeFilled != that.shapeFilled) { 557 return false; 558 } 559 if (!PaintUtilities.equal(this.fillPaint, that.fillPaint)) { 560 return false; 561 } 562 if (this.shapeOutlineVisible != that.shapeOutlineVisible) { 563 return false; 564 } 565 if (!PaintUtilities.equal(this.outlinePaint, that.outlinePaint)) { 566 return false; 567 } 568 if (!ObjectUtilities.equal(this.outlineStroke, that.outlineStroke)) { 569 return false; 570 } 571 if (this.shapeAnchor != that.shapeAnchor) { 572 return false; 573 } 574 if (this.shapeLocation != that.shapeLocation) { 575 return false; 576 } 577 if (this.lineVisible != that.lineVisible) { 578 return false; 579 } 580 if (!ShapeUtilities.equal(this.line, that.line)) { 581 return false; 582 } 583 if (!PaintUtilities.equal(this.linePaint, that.linePaint)) { 584 return false; 585 } 586 if (!ObjectUtilities.equal(this.lineStroke, that.lineStroke)) { 587 return false; 588 } 589 if (!super.equals(obj)) { 590 return false; 591 } 592 return true; 593 } 594 595 602 public Object clone() throws CloneNotSupportedException { 603 return super.clone(); 604 } 605 606 613 private void writeObject(ObjectOutputStream stream) throws IOException { 614 stream.defaultWriteObject(); 615 SerialUtilities.writeShape(this.shape, stream); 616 SerialUtilities.writePaint(this.fillPaint, stream); 617 SerialUtilities.writePaint(this.outlinePaint, stream); 618 SerialUtilities.writeStroke(this.outlineStroke, stream); 619 SerialUtilities.writeShape(this.line, stream); 620 SerialUtilities.writePaint(this.linePaint, stream); 621 SerialUtilities.writeStroke(this.lineStroke, stream); 622 } 623 624 632 private void readObject(ObjectInputStream stream) 633 throws IOException , ClassNotFoundException 634 { 635 stream.defaultReadObject(); 636 this.shape = SerialUtilities.readShape(stream); 637 this.fillPaint = SerialUtilities.readPaint(stream); 638 this.outlinePaint = SerialUtilities.readPaint(stream); 639 this.outlineStroke = SerialUtilities.readStroke(stream); 640 this.line = SerialUtilities.readShape(stream); 641 this.linePaint = SerialUtilities.readPaint(stream); 642 this.lineStroke = SerialUtilities.readStroke(stream); 643 } 644 645 } 646 | Popular Tags |