KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DrawStringDemo.java
29  * -------------------
30  * (C) Copyright 2003-2005, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: DrawStringDemo.java,v 1.4 2005/10/18 13:15:41 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 10-Jun-2003 : Version 1;
40  *
41  */

42
43 package org.jfree.demo;
44
45 import java.awt.BorderLayout JavaDoc;
46 import java.awt.event.ActionEvent JavaDoc;
47 import java.awt.event.ActionListener JavaDoc;
48
49 import javax.swing.JButton JavaDoc;
50 import javax.swing.JComboBox JavaDoc;
51 import javax.swing.JLabel JavaDoc;
52 import javax.swing.JOptionPane JavaDoc;
53 import javax.swing.JPanel JavaDoc;
54 import javax.swing.JSlider JavaDoc;
55 import javax.swing.JTabbedPane JavaDoc;
56 import javax.swing.SwingConstants JavaDoc;
57 import javax.swing.event.ChangeEvent JavaDoc;
58 import javax.swing.event.ChangeListener JavaDoc;
59
60 import org.jfree.ui.ApplicationFrame;
61 import org.jfree.ui.FontChooserPanel;
62 import org.jfree.ui.RefineryUtilities;
63 import org.jfree.ui.TextAnchor;
64
65 /**
66  * A demo of some of the string drawing methods in the JCommon class library.
67  */

68 public class DrawStringDemo extends ApplicationFrame
69                             implements ActionListener JavaDoc, ChangeListener JavaDoc {
70
71     /** The alignment anchor for the first panel. */
72     private JComboBox JavaDoc combo1;
73
74     /** The alignment anchor for the second panel. */
75     private JComboBox JavaDoc combo2;
76
77     /** The rotation anchor for the second panel. */
78     private JComboBox JavaDoc combo3;
79
80     /** A slider for the second panel - controls the angle of rotation. */
81     private JSlider JavaDoc slider;
82     
83     /** String panel 1. */
84     private DrawStringPanel drawStringPanel1;
85
86     /** String panel 2. */
87     private DrawStringPanel drawStringPanel2;
88
89     /**
90      * Creates a new demo instance.
91      *
92      * @param title the frame title.
93      */

94     public DrawStringDemo(final String JavaDoc title) {
95         super(title);
96         setContentPane(createContentPane());
97     }
98
99     /**
100      * Receives action events.
101      *
102      * @param event the event.
103      */

104     public void actionPerformed(final ActionEvent JavaDoc event) {
105         if (event.getActionCommand().equals("fontButton.clicked")) {
106             displayFontDialog();
107         }
108         if (event.getActionCommand().equals("combo1.changed")) {
109             handleCombo1Change();
110         }
111         if (event.getActionCommand().equals("combo2.changed")) {
112             handleCombo2Change();
113         }
114         if (event.getActionCommand().equals("combo3.changed")) {
115             handleCombo3Change();
116         }
117     }
118
119     public void stateChanged(ChangeEvent JavaDoc 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();
125     }
126     
127     /**
128      * Updates the display when combo 1 is updated.
129      */

130     private void handleCombo1Change() {
131         final String JavaDoc text = this.combo1.getSelectedItem().toString();
132         this.drawStringPanel1.setAnchor(convertStringToAnchor(text));
133         this.drawStringPanel1.invalidate();
134         this.drawStringPanel1.repaint();
135     }
136
137     /**
138      * Updates the display when combo 2 is updated.
139      */

140     private void handleCombo2Change() {
141         final String JavaDoc text = this.combo2.getSelectedItem().toString();
142         this.drawStringPanel2.setAnchor(convertStringToAnchor(text));
143         this.drawStringPanel2.invalidate();
144         this.drawStringPanel2.repaint();
145     }
146
147     /**
148      * Updates the display when combo 3 is updated.
149      */

150     private void handleCombo3Change() {
151         final String JavaDoc text = this.combo3.getSelectedItem().toString();
152         this.drawStringPanel2.setRotationAnchor(convertStringToAnchor(text));
153         this.drawStringPanel2.invalidate();
154         this.drawStringPanel2.repaint();
155     }
156
157     /**
158      * Creates the content pane for the demo frame.
159      *
160      * @return The content pane.
161      */

162     private JPanel JavaDoc createContentPane() {
163         final JPanel JavaDoc content = new JPanel JavaDoc(new BorderLayout JavaDoc());
164         final JTabbedPane JavaDoc tabs = new JTabbedPane JavaDoc();
165         tabs.add("Alignment", createTab1Content());
166         tabs.add("Rotation", createTab2Content());
167         content.add(tabs);
168         return content;
169     }
170
171     /**
172      * Creates the content for tab 1.
173      *
174      * @return The content panel.
175      */

176     private JPanel JavaDoc createTab1Content() {
177         final JPanel JavaDoc content = new JPanel JavaDoc(new BorderLayout JavaDoc());
178         this.combo1 = new JComboBox JavaDoc();
179         this.combo1.setActionCommand("combo1.changed");
180         populateTextAnchorCombo(this.combo1);
181         this.combo1.addActionListener(this);
182
183         final JPanel JavaDoc controls = new JPanel JavaDoc();
184         controls.add(this.combo1);
185
186         final JButton JavaDoc fontButton = new JButton JavaDoc("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);
193         return content;
194     }
195
196     /**
197      * Creates the content for tab 2.
198      *
199      * @return The content panel.
200      */

201     private JPanel JavaDoc createTab2Content() {
202         JPanel JavaDoc content = new JPanel JavaDoc(new BorderLayout JavaDoc());
203         JPanel JavaDoc controls = new JPanel JavaDoc();
204         controls.add(new JLabel JavaDoc("Text anchor: "));
205         this.combo2 = new JComboBox JavaDoc();
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 JavaDoc("Rotation anchor: "));
211         this.combo3 = new JComboBox JavaDoc();
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 JavaDoc(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);
227         return content;
228     }
229
230     /**
231      * Displays a primitive font chooser dialog to allow the user to change the font.
232      */

233     private void displayFontDialog() {
234
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);
239
240         if (result == JOptionPane.OK_OPTION) {
241             this.drawStringPanel1.setFont(panel.getSelectedFont());
242             this.drawStringPanel2.setFont(panel.getSelectedFont());
243         }
244
245     }
246
247     /**
248      * Populates a combo box with the available {@link TextAnchor} options.
249      *
250      * @param combo the combo box.
251      */

252     private void populateTextAnchorCombo(final JComboBox JavaDoc 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");
268     }
269
270     /**
271      * Converts a string to a corresponding {@link TextAnchor} instance.
272      *
273      * @param text the text.
274      *
275      * @return The anchor.
276      */

277     private TextAnchor convertStringToAnchor(final String JavaDoc text) {
278
279         if (text.equals("TextAnchor.TOP_LEFT")) {
280             return TextAnchor.TOP_LEFT;
281         }
282         else if (text.equals("TextAnchor.TOP_CENTER")) {
283             return TextAnchor.TOP_CENTER;
284         }
285         else if (text.equals("TextAnchor.TOP_RIGHT")) {
286             return TextAnchor.TOP_RIGHT;
287         }
288         else if (text.equals("TextAnchor.CENTER_LEFT")) {
289             return TextAnchor.CENTER_LEFT;
290         }
291         else if (text.equals("TextAnchor.CENTER")) {
292             return TextAnchor.CENTER;
293         }
294         else if (text.equals("TextAnchor.CENTER_RIGHT")) {
295             return TextAnchor.CENTER_RIGHT;
296         }
297         else if (text.equals("TextAnchor.HALF_ASCENT_LEFT")) {
298             return TextAnchor.HALF_ASCENT_LEFT;
299         }
300         else if (text.equals("TextAnchor.HALF_ASCENT_CENTER")) {
301             return TextAnchor.HALF_ASCENT_CENTER;
302         }
303         else if (text.equals("TextAnchor.HALF_ASCENT_RIGHT")) {
304             return TextAnchor.HALF_ASCENT_RIGHT;
305         }
306         else if (text.equals("TextAnchor.BASELINE_LEFT")) {
307             return TextAnchor.BASELINE_LEFT;
308         }
309         else if (text.equals("TextAnchor.BASELINE_CENTER")) {
310             return TextAnchor.BASELINE_CENTER;
311         }
312         else if (text.equals("TextAnchor.BASELINE_RIGHT")) {
313             return TextAnchor.BASELINE_RIGHT;
314         }
315         else if (text.equals("TextAnchor.BOTTOM_LEFT")) {
316             return TextAnchor.BOTTOM_LEFT;
317         }
318         else if (text.equals("TextAnchor.BOTTOM_CENTER")) {
319             return TextAnchor.BOTTOM_CENTER;
320         }
321         else if (text.equals("TextAnchor.BOTTOM_RIGHT")) {
322             return TextAnchor.BOTTOM_RIGHT;
323         }
324         else {
325             return null;
326         }
327
328     }
329
330     /**
331      * The starting point for the demo.
332      *
333      * @param args ignored.
334      */

335     public static void main(final String JavaDoc[] args) {
336
337         final DrawStringDemo demo = new DrawStringDemo("DrawString Demo");
338         demo.pack();
339         RefineryUtilities.centerFrameOnScreen(demo);
340         demo.setVisible(true);
341
342     }
343
344 }
345
Popular Tags