001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2022, by David Gilbert 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-2022, by David Gilbert and Contributors. 031 * 032 * Original Author: David Gilbert; 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.internal.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.api.RectangleEdge; 061import org.jfree.chart.text.TextAnchor; 062import org.jfree.chart.internal.PaintUtils; 063import org.jfree.chart.internal.Args; 064import org.jfree.chart.api.PublicCloneable; 065import org.jfree.chart.internal.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 /** 118 * The background paint (possibly null). 119 */ 120 private transient Paint backgroundPaint; 121 122 /** 123 * The flag that controls the visibility of the outline. 124 */ 125 private boolean outlineVisible; 126 127 /** 128 * The outline paint (never null). 129 */ 130 private transient Paint outlinePaint; 131 132 /** 133 * The outline stroke (never null). 134 */ 135 private transient Stroke outlineStroke; 136 137 /** 138 * Creates a new annotation to be displayed at the given coordinates. The 139 * coordinates are specified in data space (they will be converted to 140 * Java2D space for display). 141 * 142 * @param text the text ({@code null} not permitted). 143 * @param x the x-coordinate (in data space, must be finite). 144 * @param y the y-coordinate (in data space, must be finite). 145 */ 146 public XYTextAnnotation(String text, double x, double y) { 147 super(); 148 Args.nullNotPermitted(text, "text"); 149 Args.requireFinite(x, "x"); 150 Args.requireFinite(y, "y"); 151 this.text = text; 152 this.font = DEFAULT_FONT; 153 this.paint = DEFAULT_PAINT; 154 this.x = x; 155 this.y = y; 156 this.textAnchor = DEFAULT_TEXT_ANCHOR; 157 this.rotationAnchor = DEFAULT_ROTATION_ANCHOR; 158 this.rotationAngle = DEFAULT_ROTATION_ANGLE; 159 160 // by default the outline and background won't be visible 161 this.backgroundPaint = null; 162 this.outlineVisible = false; 163 this.outlinePaint = Color.BLACK; 164 this.outlineStroke = new BasicStroke(0.5f); 165 } 166 167 /** 168 * Returns the text for the annotation. 169 * 170 * @return The text (never {@code null}). 171 * 172 * @see #setText(String) 173 */ 174 public String getText() { 175 return this.text; 176 } 177 178 /** 179 * Sets the text for the annotation. 180 * 181 * @param text the text ({@code null} not permitted). 182 * 183 * @see #getText() 184 */ 185 public void setText(String text) { 186 Args.nullNotPermitted(text, "text"); 187 this.text = text; 188 fireAnnotationChanged(); 189 } 190 191 /** 192 * Returns the font for the annotation. 193 * 194 * @return The font (never {@code null}). 195 * 196 * @see #setFont(Font) 197 */ 198 public Font getFont() { 199 return this.font; 200 } 201 202 /** 203 * Sets the font for the annotation and sends an 204 * {@link AnnotationChangeEvent} to all registered listeners. 205 * 206 * @param font the font ({@code null} not permitted). 207 * 208 * @see #getFont() 209 */ 210 public void setFont(Font font) { 211 Args.nullNotPermitted(font, "font"); 212 this.font = font; 213 fireAnnotationChanged(); 214 } 215 216 /** 217 * Returns the paint for the annotation. 218 * 219 * @return The paint (never {@code null}). 220 * 221 * @see #setPaint(Paint) 222 */ 223 public Paint getPaint() { 224 return this.paint; 225 } 226 227 /** 228 * Sets the paint for the annotation and sends an 229 * {@link AnnotationChangeEvent} to all registered listeners. 230 * 231 * @param paint the paint ({@code null} not permitted). 232 * 233 * @see #getPaint() 234 */ 235 public void setPaint(Paint paint) { 236 Args.nullNotPermitted(paint, "paint"); 237 this.paint = paint; 238 fireAnnotationChanged(); 239 } 240 241 /** 242 * Returns the text anchor. 243 * 244 * @return The text anchor (never {@code null}). 245 * 246 * @see #setTextAnchor(TextAnchor) 247 */ 248 public TextAnchor getTextAnchor() { 249 return this.textAnchor; 250 } 251 252 /** 253 * Sets the text anchor (the point on the text bounding rectangle that is 254 * aligned to the (x, y) coordinate of the annotation) and sends an 255 * {@link AnnotationChangeEvent} to all registered listeners. 256 * 257 * @param anchor the anchor point ({@code null} not permitted). 258 * 259 * @see #getTextAnchor() 260 */ 261 public void setTextAnchor(TextAnchor anchor) { 262 Args.nullNotPermitted(anchor, "anchor"); 263 this.textAnchor = anchor; 264 fireAnnotationChanged(); 265 } 266 267 /** 268 * Returns the rotation anchor. 269 * 270 * @return The rotation anchor point (never {@code null}). 271 * 272 * @see #setRotationAnchor(TextAnchor) 273 */ 274 public TextAnchor getRotationAnchor() { 275 return this.rotationAnchor; 276 } 277 278 /** 279 * Sets the rotation anchor point and sends an 280 * {@link AnnotationChangeEvent} to all registered listeners. 281 * 282 * @param anchor the anchor ({@code null} not permitted). 283 * 284 * @see #getRotationAnchor() 285 */ 286 public void setRotationAnchor(TextAnchor anchor) { 287 Args.nullNotPermitted(anchor, "anchor"); 288 this.rotationAnchor = anchor; 289 fireAnnotationChanged(); 290 } 291 292 /** 293 * Returns the rotation angle. 294 * 295 * @return The rotation angle. 296 * 297 * @see #setRotationAngle(double) 298 */ 299 public double getRotationAngle() { 300 return this.rotationAngle; 301 } 302 303 /** 304 * Sets the rotation angle and sends an {@link AnnotationChangeEvent} to 305 * all registered listeners. The angle is measured clockwise in radians. 306 * 307 * @param angle the angle (in radians). 308 * 309 * @see #getRotationAngle() 310 */ 311 public void setRotationAngle(double angle) { 312 this.rotationAngle = angle; 313 fireAnnotationChanged(); 314 } 315 316 /** 317 * Returns the x coordinate for the text anchor point (measured against the 318 * domain axis). 319 * 320 * @return The x coordinate (in data space). 321 * 322 * @see #setX(double) 323 */ 324 public double getX() { 325 return this.x; 326 } 327 328 /** 329 * Sets the x coordinate for the text anchor point (measured against the 330 * domain axis) and sends an {@link AnnotationChangeEvent} to all 331 * registered listeners. 332 * 333 * @param x the x coordinate (in data space). 334 * 335 * @see #getX() 336 */ 337 public void setX(double x) { 338 Args.requireFinite(x, "x"); 339 this.x = x; 340 fireAnnotationChanged(); 341 } 342 343 /** 344 * Returns the y coordinate for the text anchor point (measured against the 345 * range axis). 346 * 347 * @return The y coordinate (in data space). 348 * 349 * @see #setY(double) 350 */ 351 public double getY() { 352 return this.y; 353 } 354 355 /** 356 * Sets the y coordinate for the text anchor point (measured against the 357 * range axis) and sends an {@link AnnotationChangeEvent} to all registered 358 * listeners. 359 * 360 * @param y the y coordinate. 361 * 362 * @see #getY() 363 */ 364 public void setY(double y) { 365 Args.requireFinite(y, "y"); 366 this.y = y; 367 fireAnnotationChanged(); 368 } 369 370 /** 371 * Returns the background paint for the annotation. 372 * 373 * @return The background paint (possibly {@code null}). 374 * 375 * @see #setBackgroundPaint(Paint) 376 */ 377 public Paint getBackgroundPaint() { 378 return this.backgroundPaint; 379 } 380 381 /** 382 * Sets the background paint for the annotation and sends an 383 * {@link AnnotationChangeEvent} to all registered listeners. 384 * 385 * @param paint the paint ({@code null} permitted). 386 * 387 * @see #getBackgroundPaint() 388 */ 389 public void setBackgroundPaint(Paint paint) { 390 this.backgroundPaint = paint; 391 fireAnnotationChanged(); 392 } 393 394 /** 395 * Returns the outline paint for the annotation. 396 * 397 * @return The outline paint (never {@code null}). 398 * 399 * @see #setOutlinePaint(Paint) 400 */ 401 public Paint getOutlinePaint() { 402 return this.outlinePaint; 403 } 404 405 /** 406 * Sets the outline paint for the annotation and sends an 407 * {@link AnnotationChangeEvent} to all registered listeners. 408 * 409 * @param paint the paint ({@code null} not permitted). 410 * 411 * @see #getOutlinePaint() 412 */ 413 public void setOutlinePaint(Paint paint) { 414 Args.nullNotPermitted(paint, "paint"); 415 this.outlinePaint = paint; 416 fireAnnotationChanged(); 417 } 418 419 /** 420 * Returns the outline stroke for the annotation. 421 * 422 * @return The outline stroke (never {@code null}). 423 * 424 * @see #setOutlineStroke(Stroke) 425 */ 426 public Stroke getOutlineStroke() { 427 return this.outlineStroke; 428 } 429 430 /** 431 * Sets the outline stroke for the annotation and sends an 432 * {@link AnnotationChangeEvent} to all registered listeners. 433 * 434 * @param stroke the stroke ({@code null} not permitted). 435 * 436 * @see #getOutlineStroke() 437 */ 438 public void setOutlineStroke(Stroke stroke) { 439 Args.nullNotPermitted(stroke, "stroke"); 440 this.outlineStroke = stroke; 441 fireAnnotationChanged(); 442 } 443 444 /** 445 * Returns the flag that controls whether or not the outline is drawn. 446 * 447 * @return A boolean. 448 */ 449 public boolean isOutlineVisible() { 450 return this.outlineVisible; 451 } 452 453 /** 454 * Sets the flag that controls whether or not the outline is drawn and 455 * sends an {@link AnnotationChangeEvent} to all registered listeners. 456 * 457 * @param visible the new flag value. 458 */ 459 public void setOutlineVisible(boolean visible) { 460 this.outlineVisible = visible; 461 fireAnnotationChanged(); 462 } 463 464 /** 465 * Draws the annotation. 466 * 467 * @param g2 the graphics device. 468 * @param plot the plot. 469 * @param dataArea the data area. 470 * @param domainAxis the domain axis. 471 * @param rangeAxis the range axis. 472 * @param rendererIndex the renderer index. 473 * @param info an optional info object that will be populated with 474 * entity information. 475 */ 476 @Override 477 public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, 478 ValueAxis domainAxis, ValueAxis rangeAxis, 479 int rendererIndex, PlotRenderingInfo info) { 480 481 PlotOrientation orientation = plot.getOrientation(); 482 RectangleEdge domainEdge = Plot.resolveDomainAxisLocation( 483 plot.getDomainAxisLocation(), orientation); 484 RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation( 485 plot.getRangeAxisLocation(), orientation); 486 487 float anchorX = (float) domainAxis.valueToJava2D( 488 this.x, dataArea, domainEdge); 489 float anchorY = (float) rangeAxis.valueToJava2D( 490 this.y, dataArea, rangeEdge); 491 492 if (orientation == PlotOrientation.HORIZONTAL) { 493 float tempAnchor = anchorX; 494 anchorX = anchorY; 495 anchorY = tempAnchor; 496 } 497 498 g2.setFont(getFont()); 499 Shape hotspot = TextUtils.calculateRotatedStringBounds( 500 getText(), g2, anchorX, anchorY, getTextAnchor(), 501 getRotationAngle(), getRotationAnchor()); 502 if (this.backgroundPaint != null) { 503 g2.setPaint(this.backgroundPaint); 504 g2.fill(hotspot); 505 } 506 g2.setPaint(getPaint()); 507 TextUtils.drawRotatedString(getText(), g2, anchorX, anchorY, 508 getTextAnchor(), getRotationAngle(), getRotationAnchor()); 509 if (this.outlineVisible) { 510 g2.setStroke(this.outlineStroke); 511 g2.setPaint(this.outlinePaint); 512 g2.draw(hotspot); 513 } 514 515 String toolTip = getToolTipText(); 516 String url = getURL(); 517 if (toolTip != null || url != null) { 518 addEntity(info, hotspot, rendererIndex, toolTip, url); 519 } 520 521 } 522 523 /** 524 * Tests this annotation for equality with an arbitrary object. 525 * 526 * @param obj the object ({@code null} permitted). 527 * 528 * @return A boolean. 529 */ 530 @Override 531 public boolean equals(Object obj) { 532 if (obj == this) { 533 return true; 534 } 535 if (!(obj instanceof XYTextAnnotation)) { 536 return false; 537 } 538 XYTextAnnotation that = (XYTextAnnotation) obj; 539 if (!this.text.equals(that.text)) { 540 return false; 541 } 542 if (this.x != that.x) { 543 return false; 544 } 545 if (this.y != that.y) { 546 return false; 547 } 548 if (!this.font.equals(that.font)) { 549 return false; 550 } 551 if (!PaintUtils.equal(this.paint, that.paint)) { 552 return false; 553 } 554 if (!this.rotationAnchor.equals(that.rotationAnchor)) { 555 return false; 556 } 557 if (this.rotationAngle != that.rotationAngle) { 558 return false; 559 } 560 if (!this.textAnchor.equals(that.textAnchor)) { 561 return false; 562 } 563 if (this.outlineVisible != that.outlineVisible) { 564 return false; 565 } 566 if (!PaintUtils.equal(this.backgroundPaint, that.backgroundPaint)) { 567 return false; 568 } 569 if (!PaintUtils.equal(this.outlinePaint, that.outlinePaint)) { 570 return false; 571 } 572 if (!(this.outlineStroke.equals(that.outlineStroke))) { 573 return false; 574 } 575 return super.equals(obj); 576 } 577 578 /** 579 * Returns a hash code for the object. 580 * 581 * @return A hash code. 582 */ 583 @Override 584 public int hashCode() { 585 int result = 193; 586 result = 37 * result + this.text.hashCode(); 587 result = 37 * result + this.font.hashCode(); 588 result = 37 * result + HashUtils.hashCodeForPaint(this.paint); 589 long temp = Double.doubleToLongBits(this.x); 590 result = 37 * result + (int) (temp ^ (temp >>> 32)); 591 temp = Double.doubleToLongBits(this.y); 592 result = 37 * result + (int) (temp ^ (temp >>> 32)); 593 result = 37 * result + this.textAnchor.hashCode(); 594 result = 37 * result + this.rotationAnchor.hashCode(); 595 temp = Double.doubleToLongBits(this.rotationAngle); 596 result = 37 * result + (int) (temp ^ (temp >>> 32)); 597 return result; 598 } 599 600 /** 601 * Returns a clone of the annotation. 602 * 603 * @return A clone. 604 * 605 * @throws CloneNotSupportedException if the annotation can't be cloned. 606 */ 607 @Override 608 public Object clone() throws CloneNotSupportedException { 609 return super.clone(); 610 } 611 612 /** 613 * Provides serialization support. 614 * 615 * @param stream the output stream. 616 * 617 * @throws IOException if there is an I/O error. 618 */ 619 private void writeObject(ObjectOutputStream stream) throws IOException { 620 stream.defaultWriteObject(); 621 SerialUtils.writePaint(this.paint, stream); 622 SerialUtils.writePaint(this.backgroundPaint, stream); 623 SerialUtils.writePaint(this.outlinePaint, stream); 624 SerialUtils.writeStroke(this.outlineStroke, stream); 625 } 626 627 /** 628 * Provides serialization support. 629 * 630 * @param stream the input stream. 631 * 632 * @throws IOException if there is an I/O error. 633 * @throws ClassNotFoundException if there is a classpath problem. 634 */ 635 private void readObject(ObjectInputStream stream) 636 throws IOException, ClassNotFoundException { 637 stream.defaultReadObject(); 638 this.paint = SerialUtils.readPaint(stream); 639 this.backgroundPaint = SerialUtils.readPaint(stream); 640 this.outlinePaint = SerialUtils.readPaint(stream); 641 this.outlineStroke = SerialUtils.readStroke(stream); 642 } 643 644}