1 54 55 package org.jfree.chart; 56 57 import java.awt.Color ; 58 import java.awt.Font ; 59 import java.awt.FontMetrics ; 60 import java.awt.Graphics2D ; 61 import java.awt.Paint ; 62 import java.awt.font.FontRenderContext ; 63 import java.awt.font.LineMetrics ; 64 import java.awt.geom.Rectangle2D ; 65 import java.io.IOException ; 66 import java.io.ObjectInputStream ; 67 import java.io.ObjectOutputStream ; 68 import java.io.Serializable ; 69 70 import org.jfree.chart.event.TitleChangeEvent; 71 import org.jfree.io.SerialUtilities; 72 import org.jfree.util.ObjectUtils; 73 74 79 public class TextTitle extends AbstractTitle implements Serializable , Cloneable { 80 81 82 public static final Font DEFAULT_FONT = new Font ("SansSerif", Font.BOLD, 12); 83 84 85 public static final Paint DEFAULT_TEXT_PAINT = Color.black; 86 87 88 private String text; 89 90 91 private Font font; 92 93 94 private transient Paint paint; 95 96 97 private transient Paint backgroundPaint; 98 99 104 public TextTitle(String text) { 105 106 this(text, 107 TextTitle.DEFAULT_FONT, 108 TextTitle.DEFAULT_TEXT_PAINT, 109 AbstractTitle.DEFAULT_POSITION, 110 AbstractTitle.DEFAULT_HORIZONTAL_ALIGNMENT, 111 AbstractTitle.DEFAULT_VERTICAL_ALIGNMENT, 112 AbstractTitle.DEFAULT_SPACER); 113 114 } 115 116 122 public TextTitle(String text, Font font) { 123 124 this(text, font, 125 TextTitle.DEFAULT_TEXT_PAINT, 126 AbstractTitle.DEFAULT_POSITION, 127 AbstractTitle.DEFAULT_HORIZONTAL_ALIGNMENT, 128 AbstractTitle.DEFAULT_VERTICAL_ALIGNMENT, 129 AbstractTitle.DEFAULT_SPACER); 130 131 } 132 133 140 public TextTitle(String text, Font font, Paint paint) { 141 142 this(text, font, paint, 143 AbstractTitle.DEFAULT_POSITION, 144 AbstractTitle.DEFAULT_HORIZONTAL_ALIGNMENT, 145 AbstractTitle.DEFAULT_VERTICAL_ALIGNMENT, 146 AbstractTitle.DEFAULT_SPACER); 147 148 } 149 159 public TextTitle(String text, Font font, int horizontalAlignment) { 160 161 this(text, font, 162 TextTitle.DEFAULT_TEXT_PAINT, 163 AbstractTitle.DEFAULT_POSITION, 164 horizontalAlignment, 165 AbstractTitle.DEFAULT_VERTICAL_ALIGNMENT, 166 AbstractTitle.DEFAULT_SPACER); 167 168 } 169 170 184 public TextTitle(String text, Font font, Paint paint, int position, 185 int horizontalAlignment, int verticalAlignment, 186 Spacer spacer) { 187 188 super(position, horizontalAlignment, verticalAlignment, spacer); 189 if (text == null) 190 { 191 throw new NullPointerException ("TextTitle(..): Text is null"); 192 } 193 if (font == null) 194 { 195 throw new NullPointerException ("TextTitle(..): Font is null"); 196 } 197 if (paint == null) 198 { 199 throw new NullPointerException ("TextTitle(..): Paint is null"); 200 } 201 this.text = text; 202 this.font = font; 203 this.paint = paint; 204 this.backgroundPaint = null; 205 206 } 207 208 213 public String getText() { 214 return text; 215 } 216 217 223 public void setText(String text) { 224 225 if (text == null) 226 { 227 throw new NullPointerException ("TextTitle.setText(..): Text is null"); 228 } 229 if (!this.text.equals(text)) { 230 this.text = text; 231 notifyListeners(new TitleChangeEvent(this)); 232 } 233 234 } 235 236 241 public Font getFont() { 242 return this.font; 243 } 244 245 251 public void setFont(Font font) { 252 253 if (font == null) { 255 throw new IllegalArgumentException ("TextTitle.setFont(...): null font not permitted."); 256 } 257 258 if (!this.font.equals(font)) { 260 this.font = font; 261 notifyListeners(new TitleChangeEvent(this)); 262 } 263 264 } 265 266 271 public Paint getPaint() { 272 return this.paint; 273 } 274 275 281 public void setPaint(Paint paint) { 282 283 if (paint == null) { 285 throw new IllegalArgumentException ("TextTitle.setPaint(...): " 286 + "null paint not permitted."); 287 } 288 289 if (!this.paint.equals(paint)) { 291 this.paint = paint; 292 notifyListeners(new TitleChangeEvent(this)); 293 } 294 295 } 296 297 302 public Paint getBackgroundPaint() { 303 return this.backgroundPaint; 304 } 305 306 312 public void setBackgroundPaint(Paint paint) { 313 314 this.backgroundPaint = paint; 316 notifyListeners(new TitleChangeEvent(this)); 317 318 } 319 320 321 329 public boolean isValidPosition(int position) { 330 331 return ((position == AbstractTitle.TOP) || (position == AbstractTitle.BOTTOM)); 332 333 } 334 335 342 public double getPreferredWidth(Graphics2D g2) { 343 344 g2.setFont(font); 346 FontMetrics fm = g2.getFontMetrics(font); 347 Rectangle2D titleBounds = fm.getStringBounds(text, g2); 348 double result = titleBounds.getWidth(); 349 350 Spacer spacer = getSpacer(); 352 if (spacer != null) { 353 result = spacer.getAdjustedWidth(result); 354 } 355 356 return result; 357 358 } 359 360 367 public double getPreferredHeight(Graphics2D g2) { 368 369 double result = 0.0; 370 371 g2.setFont(font); 375 FontRenderContext frc = g2.getFontRenderContext(); 376 LineMetrics lineMetrics = font.getLineMetrics(text, frc); 377 result = lineMetrics.getHeight(); 378 379 Spacer spacer = getSpacer(); 381 if (spacer != null) { 382 result = spacer.getAdjustedHeight(result); 383 } 384 return result; 386 387 } 388 389 395 public void draw(Graphics2D g2, Rectangle2D area) { 396 397 drawHorizontal(g2, area); 400 } 401 402 403 409 protected void drawHorizontal(Graphics2D g2, Rectangle2D area) { 410 411 if (this.text.equals("")) { 412 return; 413 } 414 FontRenderContext frc = g2.getFontRenderContext(); 415 FontMetrics fm = g2.getFontMetrics(this.font); 416 Rectangle2D titleBounds = fm.getStringBounds(this.text, g2); 417 LineMetrics lineMetrics = font.getLineMetrics(this.text, frc); 418 419 double titleWidth = titleBounds.getWidth(); 420 double leftSpace = 0.0; 421 double rightSpace = 0.0; 422 double titleHeight = lineMetrics.getHeight(); 423 double topSpace = 0.0; 424 double bottomSpace = 0.0; 425 426 Spacer spacer = getSpacer(); 428 leftSpace = spacer.getLeftSpace(area.getWidth()); 430 rightSpace = spacer.getRightSpace(area.getWidth()); 431 topSpace = spacer.getTopSpace(titleHeight); 432 bottomSpace = spacer.getBottomSpace(titleHeight); 433 435 double titleY = area.getY() + topSpace; 436 437 int verticalAlignment = getVerticalAlignment(); 439 if (verticalAlignment == TOP) { 440 titleY = titleY + titleHeight - lineMetrics.getLeading() - lineMetrics.getDescent(); 441 } 442 else { 443 if (verticalAlignment == MIDDLE) { 444 double space = (area.getHeight() - topSpace - bottomSpace - titleHeight); 445 titleY = titleY + (space / 2) + titleHeight 446 - lineMetrics.getLeading() - lineMetrics.getDescent(); 447 } 448 else { 449 if (verticalAlignment == BOTTOM) { 450 titleY = area.getMaxY() - bottomSpace 451 - lineMetrics.getLeading() 452 - lineMetrics.getDescent(); 453 } 454 } 455 } 456 457 int horizontalAlignment = getHorizontalAlignment(); 459 double titleX = area.getX() + leftSpace; 460 if (horizontalAlignment == CENTER) { 461 titleX = titleX + ((area.getWidth() - leftSpace - rightSpace) / 2) - (titleWidth / 2); 462 } 463 else if (horizontalAlignment == LEFT) { 464 titleX = area.getX() + leftSpace; 465 } 466 else if (horizontalAlignment == RIGHT) { 467 titleX = area.getMaxX() - rightSpace - titleWidth; 468 } 469 470 if (this.backgroundPaint != null) { 471 g2.setPaint(this.backgroundPaint); 472 g2.fill(area); 473 } 474 g2.setFont(this.font); 475 g2.setPaint(this.paint); 476 g2.drawString(text, (float) (titleX), (float) (titleY)); 477 478 } 479 480 487 public boolean equals(Object obj) { 488 489 if (obj == null) { 490 return false; 491 } 492 493 if (obj == this) { 494 return true; 495 } 496 497 if (obj instanceof TextTitle) { 498 499 TextTitle t = (TextTitle) obj; 500 if (super.equals(obj)) { 501 if (ObjectUtils.equal(this.text, t.text) == false) 502 { 503 return false; 504 } 505 if (ObjectUtils.equal(this.font, t.font) == false) 506 { 507 return false; 508 } 509 if (ObjectUtils.equal(this.paint, t.paint) == false) 510 { 511 return false; 512 } 513 return true; 514 } 515 } 516 517 return false; 518 519 } 520 521 528 private void writeObject(ObjectOutputStream stream) throws IOException { 529 stream.defaultWriteObject(); 530 SerialUtilities.writePaint(this.paint, stream); 531 SerialUtilities.writePaint(this.backgroundPaint, stream); 532 } 533 534 542 private void readObject(ObjectInputStream stream) throws IOException , ClassNotFoundException { 543 stream.defaultReadObject(); 544 this.paint = SerialUtilities.readPaint(stream); 545 this.backgroundPaint = SerialUtilities.readPaint(stream); 546 } 547 548 549 551 } 552 553 | Popular Tags |