1 50 51 package org.jfree.chart.annotations; 52 53 import java.awt.Color ; 54 import java.awt.Font ; 55 import java.awt.Graphics2D ; 56 import java.awt.Paint ; 57 import java.awt.Shape ; 58 import java.awt.geom.Rectangle2D ; 59 import java.io.IOException ; 60 import java.io.ObjectInputStream ; 61 import java.io.ObjectOutputStream ; 62 import java.io.Serializable ; 63 64 import org.jfree.chart.axis.ValueAxis; 65 import org.jfree.chart.plot.Plot; 66 import org.jfree.chart.plot.PlotOrientation; 67 import org.jfree.chart.plot.PlotRenderingInfo; 68 import org.jfree.chart.plot.XYPlot; 69 import org.jfree.io.SerialUtilities; 70 import org.jfree.text.TextUtilities; 71 import org.jfree.ui.RectangleEdge; 72 import org.jfree.ui.TextAnchor; 73 import org.jfree.util.PaintUtilities; 74 import org.jfree.util.PublicCloneable; 75 76 80 public class XYTextAnnotation extends AbstractXYAnnotation 81 implements Cloneable , PublicCloneable, 82 Serializable { 83 84 85 private static final long serialVersionUID = -2946063342782506328L; 86 87 88 public static final Font DEFAULT_FONT 89 = new Font ("SansSerif", Font.PLAIN, 10); 90 91 92 public static final Paint DEFAULT_PAINT = Color.black; 93 94 95 public static final TextAnchor DEFAULT_TEXT_ANCHOR = TextAnchor.CENTER; 96 97 98 public static final TextAnchor DEFAULT_ROTATION_ANCHOR = TextAnchor.CENTER; 99 100 101 public static final double DEFAULT_ROTATION_ANGLE = 0.0; 102 103 104 private String text; 105 106 107 private Font font; 108 109 110 private transient Paint paint; 111 112 113 private double x; 114 115 116 private double y; 117 118 119 private TextAnchor textAnchor; 120 121 122 private TextAnchor rotationAnchor; 123 124 125 private double rotationAngle; 126 127 136 public XYTextAnnotation(String text, double x, double y) { 137 if (text == null) { 138 throw new IllegalArgumentException ("Null 'text' argument."); 139 } 140 this.text = text; 141 this.font = DEFAULT_FONT; 142 this.paint = DEFAULT_PAINT; 143 this.x = x; 144 this.y = y; 145 this.textAnchor = DEFAULT_TEXT_ANCHOR; 146 this.rotationAnchor = DEFAULT_ROTATION_ANCHOR; 147 this.rotationAngle = DEFAULT_ROTATION_ANGLE; 148 } 149 150 155 public String getText() { 156 return this.text; 157 } 158 159 164 public void setText(String text) { 165 this.text = text; 166 } 167 168 173 public Font getFont() { 174 return this.font; 175 } 176 177 182 public void setFont(Font font) { 183 this.font = font; 184 } 185 186 191 public Paint getPaint() { 192 return this.paint; 193 } 194 195 200 public void setPaint(Paint paint) { 201 this.paint = paint; 202 } 203 204 209 public TextAnchor getTextAnchor() { 210 return this.textAnchor; 211 } 212 213 219 public void setTextAnchor(TextAnchor anchor) { 220 this.textAnchor = anchor; 221 } 222 223 228 public TextAnchor getRotationAnchor() { 229 return this.rotationAnchor; 230 } 231 232 237 public void setRotationAnchor(TextAnchor anchor) { 238 this.rotationAnchor = anchor; 239 } 240 241 246 public double getRotationAngle() { 247 return this.rotationAngle; 248 } 249 250 257 public void setRotationAngle(double angle) { 258 this.rotationAngle = angle; 259 } 260 261 267 public double getX() { 268 return this.x; 269 } 270 271 277 public void setX(double x) { 278 this.x = x; 279 } 280 281 287 public double getY() { 288 return this.y; 289 } 290 291 297 public void setY(double y) { 298 this.y = y; 299 } 300 301 313 public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, 314 ValueAxis domainAxis, ValueAxis rangeAxis, 315 int rendererIndex, 316 PlotRenderingInfo info) { 317 318 PlotOrientation orientation = plot.getOrientation(); 319 RectangleEdge domainEdge = Plot.resolveDomainAxisLocation( 320 plot.getDomainAxisLocation(), orientation 321 ); 322 RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation( 323 plot.getRangeAxisLocation(), orientation 324 ); 325 326 float anchorX = (float) domainAxis.valueToJava2D( 327 this.x, dataArea, domainEdge 328 ); 329 float anchorY = (float) rangeAxis.valueToJava2D( 330 this.y, dataArea, rangeEdge 331 ); 332 333 if (orientation == PlotOrientation.HORIZONTAL) { 334 float tempAnchor = anchorX; 335 anchorX = anchorY; 336 anchorY = tempAnchor; 337 } 338 339 g2.setFont(getFont()); 340 g2.setPaint(getPaint()); 341 TextUtilities.drawRotatedString( 342 getText(), 343 g2, 344 anchorX, 345 anchorY, 346 getTextAnchor(), 347 getRotationAngle(), 348 getRotationAnchor() 349 ); 350 Shape hotspot = TextUtilities.calculateRotatedStringBounds( 351 getText(), 352 g2, 353 anchorX, 354 anchorY, 355 getTextAnchor(), 356 getRotationAngle(), 357 getRotationAnchor() 358 ); 359 360 String toolTip = getToolTipText(); 361 String url = getURL(); 362 if (toolTip != null || url != null) { 363 addEntity(info, hotspot, rendererIndex, toolTip, url); 364 } 365 366 } 367 368 375 public boolean equals(Object obj) { 376 if (obj == this) { 377 return true; 378 } 379 if (!(obj instanceof XYTextAnnotation)) { 380 return false; 381 } 382 if (!super.equals(obj)) { 383 return false; 384 } 385 XYTextAnnotation that = (XYTextAnnotation) obj; 386 if (!this.text.equals(that.text)) { 387 return false; 388 } 389 if (!this.font.equals(that.font)) { 390 return false; 391 } 392 if (!PaintUtilities.equal(this.paint, that.paint)) { 393 return false; 394 } 395 if (!this.rotationAnchor.equals(that.rotationAnchor)) { 396 return false; 397 } 398 if (this.rotationAngle != that.rotationAngle) { 399 return false; 400 } 401 if (!this.textAnchor.equals(that.textAnchor)) { 402 return false; 403 } 404 return true; 405 } 406 407 412 public int hashCode() { 413 return this.text.hashCode(); 415 } 416 417 424 public Object clone() throws CloneNotSupportedException { 425 return super.clone(); 426 } 427 428 435 private void writeObject(ObjectOutputStream stream) throws IOException { 436 stream.defaultWriteObject(); 437 SerialUtilities.writePaint(this.paint, stream); 438 } 439 440 448 private void readObject(ObjectInputStream stream) 449 throws IOException , ClassNotFoundException { 450 stream.defaultReadObject(); 451 this.paint = SerialUtilities.readPaint(stream); 452 } 453 454 455 } 456 | Popular Tags |