1
/* ========================================================================
2
* JCommon : a free general purpose class library for the Java(tm) platform
3
* ========================================================================
5
* (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
7
* Project Info: http://www.jfree.org/jcommon/index.html
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.
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.
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,
24
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25
* in the United States and other countries.]
27
* --------------------
28
* DrawStringPanel.java
29
* --------------------
30
* (C) Copyright 2003, 2004, by Object Refinery Limited.
32
* Original Author: David Gilbert (for Object Refinery Limited);
35
* $Id: DrawStringPanel.java,v 1.5 2005/10/18 13:15:41 mungady Exp $
39
* 10-Jun-2003 : Version 1;
40
* 30-Sep-2004 : Moved drawRotatedString() from RefineryUtilities --> TextUtilities (DG);
43
package org.jfree.demo;
45
import java.awt.Color;
46
import java.awt.Dimension;
48
import java.awt.Graphics;
49
import java.awt.Graphics2D;
50
import java.awt.Insets;
51
import java.awt.geom.Line2D;
52
import java.awt.geom.Rectangle2D;
54
import javax.swing.JPanel;
56
import org.jfree.text.TextUtilities;
57
import org.jfree.ui.TextAnchor;
60
* A panel used by the {@link DrawStringDemo} class.
62
* @author David Gilbert
64
public class DrawStringPanel extends JPanel {
66
/** The preferred size for the panel. */
67
private static final Dimension PREFERRED_SIZE = new Dimension(500, 300);
69
/** Is the text rotated. */
70
private boolean rotate;
72
/** The text to display. */
73
private String text = "Hello World";
75
/** The text anchor. */
76
private TextAnchor anchor = TextAnchor.TOP_LEFT;
78
/** The rotation anchor. */
79
private TextAnchor rotationAnchor = TextAnchor.TOP_LEFT;
82
private Font font = new Font("Serif", Font.PLAIN, 12);
84
/** The rotation angle. */
88
* Creates a new panel.
90
* @param text the text.
91
* @param rotate a flag that controls whether or not the text is rotated.
93
public DrawStringPanel(final String text, final boolean rotate) {
99
* Returns the preferred size for the panel.
101
* @return The preferred size.
103
public Dimension getPreferredSize() {
104
return PREFERRED_SIZE;
108
* Sets the text anchor.
110
* @param anchor the text anchor.
112
public void setAnchor(final TextAnchor anchor) {
113
this.anchor = anchor;
117
* Sets the rotation anchor.
119
* @param anchor the rotation anchor.
121
public void setRotationAnchor(final TextAnchor anchor) {
122
this.rotationAnchor = anchor;
126
* Sets the rotation angle.
128
* @param angle the rotation angle.
130
public void setAngle(final double angle) {
139
public Font getFont() {
146
* @param font the font.
148
public void setFont(final Font font) {
155
* @param g the graphics device.
157
public void paintComponent(final Graphics g) {
159
super.paintComponent(g);
160
final Graphics2D g2 = (Graphics2D) g;
162
final Dimension size = getSize();
163
final Insets insets = getInsets();
164
final Rectangle2D available = new Rectangle2D.Double(
165
insets.left, insets.top,
166
size.getWidth() - insets.left - insets.right,
167
size.getHeight() - insets.top - insets.bottom
170
final double x = available.getCenterX();
171
final double y = available.getCenterY();
173
final Line2D line1 = new Line2D.Double(x - 2.0, y + 2.0, x + 2.0, y - 2.0);
174
final Line2D line2 = new Line2D.Double(x - 2.0, y - 2.0, x + 2.0, y + 2.0);
175
g2.setPaint(Color.red);
179
g2.setFont(this.font);
180
g2.setPaint(Color.black);
182
TextUtilities.drawRotatedString(
183
this.text, g2, (float) x, (float) y,
184
this.anchor, this.angle, this.rotationAnchor
188
TextUtilities.drawAlignedString(this.text, g2, (float) x, (float) y, this.anchor);