1 42 43 package org.jfree.demo; 44 45 import java.awt.Color ; 46 import java.awt.Dimension ; 47 import java.awt.Font ; 48 import java.awt.Graphics ; 49 import java.awt.Graphics2D ; 50 import java.awt.Insets ; 51 import java.awt.geom.Rectangle2D ; 52 53 import javax.swing.JPanel ; 54 55 import org.jfree.text.G2TextMeasurer; 56 import org.jfree.text.TextBlock; 57 import org.jfree.text.TextBlockAnchor; 58 import org.jfree.text.TextUtilities; 59 60 65 public class TextBlockPanel extends JPanel { 66 67 68 private static final Dimension PREFERRED_SIZE = new Dimension (500, 300); 69 70 71 private String text; 72 73 74 private Font font; 75 76 82 public TextBlockPanel(final String text, final Font font) { 83 this.text = text; 84 this.font = font; 85 } 86 87 92 public Dimension getPreferredSize() { 93 return PREFERRED_SIZE; 94 } 95 96 101 public Font getFont() { 102 return this.font; 103 } 104 105 110 public void setFont(final Font font) { 111 this.font = font; 112 } 113 114 119 public void paintComponent(final Graphics g) { 120 121 super.paintComponent(g); 122 final Graphics2D g2 = (Graphics2D ) g; 123 124 final Dimension size = getSize(); 125 final Insets insets = getInsets(); 126 final Rectangle2D available = new Rectangle2D.Double (insets.left, insets.top, 127 size.getWidth() - insets.left - insets.right, 128 size.getHeight() - insets.top - insets.bottom); 129 130 final double x = available.getX(); 131 final double y = available.getY(); 132 final float width = (float) available.getWidth(); 133 final TextBlock block = TextUtilities.createTextBlock( 134 this.text, this.font, Color.black, width, new G2TextMeasurer(g2) 135 ); 136 g2.setPaint(Color.black); 137 block.draw(g2, (float) x, (float) y, TextBlockAnchor.TOP_LEFT, 0.0f, 0.0f, 0.0); 138 139 } 140 141 } 142 | Popular Tags |