KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > demo > TextBlockPanel


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.3 2005/10/18 13:15:41 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 07-Jan-2004 : Version 1;
40  *
41  */

42
43 package org.jfree.demo;
44
45 import java.awt.Color JavaDoc;
46 import java.awt.Dimension JavaDoc;
47 import java.awt.Font JavaDoc;
48 import java.awt.Graphics JavaDoc;
49 import java.awt.Graphics2D JavaDoc;
50 import java.awt.Insets JavaDoc;
51 import java.awt.geom.Rectangle2D JavaDoc;
52
53 import javax.swing.JPanel JavaDoc;
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 /**
61  * A panel used by the {@link DrawStringDemo} class.
62  *
63  * @author David Gilbert
64  */

65 public class TextBlockPanel extends JPanel JavaDoc {
66
67     /** The preferred size for the panel. */
68     private static final Dimension JavaDoc PREFERRED_SIZE = new Dimension JavaDoc(500, 300);
69
70     /** The text to display. */
71     private String JavaDoc text;
72     
73     /** The font to use. */
74     private Font JavaDoc font;
75
76     /**
77      * Creates a new panel.
78      *
79      * @param text the text.
80      * @param font the font.
81      */

82     public TextBlockPanel(final String JavaDoc text, final Font JavaDoc font) {
83         this.text = text;
84         this.font = font;
85     }
86
87     /**
88      * Returns the preferred size for the panel.
89      *
90      * @return The preferred size.
91      */

92     public Dimension JavaDoc getPreferredSize() {
93         return PREFERRED_SIZE;
94     }
95
96     /**
97      * Returns the font.
98      *
99      * @return The font.
100      */

101     public Font JavaDoc getFont() {
102         return this.font;
103     }
104
105     /**
106      * Sets the font.
107      *
108      * @param font the font.
109      */

110     public void setFont(final Font JavaDoc font) {
111         this.font = font;
112     }
113
114     /**
115      * Paints the panel.
116      *
117      * @param g the graphics device.
118      */

119     public void paintComponent(final Graphics JavaDoc g) {
120
121         super.paintComponent(g);
122         final Graphics2D JavaDoc g2 = (Graphics2D JavaDoc) g;
123
124         final Dimension JavaDoc size = getSize();
125         final Insets JavaDoc insets = getInsets();
126         final Rectangle2D JavaDoc available = new Rectangle2D.Double JavaDoc(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