001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jcommon/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: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * -------------------
028: * TextBlockPanel.java
029: * -------------------
030: * (C) Copyright 2004, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: TextBlockPanel.java,v 1.3 2005/10/18 13:15:41 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 07-Jan-2004 : Version 1;
040: *
041: */
042:
043: package org.jfree.demo;
044:
045: import java.awt.Color;
046: import java.awt.Dimension;
047: import java.awt.Font;
048: import java.awt.Graphics;
049: import java.awt.Graphics2D;
050: import java.awt.Insets;
051: import java.awt.geom.Rectangle2D;
052:
053: import javax.swing.JPanel;
054:
055: import org.jfree.text.G2TextMeasurer;
056: import org.jfree.text.TextBlock;
057: import org.jfree.text.TextBlockAnchor;
058: import org.jfree.text.TextUtilities;
059:
060: /**
061: * A panel used by the {@link DrawStringDemo} class.
062: *
063: * @author David Gilbert
064: */
065: public class TextBlockPanel extends JPanel {
066:
067: /** The preferred size for the panel. */
068: private static final Dimension PREFERRED_SIZE = new Dimension(500,
069: 300);
070:
071: /** The text to display. */
072: private String text;
073:
074: /** The font to use. */
075: private Font font;
076:
077: /**
078: * Creates a new panel.
079: *
080: * @param text the text.
081: * @param font the font.
082: */
083: public TextBlockPanel(final String text, final Font font) {
084: this .text = text;
085: this .font = font;
086: }
087:
088: /**
089: * Returns the preferred size for the panel.
090: *
091: * @return The preferred size.
092: */
093: public Dimension getPreferredSize() {
094: return PREFERRED_SIZE;
095: }
096:
097: /**
098: * Returns the font.
099: *
100: * @return The font.
101: */
102: public Font getFont() {
103: return this .font;
104: }
105:
106: /**
107: * Sets the font.
108: *
109: * @param font the font.
110: */
111: public void setFont(final Font font) {
112: this .font = font;
113: }
114:
115: /**
116: * Paints the panel.
117: *
118: * @param g the graphics device.
119: */
120: public void paintComponent(final Graphics g) {
121:
122: super .paintComponent(g);
123: final Graphics2D g2 = (Graphics2D) g;
124:
125: final Dimension size = getSize();
126: final Insets insets = getInsets();
127: final Rectangle2D available = new Rectangle2D.Double(
128: insets.left, insets.top, size.getWidth() - insets.left
129: - insets.right, size.getHeight() - insets.top
130: - insets.bottom);
131:
132: final double x = available.getX();
133: final double y = available.getY();
134: final float width = (float) available.getWidth();
135: final TextBlock block = TextUtilities.createTextBlock(
136: this .text, this .font, Color.black, width,
137: new G2TextMeasurer(g2));
138: g2.setPaint(Color.black);
139: block.draw(g2, (float) x, (float) y, TextBlockAnchor.TOP_LEFT,
140: 0.0f, 0.0f, 0.0);
141:
142: }
143:
144: }
|