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: * DrawStringPanel.java
029: * --------------------
030: * (C) Copyright 2003, 2004, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: DrawStringPanel.java,v 1.5 2005/10/18 13:15:41 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 10-Jun-2003 : Version 1;
040: * 30-Sep-2004 : Moved drawRotatedString() from RefineryUtilities --> TextUtilities (DG);
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.Line2D;
052: import java.awt.geom.Rectangle2D;
053:
054: import javax.swing.JPanel;
055:
056: import org.jfree.text.TextUtilities;
057: import org.jfree.ui.TextAnchor;
058:
059: /**
060: * A panel used by the {@link DrawStringDemo} class.
061: *
062: * @author David Gilbert
063: */
064: public class DrawStringPanel extends JPanel {
065:
066: /** The preferred size for the panel. */
067: private static final Dimension PREFERRED_SIZE = new Dimension(500,
068: 300);
069:
070: /** Is the text rotated. */
071: private boolean rotate;
072:
073: /** The text to display. */
074: private String text = "Hello World";
075:
076: /** The text anchor. */
077: private TextAnchor anchor = TextAnchor.TOP_LEFT;
078:
079: /** The rotation anchor. */
080: private TextAnchor rotationAnchor = TextAnchor.TOP_LEFT;
081:
082: /** The font. */
083: private Font font = new Font("Serif", Font.PLAIN, 12);
084:
085: /** The rotation angle. */
086: private double angle;
087:
088: /**
089: * Creates a new panel.
090: *
091: * @param text the text.
092: * @param rotate a flag that controls whether or not the text is rotated.
093: */
094: public DrawStringPanel(final String text, final boolean rotate) {
095: this .text = text;
096: this .rotate = rotate;
097: }
098:
099: /**
100: * Returns the preferred size for the panel.
101: *
102: * @return The preferred size.
103: */
104: public Dimension getPreferredSize() {
105: return PREFERRED_SIZE;
106: }
107:
108: /**
109: * Sets the text anchor.
110: *
111: * @param anchor the text anchor.
112: */
113: public void setAnchor(final TextAnchor anchor) {
114: this .anchor = anchor;
115: }
116:
117: /**
118: * Sets the rotation anchor.
119: *
120: * @param anchor the rotation anchor.
121: */
122: public void setRotationAnchor(final TextAnchor anchor) {
123: this .rotationAnchor = anchor;
124: }
125:
126: /**
127: * Sets the rotation angle.
128: *
129: * @param angle the rotation angle.
130: */
131: public void setAngle(final double angle) {
132: this .angle = angle;
133: }
134:
135: /**
136: * Returns the font.
137: *
138: * @return The font.
139: */
140: public Font getFont() {
141: return this .font;
142: }
143:
144: /**
145: * Sets the font.
146: *
147: * @param font the font.
148: */
149: public void setFont(final Font font) {
150: this .font = font;
151: }
152:
153: /**
154: * Paints the panel.
155: *
156: * @param g the graphics device.
157: */
158: public void paintComponent(final Graphics g) {
159:
160: super .paintComponent(g);
161: final Graphics2D g2 = (Graphics2D) g;
162:
163: final Dimension size = getSize();
164: final Insets insets = getInsets();
165: final Rectangle2D available = new Rectangle2D.Double(
166: insets.left, insets.top, size.getWidth() - insets.left
167: - insets.right, size.getHeight() - insets.top
168: - insets.bottom);
169:
170: final double x = available.getCenterX();
171: final double y = available.getCenterY();
172:
173: final Line2D line1 = new Line2D.Double(x - 2.0, y + 2.0,
174: x + 2.0, y - 2.0);
175: final Line2D line2 = new Line2D.Double(x - 2.0, y - 2.0,
176: x + 2.0, y + 2.0);
177: g2.setPaint(Color.red);
178: g2.draw(line1);
179: g2.draw(line2);
180:
181: g2.setFont(this .font);
182: g2.setPaint(Color.black);
183: if (this .rotate) {
184: TextUtilities.drawRotatedString(this .text, g2, (float) x,
185: (float) y, this .anchor, this .angle,
186: this .rotationAnchor);
187: } else {
188: TextUtilities.drawAlignedString(this .text, g2, (float) x,
189: (float) y, this.anchor);
190: }
191:
192: }
193:
194: }
|