TextBlockPanel.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* ========================================================================
  2. * JCommon : a free general purpose class library for the Java(tm) platform
  3. * ========================================================================
  4. *
  5. * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jcommon/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  25. * in the United States and other countries.]
  26. *
  27. * -------------------
  28. * TextBlockPanel.java
  29. * -------------------
  30. * (C) Copyright 2004, by Object Refinery Limited.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * $Id: TextBlockPanel.java,v 1.4 2007/11/02 17:50:35 taqua Exp $
  36. *
  37. * Changes
  38. * -------
  39. * 07-Jan-2004 : Version 1;
  40. *
  41. */
  42. package org.jfree.demo;
  43. import java.awt.Color;
  44. import java.awt.Dimension;
  45. import java.awt.Font;
  46. import java.awt.Graphics;
  47. import java.awt.Graphics2D;
  48. import java.awt.Insets;
  49. import java.awt.geom.Rectangle2D;
  50. import javax.swing.JPanel;
  51. import org.jfree.text.G2TextMeasurer;
  52. import org.jfree.text.TextBlock;
  53. import org.jfree.text.TextBlockAnchor;
  54. import org.jfree.text.TextUtilities;
  55. /**
  56. * A panel used by the {@link DrawStringDemo} class.
  57. *
  58. * @author David Gilbert
  59. */
  60. public class TextBlockPanel extends JPanel {
  61. /** The preferred size for the panel. */
  62. private static final Dimension PREFERRED_SIZE = new Dimension(500, 300);
  63. /** The text to display. */
  64. private String text;
  65. /** The font to use. */
  66. private Font font;
  67. /**
  68. * Creates a new panel.
  69. *
  70. * @param text the text.
  71. * @param font the font.
  72. */
  73. public TextBlockPanel(final String text, final Font font) {
  74. this.text = text;
  75. this.font = font;
  76. }
  77. /**
  78. * Returns the preferred size for the panel.
  79. *
  80. * @return The preferred size.
  81. */
  82. public Dimension getPreferredSize() {
  83. return PREFERRED_SIZE;
  84. }
  85. /**
  86. * Returns the font.
  87. *
  88. * @return The font.
  89. */
  90. public Font getFont() {
  91. return this.font;
  92. }
  93. /**
  94. * Sets the font.
  95. *
  96. * @param font the font.
  97. */
  98. public void setFont(final Font font) {
  99. this.font = font;
  100. }
  101. /**
  102. * Paints the panel.
  103. *
  104. * @param g the graphics device.
  105. */
  106. public void paintComponent(final Graphics g) {
  107. super.paintComponent(g);
  108. final Graphics2D g2 = (Graphics2D) g;
  109. final Dimension size = getSize();
  110. final Insets insets = getInsets();
  111. final Rectangle2D available = new Rectangle2D.Double(insets.left, insets.top,
  112. size.getWidth() - insets.left - insets.right,
  113. size.getHeight() - insets.top - insets.bottom);
  114. final double x = available.getX();
  115. final double y = available.getY();
  116. final float width = (float) available.getWidth();
  117. final TextBlock block = TextUtilities.createTextBlock(
  118. this.text, this.font, Color.black, width, new G2TextMeasurer(g2)
  119. );
  120. g2.setPaint(Color.black);
  121. block.draw(g2, (float) x, (float) y, TextBlockAnchor.TOP_LEFT, 0.0f, 0.0f, 0.0);
  122. }
  123. }