KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > demo > CompassDemo


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * ----------------
23  * CompassDemo.java
24  * ----------------
25  * (C) Copyright 2002, 2003, by the Australian Antarctic Division and Contributors.
26  *
27  * Original Author: Bryan Scott (for the Australian Antarctic Division);
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * $Id: CompassDemo.java,v 1.6 2003/11/28 10:57:33 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 25-Sep-2002 : Version 1, contributed by Bryan Scott (DG);
35  * 10-Oct-2002 : Fixed errors reported by Checkstyle (DG);
36  * 27-Mar-2003 : Changed dataset to ValueDataset (DG);
37  *
38  */

39
40 package org.jfree.chart.demo;
41
42 import java.awt.BorderLayout JavaDoc;
43 import java.awt.Color JavaDoc;
44 import java.awt.Dimension JavaDoc;
45 import java.awt.GridBagConstraints JavaDoc;
46 import java.awt.GridBagLayout JavaDoc;
47 import java.awt.GridLayout JavaDoc;
48 import java.awt.Insets JavaDoc;
49 import java.awt.Toolkit JavaDoc;
50 import java.awt.event.ActionEvent JavaDoc;
51 import java.beans.PropertyChangeEvent JavaDoc;
52 import java.beans.PropertyChangeListener JavaDoc;
53
54 import javax.swing.JCheckBox JavaDoc;
55 import javax.swing.JComboBox JavaDoc;
56 import javax.swing.JFrame JavaDoc;
57 import javax.swing.JPanel JavaDoc;
58 import javax.swing.SwingConstants JavaDoc;
59 import javax.swing.border.TitledBorder JavaDoc;
60
61 import org.jfree.chart.ChartPanel;
62 import org.jfree.chart.JFreeChart;
63 import org.jfree.chart.plot.CompassPlot;
64 import org.jfree.data.DefaultValueDataset;
65 import org.jfree.ui.Spinner;
66
67 /**
68  * A demo application showing how to use the {@link CompassPlot} class.
69  *
70  * @author Bryan Scott
71  */

72 public class CompassDemo extends JPanel JavaDoc {
73
74     /** The available needle types. */
75     public static final String JavaDoc[] NEEDLE_TYPES
76         = {"Arrow", "Line", "Long", "Pin", "Plum", "Pointer", "Ship", "Wind", "Arrow"};
77
78     /** Dataset 1. */
79     private DefaultValueDataset compassData
80         = new DefaultValueDataset(new Double JavaDoc(0.0));
81
82     /** Dataset 2. */
83     private DefaultValueDataset shipData
84         = new DefaultValueDataset(new Double JavaDoc(0.0));
85
86     /** The compass plot. */
87     private CompassPlot compassPlot = new CompassPlot(compassData);
88
89     /** The chart. */
90     private JFreeChart compassChart = new JFreeChart("Compass Test",
91                                                      JFreeChart.DEFAULT_TITLE_FONT,
92                                                      compassPlot, false);
93
94     /** The chart panel. */
95     private ChartPanel panelCompass = new ChartPanel(compassChart);
96
97     /** A grid layout. */
98     private GridLayout JavaDoc gridLayout1 = new GridLayout JavaDoc();
99
100     /** A panel. */
101     private JPanel JavaDoc panelCompassHolder = new JPanel JavaDoc();
102
103     /** A border layout. */
104     private BorderLayout JavaDoc borderLayout = new BorderLayout JavaDoc();
105
106     /** A panel. */
107     private JPanel JavaDoc jPanel12 = new JPanel JavaDoc();
108
109     /** A checkbox. */
110     private JCheckBox JavaDoc windNullCheckBox = new JCheckBox JavaDoc();
111
112     /** A checkbox. */
113     private JCheckBox JavaDoc shipNullCheckBox = new JCheckBox JavaDoc();
114
115     // SpinnerNumberModel modelWind = new SpinnerNumberModel(0, -1, 361, 1);
116
// SpinnerNumberModel modelShip = new SpinnerNumberModel(0, -1, 361, 1);
117
// JSpinner spinWind = new JSpinner(modelWind);
118
// JSpinner spinShip = new JSpinner(modelShip);
119

120     /** The wind spinner control. */
121     private Spinner spinWind = new Spinner(270);
122
123     /** The ship spinner control. */
124     private Spinner spinShip = new Spinner(45);
125
126     /** A panel. */
127     private JPanel JavaDoc jPanel1 = new JPanel JavaDoc();
128
129     /** A combo box. */
130     private JComboBox JavaDoc pick1Pointer = new JComboBox JavaDoc(NEEDLE_TYPES);
131
132     /** A panel. */
133     private JPanel JavaDoc jPanel2 = new JPanel JavaDoc();
134
135     /** A combo box. */
136     private JComboBox JavaDoc pick2Pointer = new JComboBox JavaDoc(NEEDLE_TYPES);
137
138     /** A titled border. */
139     private TitledBorder JavaDoc titledBorder1;
140
141     /** A titled border. */
142     private TitledBorder JavaDoc titledBorder2;
143
144     /** A grid bag layout. */
145     private GridBagLayout JavaDoc gridBagLayout1 = new GridBagLayout JavaDoc();
146
147     /** A grid bag layout. */
148     private GridBagLayout JavaDoc gridBagLayout2 = new GridBagLayout JavaDoc();
149
150     /** A titled border. */
151     private TitledBorder JavaDoc titledBorder3;
152
153     /** A grid layout. */
154     private GridLayout JavaDoc gridLayout2 = new GridLayout JavaDoc();
155
156     /**
157      * Default constructor.
158      */

159     public CompassDemo() {
160         try {
161             compassPlot.addData(shipData);
162             compassPlot.setSeriesNeedle(0, 7);
163             compassPlot.setSeriesNeedle(1, 5);
164             compassPlot.setSeriesPaint(0, Color.blue);
165             compassPlot.setSeriesOutlinePaint(0, Color.blue);
166             compassPlot.setSeriesPaint(1, Color.red);
167             compassPlot.setSeriesOutlinePaint(1, Color.red);
168             pick1Pointer.setSelectedIndex(7);
169             pick2Pointer.setSelectedIndex(5);
170             jbInit();
171         }
172         catch (Exception JavaDoc ex) {
173             ex.printStackTrace();
174         }
175     }
176
177     /**
178      * Initialises the user interface.
179      *
180      * @throws Exception if there are any exceptions.
181      */

182     void jbInit() throws Exception JavaDoc {
183         titledBorder1 = new TitledBorder JavaDoc("");
184         titledBorder2 = new TitledBorder JavaDoc("");
185         titledBorder3 = new TitledBorder JavaDoc("");
186         setLayout(gridLayout1);
187         panelCompassHolder.setLayout(borderLayout);
188         windNullCheckBox.setHorizontalTextPosition(SwingConstants.LEADING);
189         windNullCheckBox.setText("Null");
190         windNullCheckBox.addActionListener(new java.awt.event.ActionListener JavaDoc() {
191             public void actionPerformed(ActionEvent JavaDoc e) {
192                 checkWindNullActionPerformed(e);
193             }
194         });
195         shipNullCheckBox.setHorizontalTextPosition(SwingConstants.LEFT);
196         shipNullCheckBox.setText("Null");
197         shipNullCheckBox.addActionListener(new java.awt.event.ActionListener JavaDoc() {
198             public void actionPerformed(ActionEvent JavaDoc e) {
199                 checkShipNullActionPerformed(e);
200             }
201         });
202
203         spinShip.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
204             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
205                 Spinner spinner = (Spinner) evt.getSource();
206                 //shipData.setValue((new Double(((Integer)spinner.getValue()).intValue())));
207
shipData.setValue(new Double JavaDoc(spinner.getValue()));
208             }
209         });
210
211         spinWind.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
212             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
213                 Spinner spinner = (Spinner) evt.getSource();
214                 // compassData.setValue((new Double(((Integer)spinner.getValue()).intValue())));
215
compassData.setValue(new Double JavaDoc(spinner.getValue()));
216             }
217         });
218         jPanel12.setLayout(gridLayout2);
219         jPanel2.setBorder(titledBorder1);
220         jPanel2.setLayout(gridBagLayout2);
221         jPanel1.setBorder(titledBorder2);
222         jPanel1.setLayout(gridBagLayout1);
223         titledBorder1.setTitle("Second Pointer");
224         titledBorder2.setTitle("First Pointer");
225         titledBorder3.setTitle("Plot Options");
226         pick2Pointer.addActionListener(new java.awt.event.ActionListener JavaDoc() {
227             public void actionPerformed(ActionEvent JavaDoc e) {
228                 pick2PointerActionPerformed(e);
229             }
230         });
231         pick1Pointer.addActionListener(new java.awt.event.ActionListener JavaDoc() {
232             public void actionPerformed(ActionEvent JavaDoc e) {
233                 pick1PointerActionPerformed(e);
234             }
235         });
236         add(panelCompassHolder, null);
237         panelCompassHolder.add(jPanel12, BorderLayout.SOUTH);
238         jPanel12.add(jPanel1, null);
239
240         jPanel1.add(pick1Pointer,
241                     new GridBagConstraints JavaDoc(0, 0, 1, 1, 1.0, 0.0,
242                                            GridBagConstraints.CENTER,
243                                            GridBagConstraints.HORIZONTAL,
244                                            new Insets JavaDoc(0, 0, 0, 0),
245                                            0, 0));
246
247         jPanel1.add(windNullCheckBox,
248                     new GridBagConstraints JavaDoc(1, 0, 1, 1, 0.0, 0.0,
249                                            GridBagConstraints.CENTER,
250                                            GridBagConstraints.NONE,
251                                            new Insets JavaDoc(0, 0, 0, 0),
252                                            0, 0));
253
254         jPanel1.add(spinWind,
255                     new GridBagConstraints JavaDoc(2, 0, 1, 1, 1.0, 0.0,
256                                            GridBagConstraints.CENTER,
257                                            GridBagConstraints.BOTH,
258                                            new Insets JavaDoc(0, 0, 0, 0),
259                                            0, 0));
260
261         jPanel12.add(jPanel2, null);
262
263         jPanel2.add(pick2Pointer,
264                     new GridBagConstraints JavaDoc(0, 0, 1, 1, 1.0, 0.0,
265                                            GridBagConstraints.CENTER,
266                                            GridBagConstraints.HORIZONTAL,
267                                            new Insets JavaDoc(0, 0, 0, 0),
268                                            0, 0));
269
270         jPanel2.add(shipNullCheckBox,
271                     new GridBagConstraints JavaDoc(1, 0, 1, 1, 0.0, 0.0,
272                                            GridBagConstraints.CENTER,
273                                            GridBagConstraints.NONE,
274                                            new Insets JavaDoc(0, 0, 0, 0),
275                                            0, 0));
276
277         jPanel2.add(spinShip,
278                     new GridBagConstraints JavaDoc(2, 0, 1, 1, 1.0, 0.0,
279                     GridBagConstraints.CENTER,
280                     GridBagConstraints.BOTH,
281                     new Insets JavaDoc(0, 0, 0, 0),
282                     0, 0));
283
284         panelCompassHolder.add(panelCompass, BorderLayout.CENTER);
285
286     }
287
288     /**
289      * Entry point for the demo application.
290      *
291      * @param args ignored.
292      */

293     public static void main(String JavaDoc[] args) {
294
295         final CompassDemo panel = new CompassDemo();
296
297         JFrame JavaDoc frame = new JFrame JavaDoc();
298         frame.getContentPane().setLayout(new BorderLayout JavaDoc(5, 5));
299         frame.setDefaultCloseOperation(3);
300         frame.setTitle("Compass Demo");
301         frame.getContentPane().add(panel, BorderLayout.CENTER);
302         frame.setSize(700, 400);
303         Dimension JavaDoc d = Toolkit.getDefaultToolkit().getScreenSize();
304         frame.setLocation((d.width - frame.getSize().width) / 2,
305                           (d.height - frame.getSize().height) / 2);
306         frame.setVisible(true);
307     }
308
309     // ****************************************************************************
310
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
311
// * Please note that commercial support and documentation is available from: *
312
// * *
313
// * http://www.object-refinery.com/jfreechart/support.html *
314
// * *
315
// * This is not only a great service for developers, but is a VERY IMPORTANT *
316
// * source of funding for the JFreeChart project. Please support us so that *
317
// * we can continue developing free software. *
318
// ****************************************************************************
319

320     /**
321      * Updates the data.
322      *
323      * @param value the value.
324      */

325     public void adjustData(double value) {
326
327         Number JavaDoc val = compassData.getValue();
328         double newVal = value;
329
330         if (val != null) {
331             newVal += val.doubleValue();
332         }
333
334         if (newVal > 360) {
335             newVal = 0;
336         }
337
338         if (newVal < 0) {
339             newVal = 360;
340         }
341
342         compassData.setValue(new Double JavaDoc(newVal));
343
344     }
345
346     /**
347      * Handles an action event.
348      *
349      * @param e the event.
350      */

351     void checkWindNullActionPerformed(ActionEvent JavaDoc e) {
352
353         if (windNullCheckBox.isSelected()) {
354             compassData.setValue(null);
355             spinWind.setEnabled(false);
356         }
357         else {
358             // compassData.setValue((new Double(((Integer)spinWind.getValue()).intValue())));
359
compassData.setValue(new Double JavaDoc(spinWind.getValue()));
360             spinWind.setEnabled(true);
361         }
362     }
363
364     /**
365      * Handles an action event.
366      *
367      * @param e the event.
368      */

369     void checkShipNullActionPerformed(ActionEvent JavaDoc e) {
370
371         if (shipNullCheckBox.isSelected()) {
372             shipData.setValue(null);
373             spinShip.setEnabled(false);
374         }
375         else {
376            // shipData.setValue((new Double(((Integer)spinShip.getValue()).intValue())));
377
shipData.setValue(new Double JavaDoc(spinShip.getValue()));
378            spinShip.setEnabled(true);
379         }
380     }
381
382     /**
383      * Handles an action event.
384      *
385      * @param e the event.
386      */

387     void pick2PointerActionPerformed(ActionEvent JavaDoc e) {
388         compassPlot.setSeriesNeedle(1, pick2Pointer.getSelectedIndex());
389         compassPlot.setSeriesPaint(1, Color.red);
390         compassPlot.setSeriesOutlinePaint(1, Color.red);
391     }
392
393     /**
394      * Handles an action event.
395      *
396      * @param e the event.
397      */

398     void pick1PointerActionPerformed(ActionEvent JavaDoc e) {
399         compassPlot.setSeriesNeedle(0, pick1Pointer.getSelectedIndex());
400         compassPlot.setSeriesPaint(0, Color.blue);
401         compassPlot.setSeriesOutlinePaint(0, Color.blue);
402     }
403
404 }
405
Popular Tags