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.]
30
* (C) Copyright 2003-2005, by Object Refinery Limited.
32
* Original Author: David Gilbert (for Object Refinery Limited);
35
* $Id: DrawStringDemo.java,v 1.4 2005/10/18 13:15:41 mungady Exp $
39
* 10-Jun-2003 : Version 1;
43
package org.jfree.demo;
45
import java.awt.BorderLayout;
46
import java.awt.event.ActionEvent;
47
import java.awt.event.ActionListener;
49
import javax.swing.JButton;
50
import javax.swing.JComboBox;
51
import javax.swing.JLabel;
52
import javax.swing.JOptionPane;
53
import javax.swing.JPanel;
54
import javax.swing.JSlider;
55
import javax.swing.JTabbedPane;
56
import javax.swing.SwingConstants;
57
import javax.swing.event.ChangeEvent;
58
import javax.swing.event.ChangeListener;
60
import org.jfree.ui.ApplicationFrame;
61
import org.jfree.ui.FontChooserPanel;
62
import org.jfree.ui.RefineryUtilities;
63
import org.jfree.ui.TextAnchor;
66
* A demo of some of the string drawing methods in the JCommon class library.
68
public class DrawStringDemo extends ApplicationFrame
69
implements ActionListener, ChangeListener {
71
/** The alignment anchor for the first panel. */
72
private JComboBox combo1;
74
/** The alignment anchor for the second panel. */
75
private JComboBox combo2;
77
/** The rotation anchor for the second panel. */
78
private JComboBox combo3;
80
/** A slider for the second panel - controls the angle of rotation. */
81
private JSlider slider;
83
/** String panel 1. */
84
private DrawStringPanel drawStringPanel1;
86
/** String panel 2. */
87
private DrawStringPanel drawStringPanel2;
90
* Creates a new demo instance.
92
* @param title the frame title.
94
public DrawStringDemo(final String title) {
96
setContentPane(createContentPane());
100
* Receives action events.
102
* @param event the event.
104
public void actionPerformed(final ActionEvent event) {
105
if (event.getActionCommand().equals("fontButton.clicked")) {
108
if (event.getActionCommand().equals("combo1.changed")) {
109
handleCombo1Change();
111
if (event.getActionCommand().equals("combo2.changed")) {
112
handleCombo2Change();
114
if (event.getActionCommand().equals("combo3.changed")) {
115
handleCombo3Change();
119
public void stateChanged(ChangeEvent event) {
120
int r = this.slider.getValue();
121
double angle = Math.PI * 2.0 * (r / 360.0);
122
this.drawStringPanel2.setAngle(angle);
123
this.drawStringPanel2.invalidate();
124
this.drawStringPanel2.repaint();
128
* Updates the display when combo 1 is updated.
130
private void handleCombo1Change() {
131
final String text = this.combo1.getSelectedItem().toString();
132
this.drawStringPanel1.setAnchor(convertStringToAnchor(text));
133
this.drawStringPanel1.invalidate();
134
this.drawStringPanel1.repaint();
138
* Updates the display when combo 2 is updated.
140
private void handleCombo2Change() {
141
final String text = this.combo2.getSelectedItem().toString();
142
this.drawStringPanel2.setAnchor(convertStringToAnchor(text));
143
this.drawStringPanel2.invalidate();
144
this.drawStringPanel2.repaint();
148
* Updates the display when combo 3 is updated.
150
private void handleCombo3Change() {
151
final String text = this.combo3.getSelectedItem().toString();
152
this.drawStringPanel2.setRotationAnchor(convertStringToAnchor(text));
153
this.drawStringPanel2.invalidate();
154
this.drawStringPanel2.repaint();
158
* Creates the content pane for the demo frame.
160
* @return The content pane.
162
private JPanel createContentPane() {
163
final JPanel content = new JPanel(new BorderLayout());
164
final JTabbedPane tabs = new JTabbedPane();
165
tabs.add("Alignment", createTab1Content());
166
tabs.add("Rotation", createTab2Content());
172
* Creates the content for tab 1.
174
* @return The content panel.
176
private JPanel createTab1Content() {
177
final JPanel content = new JPanel(new BorderLayout());
178
this.combo1 = new JComboBox();
179
this.combo1.setActionCommand("combo1.changed");
180
populateTextAnchorCombo(this.combo1);
181
this.combo1.addActionListener(this);
183
final JPanel controls = new JPanel();
184
controls.add(this.combo1);
186
final JButton fontButton = new JButton("Select Font...");
187
fontButton.setActionCommand("fontButton.clicked");
188
fontButton.addActionListener(this);
189
controls.add(fontButton);
190
content.add(controls, BorderLayout.NORTH);
191
this.drawStringPanel1 = new DrawStringPanel("0123456789", false);
192
content.add(this.drawStringPanel1);
197
* Creates the content for tab 2.
199
* @return The content panel.
201
private JPanel createTab2Content() {
202
JPanel content = new JPanel(new BorderLayout());
203
JPanel controls = new JPanel();
204
controls.add(new JLabel("Text anchor: "));
205
this.combo2 = new JComboBox();
206
populateTextAnchorCombo(this.combo2);
207
this.combo2.setActionCommand("combo2.changed");
208
this.combo2.addActionListener(this);
209
controls.add(this.combo2);
210
controls.add(new JLabel("Rotation anchor: "));
211
this.combo3 = new JComboBox();
212
populateTextAnchorCombo(this.combo3);
213
this.combo3.setActionCommand("combo3.changed");
214
this.combo3.addActionListener(this);
215
controls.add(this.combo3);
216
this.slider = new JSlider(SwingConstants.VERTICAL, 0, 360, 0);
217
this.slider.setMajorTickSpacing(45);
218
this.slider.setMinorTickSpacing(5);
219
this.slider.setPaintLabels(true);
220
this.slider.setPaintTicks(true);
221
this.slider.setPaintTrack(true);
222
this.slider.addChangeListener(this);
223
content.add(controls, BorderLayout.NORTH);
224
content.add(this.slider, BorderLayout.WEST);
225
this.drawStringPanel2 = new DrawStringPanel("Rotated Text", true);
226
content.add(this.drawStringPanel2);
231
* Displays a primitive font chooser dialog to allow the user to change the font.
233
private void displayFontDialog() {
235
final FontChooserPanel panel = new FontChooserPanel(this.drawStringPanel1.getFont());
236
final int result = JOptionPane.showConfirmDialog(this, panel, "Font Selection",
237
JOptionPane.OK_CANCEL_OPTION,
238
JOptionPane.PLAIN_MESSAGE);
240
if (result == JOptionPane.OK_OPTION) {
241
this.drawStringPanel1.setFont(panel.getSelectedFont());
242
this.drawStringPanel2.setFont(panel.getSelectedFont());
248
* Populates a combo box with the available {@link TextAnchor} options.
250
* @param combo the combo box.
252
private void populateTextAnchorCombo(final JComboBox combo) {
253
combo.addItem("TextAnchor.TOP_LEFT");
254
combo.addItem("TextAnchor.TOP_CENTER");
255
combo.addItem("TextAnchor.TOP_RIGHT");
256
combo.addItem("TextAnchor.HALF_ASCENT_LEFT");
257
combo.addItem("TextAnchor.HALF_ASCENT_CENTER");
258
combo.addItem("TextAnchor.HALF_ASCENT_RIGHT");
259
combo.addItem("TextAnchor.CENTER_LEFT");
260
combo.addItem("TextAnchor.CENTER");
261
combo.addItem("TextAnchor.CENTER_RIGHT");
262
combo.addItem("TextAnchor.BASELINE_LEFT");
263
combo.addItem("TextAnchor.BASELINE_CENTER");
264
combo.addItem("TextAnchor.BASELINE_RIGHT");
265
combo.addItem("TextAnchor.BOTTOM_LEFT");
266
combo.addItem("TextAnchor.BOTTOM_CENTER");
267
combo.addItem("TextAnchor.BOTTOM_RIGHT");
271
* Converts a string to a corresponding {@link TextAnchor} instance.
273
* @param text the text.
275
* @return The anchor.
277
private TextAnchor convertStringToAnchor(final String text) {
279
if (text.equals("TextAnchor.TOP_LEFT")) {
280
return TextAnchor.TOP_LEFT;
282
else if (text.equals("TextAnchor.TOP_CENTER")) {
283
return TextAnchor.TOP_CENTER;
285
else if (text.equals("TextAnchor.TOP_RIGHT")) {
286
return TextAnchor.TOP_RIGHT;
288
else if (text.equals("TextAnchor.CENTER_LEFT")) {
289
return TextAnchor.CENTER_LEFT;
291
else if (text.equals("TextAnchor.CENTER")) {
292
return TextAnchor.CENTER;
294
else if (text.equals("TextAnchor.CENTER_RIGHT")) {
295
return TextAnchor.CENTER_RIGHT;
297
else if (text.equals("TextAnchor.HALF_ASCENT_LEFT")) {
298
return TextAnchor.HALF_ASCENT_LEFT;
300
else if (text.equals("TextAnchor.HALF_ASCENT_CENTER")) {
301
return TextAnchor.HALF_ASCENT_CENTER;
303
else if (text.equals("TextAnchor.HALF_ASCENT_RIGHT")) {
304
return TextAnchor.HALF_ASCENT_RIGHT;
306
else if (text.equals("TextAnchor.BASELINE_LEFT")) {
307
return TextAnchor.BASELINE_LEFT;
309
else if (text.equals("TextAnchor.BASELINE_CENTER")) {
310
return TextAnchor.BASELINE_CENTER;
312
else if (text.equals("TextAnchor.BASELINE_RIGHT")) {
313
return TextAnchor.BASELINE_RIGHT;
315
else if (text.equals("TextAnchor.BOTTOM_LEFT")) {
316
return TextAnchor.BOTTOM_LEFT;
318
else if (text.equals("TextAnchor.BOTTOM_CENTER")) {
319
return TextAnchor.BOTTOM_CENTER;
321
else if (text.equals("TextAnchor.BOTTOM_RIGHT")) {
322
return TextAnchor.BOTTOM_RIGHT;
331
* The starting point for the demo.
333
* @param args ignored.
335
public static void main(final String[] args) {
337
final DrawStringDemo demo = new DrawStringDemo("DrawString Demo");
339
RefineryUtilities.centerFrameOnScreen(demo);
340
demo.setVisible(true);