~ubuntu-branches/ubuntu/lucid/libjcommon-java/lucid

« back to all changes in this revision

Viewing changes to source/org/jfree/demo/DrawStringPanel.java

  • Committer: Bazaar Package Importer
  • Author(s): Wolfgang Baer
  • Date: 2006-02-09 15:58:13 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060209155813-fzi9zwh2rzedbllq
Tags: 1.0.0-1
* New stable upstream release (closes: #328574)
* Move to main - build with kaffe
* Use cdbs build system - added cdbs build-dependency
* Move package to pkg-java-maintainers for comaintenance, 
  added Christian Bayle and myself as uploaders
* Removed unneeded README.Debian
* Added README.Debian-source how the upstream tarball was cleaned
* Move big documentation in an own -doc package
* Register javadoc api with doc-base
* Standards-Version 3.6.2 (no changes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 * DrawStringPanel.java
 
29
 * --------------------
 
30
 * (C) Copyright 2003, 2004, by Object Refinery Limited.
 
31
 *
 
32
 * Original Author:  David Gilbert (for Object Refinery Limited);
 
33
 * Contributor(s):   -;
 
34
 *
 
35
 * $Id: DrawStringPanel.java,v 1.5 2005/10/18 13:15:41 mungady Exp $
 
36
 *
 
37
 * Changes
 
38
 * -------
 
39
 * 10-Jun-2003 : Version 1;
 
40
 * 30-Sep-2004 : Moved drawRotatedString() from RefineryUtilities --> TextUtilities (DG);
 
41
 *
 
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.Line2D;
 
52
import java.awt.geom.Rectangle2D;
 
53
 
 
54
import javax.swing.JPanel;
 
55
 
 
56
import org.jfree.text.TextUtilities;
 
57
import org.jfree.ui.TextAnchor;
 
58
 
 
59
/**
 
60
 * A panel used by the {@link DrawStringDemo} class.
 
61
 * 
 
62
 * @author David Gilbert
 
63
 */
 
64
public class DrawStringPanel extends JPanel {
 
65
 
 
66
    /** The preferred size for the panel. */
 
67
    private static final Dimension PREFERRED_SIZE = new Dimension(500, 300);
 
68
 
 
69
    /** Is the text rotated. */
 
70
    private boolean rotate;
 
71
 
 
72
    /** The text to display. */
 
73
    private String text = "Hello World";
 
74
 
 
75
    /** The text anchor. */
 
76
    private TextAnchor anchor = TextAnchor.TOP_LEFT;
 
77
 
 
78
    /** The rotation anchor. */
 
79
    private TextAnchor rotationAnchor = TextAnchor.TOP_LEFT;
 
80
 
 
81
    /** The font. */
 
82
    private Font font = new Font("Serif", Font.PLAIN, 12);
 
83
 
 
84
    /** The rotation angle. */
 
85
    private double angle;
 
86
 
 
87
    /**
 
88
     * Creates a new panel.
 
89
     *
 
90
     * @param text  the text.
 
91
     * @param rotate  a flag that controls whether or not the text is rotated.
 
92
     */
 
93
    public DrawStringPanel(final String text, final boolean rotate) {
 
94
        this.text = text;
 
95
        this.rotate = rotate;
 
96
    }
 
97
 
 
98
    /**
 
99
     * Returns the preferred size for the panel.
 
100
     *
 
101
     * @return The preferred size.
 
102
     */
 
103
    public Dimension getPreferredSize() {
 
104
        return PREFERRED_SIZE;
 
105
    }
 
106
 
 
107
    /**
 
108
     * Sets the text anchor.
 
109
     *
 
110
     * @param anchor  the text anchor.
 
111
     */
 
112
    public void setAnchor(final TextAnchor anchor) {
 
113
        this.anchor = anchor;
 
114
    }
 
115
 
 
116
    /**
 
117
     * Sets the rotation anchor.
 
118
     *
 
119
     * @param anchor  the rotation anchor.
 
120
     */
 
121
    public void setRotationAnchor(final TextAnchor anchor) {
 
122
        this.rotationAnchor = anchor;
 
123
    }
 
124
 
 
125
    /**
 
126
     * Sets the rotation angle.
 
127
     *
 
128
     * @param angle  the rotation angle.
 
129
     */
 
130
    public void setAngle(final double angle) {
 
131
        this.angle = angle;
 
132
    }
 
133
 
 
134
    /**
 
135
     * Returns the font.
 
136
     *
 
137
     * @return The font.
 
138
     */
 
139
    public Font getFont() {
 
140
        return this.font;
 
141
    }
 
142
 
 
143
    /**
 
144
     * Sets the font.
 
145
     *
 
146
     * @param font  the font.
 
147
     */
 
148
    public void setFont(final Font font) {
 
149
        this.font = font;
 
150
    }
 
151
 
 
152
    /**
 
153
     * Paints the panel.
 
154
     *
 
155
     * @param g  the graphics device.
 
156
     */
 
157
    public void paintComponent(final Graphics g) {
 
158
 
 
159
        super.paintComponent(g);
 
160
        final Graphics2D g2 = (Graphics2D) g;
 
161
 
 
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
 
168
        );
 
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, 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);
 
176
        g2.draw(line1);
 
177
        g2.draw(line2);
 
178
 
 
179
        g2.setFont(this.font);
 
180
        g2.setPaint(Color.black);
 
181
        if (this.rotate) {
 
182
            TextUtilities.drawRotatedString(
 
183
                this.text, g2, (float) x, (float) y,
 
184
                this.anchor, this.angle, this.rotationAnchor
 
185
            );
 
186
        }
 
187
        else {
 
188
            TextUtilities.drawAlignedString(this.text, g2, (float) x, (float) y, this.anchor);
 
189
        }
 
190
 
 
191
    }
 
192
 
 
193
}