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: * DrawStringDemo.java
029: * -------------------
030: * (C) Copyright 2003-2005, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: DrawStringDemo.java,v 1.4 2005/10/18 13:15:41 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 10-Jun-2003 : Version 1;
040: *
041: */
042:
043: package org.jfree.demo;
044:
045: import java.awt.BorderLayout;
046: import java.awt.event.ActionEvent;
047: import java.awt.event.ActionListener;
048:
049: import javax.swing.JButton;
050: import javax.swing.JComboBox;
051: import javax.swing.JLabel;
052: import javax.swing.JOptionPane;
053: import javax.swing.JPanel;
054: import javax.swing.JSlider;
055: import javax.swing.JTabbedPane;
056: import javax.swing.SwingConstants;
057: import javax.swing.event.ChangeEvent;
058: import javax.swing.event.ChangeListener;
059:
060: import org.jfree.ui.ApplicationFrame;
061: import org.jfree.ui.FontChooserPanel;
062: import org.jfree.ui.RefineryUtilities;
063: import org.jfree.ui.TextAnchor;
064:
065: /**
066: * A demo of some of the string drawing methods in the JCommon class library.
067: */
068: public class DrawStringDemo extends ApplicationFrame implements
069: ActionListener, ChangeListener {
070:
071: /** The alignment anchor for the first panel. */
072: private JComboBox combo1;
073:
074: /** The alignment anchor for the second panel. */
075: private JComboBox combo2;
076:
077: /** The rotation anchor for the second panel. */
078: private JComboBox combo3;
079:
080: /** A slider for the second panel - controls the angle of rotation. */
081: private JSlider slider;
082:
083: /** String panel 1. */
084: private DrawStringPanel drawStringPanel1;
085:
086: /** String panel 2. */
087: private DrawStringPanel drawStringPanel2;
088:
089: /**
090: * Creates a new demo instance.
091: *
092: * @param title the frame title.
093: */
094: public DrawStringDemo(final String title) {
095: super (title);
096: setContentPane(createContentPane());
097: }
098:
099: /**
100: * Receives action events.
101: *
102: * @param event the event.
103: */
104: public void actionPerformed(final ActionEvent 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 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 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 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 text = this .combo3.getSelectedItem().toString();
152: this .drawStringPanel2
153: .setRotationAnchor(convertStringToAnchor(text));
154: this .drawStringPanel2.invalidate();
155: this .drawStringPanel2.repaint();
156: }
157:
158: /**
159: * Creates the content pane for the demo frame.
160: *
161: * @return The content pane.
162: */
163: private JPanel createContentPane() {
164: final JPanel content = new JPanel(new BorderLayout());
165: final JTabbedPane tabs = new JTabbedPane();
166: tabs.add("Alignment", createTab1Content());
167: tabs.add("Rotation", createTab2Content());
168: content.add(tabs);
169: return content;
170: }
171:
172: /**
173: * Creates the content for tab 1.
174: *
175: * @return The content panel.
176: */
177: private JPanel createTab1Content() {
178: final JPanel content = new JPanel(new BorderLayout());
179: this .combo1 = new JComboBox();
180: this .combo1.setActionCommand("combo1.changed");
181: populateTextAnchorCombo(this .combo1);
182: this .combo1.addActionListener(this );
183:
184: final JPanel controls = new JPanel();
185: controls.add(this .combo1);
186:
187: final JButton fontButton = new JButton("Select Font...");
188: fontButton.setActionCommand("fontButton.clicked");
189: fontButton.addActionListener(this );
190: controls.add(fontButton);
191: content.add(controls, BorderLayout.NORTH);
192: this .drawStringPanel1 = new DrawStringPanel("0123456789", false);
193: content.add(this .drawStringPanel1);
194: return content;
195: }
196:
197: /**
198: * Creates the content for tab 2.
199: *
200: * @return The content panel.
201: */
202: private JPanel createTab2Content() {
203: JPanel content = new JPanel(new BorderLayout());
204: JPanel controls = new JPanel();
205: controls.add(new JLabel("Text anchor: "));
206: this .combo2 = new JComboBox();
207: populateTextAnchorCombo(this .combo2);
208: this .combo2.setActionCommand("combo2.changed");
209: this .combo2.addActionListener(this );
210: controls.add(this .combo2);
211: controls.add(new JLabel("Rotation anchor: "));
212: this .combo3 = new JComboBox();
213: populateTextAnchorCombo(this .combo3);
214: this .combo3.setActionCommand("combo3.changed");
215: this .combo3.addActionListener(this );
216: controls.add(this .combo3);
217: this .slider = new JSlider(SwingConstants.VERTICAL, 0, 360, 0);
218: this .slider.setMajorTickSpacing(45);
219: this .slider.setMinorTickSpacing(5);
220: this .slider.setPaintLabels(true);
221: this .slider.setPaintTicks(true);
222: this .slider.setPaintTrack(true);
223: this .slider.addChangeListener(this );
224: content.add(controls, BorderLayout.NORTH);
225: content.add(this .slider, BorderLayout.WEST);
226: this .drawStringPanel2 = new DrawStringPanel("Rotated Text",
227: true);
228: content.add(this .drawStringPanel2);
229: return content;
230: }
231:
232: /**
233: * Displays a primitive font chooser dialog to allow the user to change the font.
234: */
235: private void displayFontDialog() {
236:
237: final FontChooserPanel panel = new FontChooserPanel(
238: this .drawStringPanel1.getFont());
239: final int result = JOptionPane.showConfirmDialog(this , panel,
240: "Font Selection", JOptionPane.OK_CANCEL_OPTION,
241: JOptionPane.PLAIN_MESSAGE);
242:
243: if (result == JOptionPane.OK_OPTION) {
244: this .drawStringPanel1.setFont(panel.getSelectedFont());
245: this .drawStringPanel2.setFont(panel.getSelectedFont());
246: }
247:
248: }
249:
250: /**
251: * Populates a combo box with the available {@link TextAnchor} options.
252: *
253: * @param combo the combo box.
254: */
255: private void populateTextAnchorCombo(final JComboBox combo) {
256: combo.addItem("TextAnchor.TOP_LEFT");
257: combo.addItem("TextAnchor.TOP_CENTER");
258: combo.addItem("TextAnchor.TOP_RIGHT");
259: combo.addItem("TextAnchor.HALF_ASCENT_LEFT");
260: combo.addItem("TextAnchor.HALF_ASCENT_CENTER");
261: combo.addItem("TextAnchor.HALF_ASCENT_RIGHT");
262: combo.addItem("TextAnchor.CENTER_LEFT");
263: combo.addItem("TextAnchor.CENTER");
264: combo.addItem("TextAnchor.CENTER_RIGHT");
265: combo.addItem("TextAnchor.BASELINE_LEFT");
266: combo.addItem("TextAnchor.BASELINE_CENTER");
267: combo.addItem("TextAnchor.BASELINE_RIGHT");
268: combo.addItem("TextAnchor.BOTTOM_LEFT");
269: combo.addItem("TextAnchor.BOTTOM_CENTER");
270: combo.addItem("TextAnchor.BOTTOM_RIGHT");
271: }
272:
273: /**
274: * Converts a string to a corresponding {@link TextAnchor} instance.
275: *
276: * @param text the text.
277: *
278: * @return The anchor.
279: */
280: private TextAnchor convertStringToAnchor(final String text) {
281:
282: if (text.equals("TextAnchor.TOP_LEFT")) {
283: return TextAnchor.TOP_LEFT;
284: } else if (text.equals("TextAnchor.TOP_CENTER")) {
285: return TextAnchor.TOP_CENTER;
286: } else if (text.equals("TextAnchor.TOP_RIGHT")) {
287: return TextAnchor.TOP_RIGHT;
288: } else if (text.equals("TextAnchor.CENTER_LEFT")) {
289: return TextAnchor.CENTER_LEFT;
290: } else if (text.equals("TextAnchor.CENTER")) {
291: return TextAnchor.CENTER;
292: } else if (text.equals("TextAnchor.CENTER_RIGHT")) {
293: return TextAnchor.CENTER_RIGHT;
294: } else if (text.equals("TextAnchor.HALF_ASCENT_LEFT")) {
295: return TextAnchor.HALF_ASCENT_LEFT;
296: } else if (text.equals("TextAnchor.HALF_ASCENT_CENTER")) {
297: return TextAnchor.HALF_ASCENT_CENTER;
298: } else if (text.equals("TextAnchor.HALF_ASCENT_RIGHT")) {
299: return TextAnchor.HALF_ASCENT_RIGHT;
300: } else if (text.equals("TextAnchor.BASELINE_LEFT")) {
301: return TextAnchor.BASELINE_LEFT;
302: } else if (text.equals("TextAnchor.BASELINE_CENTER")) {
303: return TextAnchor.BASELINE_CENTER;
304: } else if (text.equals("TextAnchor.BASELINE_RIGHT")) {
305: return TextAnchor.BASELINE_RIGHT;
306: } else if (text.equals("TextAnchor.BOTTOM_LEFT")) {
307: return TextAnchor.BOTTOM_LEFT;
308: } else if (text.equals("TextAnchor.BOTTOM_CENTER")) {
309: return TextAnchor.BOTTOM_CENTER;
310: } else if (text.equals("TextAnchor.BOTTOM_RIGHT")) {
311: return TextAnchor.BOTTOM_RIGHT;
312: } else {
313: return null;
314: }
315:
316: }
317:
318: /**
319: * The starting point for the demo.
320: *
321: * @param args ignored.
322: */
323: public static void main(final String[] args) {
324:
325: final DrawStringDemo demo = new DrawStringDemo(
326: "DrawString Demo");
327: demo.pack();
328: RefineryUtilities.centerFrameOnScreen(demo);
329: demo.setVisible(true);
330:
331: }
332:
333: }
|