001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2020, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * --------------------- 028 * XYTextAnnotation.java 029 * --------------------- 030 * (C) Copyright 2002-2020, by Object Refinery Limited and Contributors. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): Peter Kolb (patch 2809117); 034 * 035 */ 036 037package org.jfree.chart.annotations; 038 039import java.awt.BasicStroke; 040import java.awt.Color; 041import java.awt.Font; 042import java.awt.Graphics2D; 043import java.awt.Paint; 044import java.awt.Shape; 045import java.awt.Stroke; 046import java.awt.geom.Rectangle2D; 047import java.io.IOException; 048import java.io.ObjectInputStream; 049import java.io.ObjectOutputStream; 050import java.io.Serializable; 051 052import org.jfree.chart.HashUtils; 053import org.jfree.chart.axis.ValueAxis; 054import org.jfree.chart.event.AnnotationChangeEvent; 055import org.jfree.chart.plot.Plot; 056import org.jfree.chart.plot.PlotOrientation; 057import org.jfree.chart.plot.PlotRenderingInfo; 058import org.jfree.chart.plot.XYPlot; 059import org.jfree.chart.text.TextUtils; 060import org.jfree.chart.ui.RectangleEdge; 061import org.jfree.chart.ui.TextAnchor; 062import org.jfree.chart.util.PaintUtils; 063import org.jfree.chart.util.Args; 064import org.jfree.chart.util.PublicCloneable; 065import org.jfree.chart.util.SerialUtils; 066 067/** 068 * A text annotation that can be placed at a particular (x, y) location on an 069 * {@link XYPlot}. 070 */ 071public class XYTextAnnotation extends AbstractXYAnnotation 072 implements Cloneable, PublicCloneable, Serializable { 073 074 /** For serialization. */ 075 private static final long serialVersionUID = -2946063342782506328L; 076 077 /** The default font. */ 078 public static final Font DEFAULT_FONT = new Font("SansSerif", Font.PLAIN, 079 10); 080 081 /** The default paint. */ 082 public static final Paint DEFAULT_PAINT = Color.BLACK; 083 084 /** The default text anchor. */ 085 public static final TextAnchor DEFAULT_TEXT_ANCHOR = TextAnchor.CENTER; 086 087 /** The default rotation anchor. */ 088 public static final TextAnchor DEFAULT_ROTATION_ANCHOR = TextAnchor.CENTER; 089 090 /** The default rotation angle. */ 091 public static final double DEFAULT_ROTATION_ANGLE = 0.0; 092 093 /** The text. */ 094 private String text; 095 096 /** The font. */ 097 private Font font; 098 099 /** The paint. */ 100 private transient Paint paint; 101 102 /** The x-coordinate. */ 103 private double x; 104 105 /** The y-coordinate. */ 106 private double y; 107 108 /** The text anchor (to be aligned with (x, y)). */ 109 private TextAnchor textAnchor; 110 111 /** The rotation anchor. */ 112 private TextAnchor rotationAnchor; 113 114 /** The rotation angle. */ 115 private double rotationAngle; 116 117 /** The background paint (possibly null). */ 118 private transient Paint backgroundPaint; 119 120 /** The flag that controls the visibility of the outline. */ 121 private boolean outlineVisible; 122 123 /** The outline paint (never null). */ 124 private transient Paint outlinePaint; 125 126 /** The outline stroke (never null). */ 127 private transient Stroke outlineStroke; 128 129 /** 130 * Creates a new annotation to be displayed at the given coordinates. The 131 * coordinates are specified in data space (they will be converted to 132 * Java2D space for display). 133 * 134 * @param text the text ({@code null} not permitted). 135 * @param x the x-coordinate (in data space). 136 * @param y the y-coordinate (in data space). 137 */ 138 public XYTextAnnotation(String text, double x, double y) { 139 super(); 140 Args.nullNotPermitted(text, "text"); 141 this.text = text; 142 this.font = DEFAULT_FONT; 143 this.paint = DEFAULT_PAINT; 144 this.x = x; 145 this.y = y; 146 this.textAnchor = DEFAULT_TEXT_ANCHOR; 147 this.rotationAnchor = DEFAULT_ROTATION_ANCHOR; 148 this.rotationAngle = DEFAULT_ROTATION_ANGLE; 149 150 // by default the outline and background won't be visible 151 this.backgroundPaint = null; 152 this.outlineVisible = false; 153 this.outlinePaint = Color.BLACK; 154 this.outlineStroke = new BasicStroke(0.5f); 155 } 156 157 /** 158 * Returns the text for the annotation. 159 * 160 * @return The text (never {@code null}). 161 * 162 * @see #setText(String) 163 */ 164 public String getText() { 165 return this.text; 166 } 167 168 /** 169 * Sets the text for the annotation. 170 * 171 * @param text the text ({@code null} not permitted). 172 * 173 * @see #getText() 174 */ 175 public void setText(String text) { 176 Args.nullNotPermitted(text, "text"); 177 this.text = text; 178 fireAnnotationChanged(); 179 } 180 181 /** 182 * Returns the font for the annotation. 183 * 184 * @return The font (never {@code null}). 185 * 186 * @see #setFont(Font) 187 */ 188 public Font getFont() { 189 return this.font; 190 } 191 192 /** 193 * Sets the font for the annotation and sends an 194 * {@link AnnotationChangeEvent} to all registered listeners. 195 * 196 * @param font the font ({@code null} not permitted). 197 * 198 * @see #getFont() 199 */ 200 public void setFont(Font font) { 201 Args.nullNotPermitted(font, "font"); 202 this.font = font; 203 fireAnnotationChanged(); 204 } 205 206 /** 207 * Returns the paint for the annotation. 208 * 209 * @return The paint (never {@code null}). 210 * 211 * @see #setPaint(Paint) 212 */ 213 public Paint getPaint() { 214 return this.paint; 215 } 216 217 /** 218 * Sets the paint for the annotation and sends an 219 * {@link AnnotationChangeEvent} to all registered listeners. 220 * 221 * @param paint the paint ({@code null} not permitted). 222 * 223 * @see #getPaint() 224 */ 225 public void setPaint(Paint paint) { 226 Args.nullNotPermitted(paint, "paint"); 227 this.paint = paint; 228 fireAnnotationChanged(); 229 } 230 231 /** 232 * Returns the text anchor. 233 * 234 * @return The text anchor (never {@code null}). 235 * 236 * @see #setTextAnchor(TextAnchor) 237 */ 238 public TextAnchor getTextAnchor() { 239 return this.textAnchor; 240 } 241 242 /** 243 * Sets the text anchor (the point on the text bounding rectangle that is 244 * aligned to the (x, y) coordinate of the annotation) and sends an 245 * {@link AnnotationChangeEvent} to all registered listeners. 246 * 247 * @param anchor the anchor point ({@code null} not permitted). 248 * 249 * @see #getTextAnchor() 250 */ 251 public void setTextAnchor(TextAnchor anchor) { 252 Args.nullNotPermitted(anchor, "anchor"); 253 this.textAnchor = anchor; 254 fireAnnotationChanged(); 255 } 256 257 /** 258 * Returns the rotation anchor. 259 * 260 * @return The rotation anchor point (never {@code null}). 261 * 262 * @see #setRotationAnchor(TextAnchor) 263 */ 264 public TextAnchor getRotationAnchor() { 265 return this.rotationAnchor; 266 } 267 268 /** 269 * Sets the rotation anchor point and sends an 270 * {@link AnnotationChangeEvent} to all registered listeners. 271 * 272 * @param anchor the anchor ({@code null} not permitted). 273 * 274 * @see #getRotationAnchor() 275 */ 276 public void setRotationAnchor(TextAnchor anchor) { 277 Args.nullNotPermitted(anchor, "anchor"); 278 this.rotationAnchor = anchor; 279 fireAnnotationChanged(); 280 } 281 282 /** 283 * Returns the rotation angle. 284 * 285 * @return The rotation angle. 286 * 287 * @see #setRotationAngle(double) 288 */ 289 public double getRotationAngle() { 290 return this.rotationAngle; 291 } 292 293 /** 294 * Sets the rotation angle and sends an {@link AnnotationChangeEvent} to 295 * all registered listeners. The angle is measured clockwise in radians. 296 * 297 * @param angle the angle (in radians). 298 * 299 * @see #getRotationAngle() 300 */ 301 public void setRotationAngle(double angle) { 302 this.rotationAngle = angle; 303 fireAnnotationChanged(); 304 } 305 306 /** 307 * Returns the x coordinate for the text anchor point (measured against the 308 * domain axis). 309 * 310 * @return The x coordinate (in data space). 311 * 312 * @see #setX(double) 313 */ 314 public double getX() { 315 return this.x; 316 } 317 318 /** 319 * Sets the x coordinate for the text anchor point (measured against the 320 * domain axis) and sends an {@link AnnotationChangeEvent} to all 321 * registered listeners. 322 * 323 * @param x the x coordinate (in data space). 324 * 325 * @see #getX() 326 */ 327 public void setX(double x) { 328 this.x = x; 329 fireAnnotationChanged(); 330 } 331 332 /** 333 * Returns the y coordinate for the text anchor point (measured against the 334 * range axis). 335 * 336 * @return The y coordinate (in data space). 337 * 338 * @see #setY(double) 339 */ 340 public double getY() { 341 return this.y; 342 } 343 344 /** 345 * Sets the y coordinate for the text anchor point (measured against the 346 * range axis) and sends an {@link AnnotationChangeEvent} to all registered 347 * listeners. 348 * 349 * @param y the y coordinate. 350 * 351 * @see #getY() 352 */ 353 public void setY(double y) { 354 this.y = y; 355 fireAnnotationChanged(); 356 } 357 358 /** 359 * Returns the background paint for the annotation. 360 * 361 * @return The background paint (possibly {@code null}). 362 * 363 * @see #setBackgroundPaint(Paint) 364 */ 365 public Paint getBackgroundPaint() { 366 return this.backgroundPaint; 367 } 368 369 /** 370 * Sets the background paint for the annotation and sends an 371 * {@link AnnotationChangeEvent} to all registered listeners. 372 * 373 * @param paint the paint ({@code null} permitted). 374 * 375 * @see #getBackgroundPaint() 376 */ 377 public void setBackgroundPaint(Paint paint) { 378 this.backgroundPaint = paint; 379 fireAnnotationChanged(); 380 } 381 382 /** 383 * Returns the outline paint for the annotation. 384 * 385 * @return The outline paint (never {@code null}). 386 * 387 * @see #setOutlinePaint(Paint) 388 */ 389 public Paint getOutlinePaint() { 390 return this.outlinePaint; 391 } 392 393 /** 394 * Sets the outline paint for the annotation and sends an 395 * {@link AnnotationChangeEvent} to all registered listeners. 396 * 397 * @param paint the paint ({@code null} not permitted). 398 * 399 * @see #getOutlinePaint() 400 */ 401 public void setOutlinePaint(Paint paint) { 402 Args.nullNotPermitted(paint, "paint"); 403 this.outlinePaint = paint; 404 fireAnnotationChanged(); 405 } 406 407 /** 408 * Returns the outline stroke for the annotation. 409 * 410 * @return The outline stroke (never {@code null}). 411 * 412 * @see #setOutlineStroke(Stroke) 413 */ 414 public Stroke getOutlineStroke() { 415 return this.outlineStroke; 416 } 417 418 /** 419 * Sets the outline stroke for the annotation and sends an 420 * {@link AnnotationChangeEvent} to all registered listeners. 421 * 422 * @param stroke the stroke ({@code null} not permitted). 423 * 424 * @see #getOutlineStroke() 425 */ 426 public void setOutlineStroke(Stroke stroke) { 427 Args.nullNotPermitted(stroke, "stroke"); 428 this.outlineStroke = stroke; 429 fireAnnotationChanged(); 430 } 431 432 /** 433 * Returns the flag that controls whether or not the outline is drawn. 434 * 435 * @return A boolean. 436 */ 437 public boolean isOutlineVisible() { 438 return this.outlineVisible; 439 } 440 441 /** 442 * Sets the flag that controls whether or not the outline is drawn and 443 * sends an {@link AnnotationChangeEvent} to all registered listeners. 444 * 445 * @param visible the new flag value. 446 */ 447 public void setOutlineVisible(boolean visible) { 448 this.outlineVisible = visible; 449 fireAnnotationChanged(); 450 } 451 452 /** 453 * Draws the annotation. 454 * 455 * @param g2 the graphics device. 456 * @param plot the plot. 457 * @param dataArea the data area. 458 * @param domainAxis the domain axis. 459 * @param rangeAxis the range axis. 460 * @param rendererIndex the renderer index. 461 * @param info an optional info object that will be populated with 462 * entity information. 463 */ 464 @Override 465 public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, 466 ValueAxis domainAxis, ValueAxis rangeAxis, 467 int rendererIndex, PlotRenderingInfo info) { 468 469 PlotOrientation orientation = plot.getOrientation(); 470 RectangleEdge domainEdge = Plot.resolveDomainAxisLocation( 471 plot.getDomainAxisLocation(), orientation); 472 RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation( 473 plot.getRangeAxisLocation(), orientation); 474 475 float anchorX = (float) domainAxis.valueToJava2D( 476 this.x, dataArea, domainEdge); 477 float anchorY = (float) rangeAxis.valueToJava2D( 478 this.y, dataArea, rangeEdge); 479 480 if (orientation == PlotOrientation.HORIZONTAL) { 481 float tempAnchor = anchorX; 482 anchorX = anchorY; 483 anchorY = tempAnchor; 484 } 485 486 g2.setFont(getFont()); 487 Shape hotspot = TextUtils.calculateRotatedStringBounds( 488 getText(), g2, anchorX, anchorY, getTextAnchor(), 489 getRotationAngle(), getRotationAnchor()); 490 if (this.backgroundPaint != null) { 491 g2.setPaint(this.backgroundPaint); 492 g2.fill(hotspot); 493 } 494 g2.setPaint(getPaint()); 495 TextUtils.drawRotatedString(getText(), g2, anchorX, anchorY, 496 getTextAnchor(), getRotationAngle(), getRotationAnchor()); 497 if (this.outlineVisible) { 498 g2.setStroke(this.outlineStroke); 499 g2.setPaint(this.outlinePaint); 500 g2.draw(hotspot); 501 } 502 503 String toolTip = getToolTipText(); 504 String url = getURL(); 505 if (toolTip != null || url != null) { 506 addEntity(info, hotspot, rendererIndex, toolTip, url); 507 } 508 509 } 510 511 /** 512 * Tests this annotation for equality with an arbitrary object. 513 * 514 * @param obj the object ({@code null} permitted). 515 * 516 * @return A boolean. 517 */ 518 @Override 519 public boolean equals(Object obj) { 520 if (obj == this) { 521 return true; 522 } 523 if (!(obj instanceof XYTextAnnotation)) { 524 return false; 525 } 526 XYTextAnnotation that = (XYTextAnnotation) obj; 527 if (!this.text.equals(that.text)) { 528 return false; 529 } 530 if (this.x != that.x) { 531 return false; 532 } 533 if (this.y != that.y) { 534 return false; 535 } 536 if (!this.font.equals(that.font)) { 537 return false; 538 } 539 if (!PaintUtils.equal(this.paint, that.paint)) { 540 return false; 541 } 542 if (!this.rotationAnchor.equals(that.rotationAnchor)) { 543 return false; 544 } 545 if (this.rotationAngle != that.rotationAngle) { 546 return false; 547 } 548 if (!this.textAnchor.equals(that.textAnchor)) { 549 return false; 550 } 551 if (this.outlineVisible != that.outlineVisible) { 552 return false; 553 } 554 if (!PaintUtils.equal(this.backgroundPaint, that.backgroundPaint)) { 555 return false; 556 } 557 if (!PaintUtils.equal(this.outlinePaint, that.outlinePaint)) { 558 return false; 559 } 560 if (!(this.outlineStroke.equals(that.outlineStroke))) { 561 return false; 562 } 563 return super.equals(obj); 564 } 565 566 /** 567 * Returns a hash code for the object. 568 * 569 * @return A hash code. 570 */ 571 @Override 572 public int hashCode() { 573 int result = 193; 574 result = 37 * result + this.text.hashCode(); 575 result = 37 * result + this.font.hashCode(); 576 result = 37 * result + HashUtils.hashCodeForPaint(this.paint); 577 long temp = Double.doubleToLongBits(this.x); 578 result = 37 * result + (int) (temp ^ (temp >>> 32)); 579 temp = Double.doubleToLongBits(this.y); 580 result = 37 * result + (int) (temp ^ (temp >>> 32)); 581 result = 37 * result + this.textAnchor.hashCode(); 582 result = 37 * result + this.rotationAnchor.hashCode(); 583 temp = Double.doubleToLongBits(this.rotationAngle); 584 result = 37 * result + (int) (temp ^ (temp >>> 32)); 585 return result; 586 } 587 588 /** 589 * Returns a clone of the annotation. 590 * 591 * @return A clone. 592 * 593 * @throws CloneNotSupportedException if the annotation can't be cloned. 594 */ 595 @Override 596 public Object clone() throws CloneNotSupportedException { 597 return super.clone(); 598 } 599 600 /** 601 * Provides serialization support. 602 * 603 * @param stream the output stream. 604 * 605 * @throws IOException if there is an I/O error. 606 */ 607 private void writeObject(ObjectOutputStream stream) throws IOException { 608 stream.defaultWriteObject(); 609 SerialUtils.writePaint(this.paint, stream); 610 SerialUtils.writePaint(this.backgroundPaint, stream); 611 SerialUtils.writePaint(this.outlinePaint, stream); 612 SerialUtils.writeStroke(this.outlineStroke, stream); 613 } 614 615 /** 616 * Provides serialization support. 617 * 618 * @param stream the input stream. 619 * 620 * @throws IOException if there is an I/O error. 621 * @throws ClassNotFoundException if there is a classpath problem. 622 */ 623 private void readObject(ObjectInputStream stream) 624 throws IOException, ClassNotFoundException { 625 stream.defaultReadObject(); 626 this.paint = SerialUtils.readPaint(stream); 627 this.backgroundPaint = SerialUtils.readPaint(stream); 628 this.outlinePaint = SerialUtils.readPaint(stream); 629 this.outlineStroke = SerialUtils.readStroke(stream); 630 } 631 632}