KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ThermometerDemo.java
24  * --------------------
25  * (C) Copyright 2002, 2003, by Australian Antarctic Division and Contributors.
26  *
27  * Original Author: Bryan Scott (for Australian Antarctic Division).
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * $Id: ThermometerDemo.java,v 1.6 2003/11/28 10:57:34 mungady Exp $
31  *
32  * Changes (since 24-Apr-2002)
33  * ---------------------------
34  * 24-Apr-2002 : added standard source header (DG);
35  * 17-Sep-2002 : fixed errors reported by Checkstyle 2.3 (DG);
36  * 19-Nov-2003 : Added support for Left value display
37  *
38  */

39 package org.jfree.chart.demo;
40
41 import java.awt.BorderLayout JavaDoc;
42 import java.awt.Color JavaDoc;
43 import java.awt.Dimension JavaDoc;
44 import java.awt.Font JavaDoc;
45 import java.awt.GridLayout JavaDoc;
46 import java.awt.Toolkit JavaDoc;
47 import java.awt.event.ActionEvent JavaDoc;
48 import java.text.DecimalFormat JavaDoc;
49
50 import org.jfree.chart.ChartPanel;
51 import org.jfree.chart.JFreeChart;
52 import org.jfree.chart.plot.JThermometer;
53 import org.jfree.chart.plot.MeterPlot;
54 import org.jfree.data.DefaultValueDataset;
55 import javax.swing.*;
56
57 /**
58  * A demonstration application for the thermometer plot.
59  *
60  * @author Bryan Scott
61  */

62 public class ThermometerDemo
63     extends JPanel {
64
65   /** Options for the value label position combo box. */
66   protected static final String JavaDoc[] OPTIONS = {
67       "None", "Right", "Left", "Bulb"};
68
69   protected static final String JavaDoc[] AXIS_OPTIONS = {
70       "None", "Right", "Left"};
71
72   /** The dataset. */
73   private DefaultValueDataset data = new DefaultValueDataset(20.0);
74
75   /** The meter plot (dial). */
76   private MeterPlot meterplot = new MeterPlot(data);
77
78   /** The meter chart (dial). */
79   private JFreeChart meterchart = new JFreeChart("Meter Chart",
80                                                  JFreeChart.DEFAULT_TITLE_FONT,
81                                                  meterplot, false);
82
83   /** The meter panel. */
84   private ChartPanel panelMeter = new ChartPanel(meterchart);
85
86   /** Panel 1. */
87   private JPanel jPanel1 = new JPanel();
88
89   /** Increment button for thermometer ?. */
90   private JButton butUp3 = new JButton();
91
92   /** Decrement button for thermometer ?. */
93   private JButton butDown3 = new JButton();
94
95   /** Panel 2. */
96   private JPanel jPanel2 = new JPanel();
97
98   /** Borderlayout 2. */
99   private BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
100
101   /** Panel 3. */
102   private JPanel jPanel3 = new JPanel();
103
104   /** Borderlayout 3. */
105   private BorderLayout JavaDoc borderLayout3 = new BorderLayout JavaDoc();
106
107   /** Panel 4. */
108   private JPanel jPanel4 = new JPanel();
109
110   /** Decrement button for thermometer ?. */
111   private JButton butDown2 = new JButton();
112
113   /** Increment button for thermometer ?. */
114   private JButton butUp2 = new JButton();
115
116   /** Panel 5. */
117   private JPanel jPanel5 = new JPanel();
118
119   /** Grid layout 1. */
120   private GridLayout JavaDoc gridLayout1 = new GridLayout JavaDoc();
121
122   /** Panel 6. */
123   private JPanel jPanel6 = new JPanel();
124
125   /** Increment button for thermometer ?. */
126   private JButton butUp1 = new JButton();
127
128   /** Decrement button for thermometer ?. */
129   private JButton butDown1 = new JButton();
130
131   /** Thermometer 1. */
132   private JThermometer thermo1 = new JThermometer();
133
134   /** Thermometer 2. */
135   private JThermometer thermo2 = new JThermometer();
136
137   /** Thermometer 2. */
138   private JThermometer thermo3 = new JThermometer();
139
140   /** Array of thermometers. */
141   private JThermometer[] thermo = new JThermometer[3];
142
143   /** Borderlayout 1. */
144   private BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
145
146   /** Panel 7. */
147   private JPanel jPanel7 = new JPanel();
148
149   /** Panel 8. */
150   private JPanel jPanel8 = new JPanel();
151
152   /** Panel 9. */
153   private JPanel jPanel9 = new JPanel();
154
155   /** Grid layout 2. */
156   private GridLayout JavaDoc gridLayout2 = new GridLayout JavaDoc();
157
158   /** Grid layout 3. */
159   private GridLayout JavaDoc gridLayout3 = new GridLayout JavaDoc();
160
161   /** Grid layout 4. */
162   private GridLayout JavaDoc gridLayout4 = new GridLayout JavaDoc();
163
164   /** Combo box 1 for value label position. */
165   private JComboBox pickShow0 = new JComboBox(OPTIONS);
166
167   /** Combo box 2 for value label position. */
168   private JComboBox pickShow1 = new JComboBox(OPTIONS);
169
170   /** Combo box 3 for value label position. */
171   private JComboBox pickShow2 = new JComboBox(OPTIONS);
172
173   /** Combo box 1 for axis position. */
174   private JComboBox pickAxis0 = new JComboBox(AXIS_OPTIONS);
175   /** Combo box 2 for axis position. */
176   private JComboBox pickAxis1 = new JComboBox(AXIS_OPTIONS);
177   /** Combo box 3 for axis position. */
178   private JComboBox pickAxis2 = new JComboBox(AXIS_OPTIONS);
179
180   /** An array of combo boxes. */
181   private JComboBox[] pickShow = new JComboBox[3];
182
183   /** An array of combo boxes. */
184   private JComboBox[] pickAxis = new JComboBox[3];
185
186   /** Panel 10. */
187   private JPanel jPanel10 = new JPanel();
188
189   /** Borderlayout 4. */
190   private BorderLayout JavaDoc borderLayout4 = new BorderLayout JavaDoc();
191
192   /** Panel 11. */
193   private JPanel jPanel11 = new JPanel();
194
195   /** Decrement button for thermometer ?. */
196   private JButton butDown4 = new JButton();
197
198   /** Increment button for thermometer ?. */
199   private JButton butUp4 = new JButton();
200
201   /**
202    * Default constructor.
203    */

204   public ThermometerDemo() {
205     try {
206       jbInit();
207     }
208     catch (Exception JavaDoc ex) {
209       ex.printStackTrace();
210     }
211   }
212
213   // ****************************************************************************
214
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
215
// * Please note that commercial support and documentation is available from: *
216
// * *
217
// * http://www.object-refinery.com/jfreechart/support.html *
218
// * *
219
// * This is not only a great service for developers, but is a VERY IMPORTANT *
220
// * source of funding for the JFreeChart project. Please support us so that *
221
// * we can continue developing free software. *
222
// ****************************************************************************
223

224   /**
225    * Initialises the class.
226    *
227    * @throws Exception for any exception.
228    */

229   void jbInit() throws Exception JavaDoc {
230
231     //data.setRange(new Double(-20), new Double(20));
232
thermo[0] = thermo1;
233     thermo[1] = thermo2;
234     thermo[2] = thermo3;
235
236     thermo[0].setValue(0.0);
237     thermo[1].setValue(0.2);
238     thermo[2].setValue(0.3);
239
240     thermo[0].setBackground(Color.white);
241     thermo[2].setBackground(Color.white);
242
243     thermo[0].setOutlinePaint(null);
244     thermo[1].setOutlinePaint(null);
245     thermo[2].setOutlinePaint(null);
246
247     thermo[0].setUnits(0);
248     thermo[1].setUnits(1);
249     thermo[2].setUnits(2);
250
251     //thermo[0].setFont(new Font("SansSerif", Font.BOLD, 20));
252
thermo[0].setShowValueLines(true);
253     thermo[0].setFollowDataInSubranges(true);
254     thermo[1].setValueLocation(1);
255
256     thermo[1].setForeground(Color.blue);
257     thermo[2].setForeground(Color.pink);
258
259     thermo[0].setRange( -10.0, 40.0);
260     thermo[0].setSubrangeInfo(0, -50.0, 20.0, -10.0, 22.0);
261     thermo[0].setSubrangeInfo(1, 20.0, 24.0, 18.0, 26.0);
262     thermo[0].setSubrangeInfo(2, 24.0, 100.0, 22.0, 40.0);
263
264     thermo[0].addSubtitle("Sea Water Temp");
265     thermo[1].addSubtitle("Air Temp", new Font JavaDoc("SansSerif", Font.PLAIN, 16));
266     thermo[2].addSubtitle("Ship Temp",
267                           new Font JavaDoc("SansSerif", Font.ITALIC + Font.BOLD, 20));
268
269     thermo[1].setValueFormat(new DecimalFormat JavaDoc("#0.0"));
270     thermo[2].setValueFormat(new DecimalFormat JavaDoc("#0.00"));
271
272     pickShow[0] = pickShow0;
273     pickShow[1] = pickShow1;
274     pickShow[2] = pickShow2;
275
276     pickAxis[0] = pickAxis0;
277     pickAxis[1] = pickAxis1;
278     pickAxis[2] = pickAxis2;
279
280     pickAxis[0].setSelectedIndex(2);
281     pickAxis[1].setSelectedIndex(2);
282     pickAxis[2].setSelectedIndex(2);
283
284     setLayout(gridLayout1);
285     butDown3.setText("<");
286     butDown3.addActionListener(new java.awt.event.ActionListener JavaDoc() {
287       public void actionPerformed(ActionEvent JavaDoc e) {
288         setValue(2, -1);
289       }
290     });
291     butUp3.setText(">");
292     butUp3.addActionListener(new java.awt.event.ActionListener JavaDoc() {
293       public void actionPerformed(ActionEvent JavaDoc e) {
294         setValue(2, 1);
295       }
296     });
297     jPanel1.setLayout(borderLayout2);
298     jPanel3.setLayout(borderLayout3);
299     butDown2.setText("<");
300     butDown2.addActionListener(new java.awt.event.ActionListener JavaDoc() {
301       public void actionPerformed(ActionEvent JavaDoc e) {
302         setValue(1, -1);
303       }
304     });
305     butUp2.setText(">");
306     butUp2.addActionListener(new java.awt.event.ActionListener JavaDoc() {
307       public void actionPerformed(ActionEvent JavaDoc e) {
308         setValue(1, 1);
309       }
310     });
311     butUp1.setText(">");
312     butUp1.addActionListener(new java.awt.event.ActionListener JavaDoc() {
313       public void actionPerformed(ActionEvent JavaDoc e) {
314         setValue(0, 1);
315       }
316     });
317     butDown1.setText("<");
318     butDown1.addActionListener(new java.awt.event.ActionListener JavaDoc() {
319       public void actionPerformed(ActionEvent JavaDoc e) {
320         setValue(0, -1);
321       }
322     });
323     jPanel5.setLayout(borderLayout1);
324     pickShow0.addActionListener(new java.awt.event.ActionListener JavaDoc() {
325       public void actionPerformed(ActionEvent JavaDoc e) {
326         setShowValue(0);
327       }
328     });
329     pickShow1.addActionListener(new java.awt.event.ActionListener JavaDoc() {
330       public void actionPerformed(ActionEvent JavaDoc e) {
331         setShowValue(1);
332       }
333     });
334     pickShow2.addActionListener(new java.awt.event.ActionListener JavaDoc() {
335       public void actionPerformed(ActionEvent JavaDoc e) {
336         setShowValue(2);
337       }
338     });
339
340     pickAxis0.addActionListener(new java.awt.event.ActionListener JavaDoc() {
341       public void actionPerformed(ActionEvent JavaDoc e) {
342         setShowAxis(0);
343       }
344     });
345     pickAxis1.addActionListener(new java.awt.event.ActionListener JavaDoc() {
346       public void actionPerformed(ActionEvent JavaDoc e) {
347         setShowAxis(1);
348       }
349     });
350     pickAxis2.addActionListener(new java.awt.event.ActionListener JavaDoc() {
351       public void actionPerformed(ActionEvent JavaDoc e) {
352         setShowAxis(2);
353       }
354     });
355
356     jPanel9.setLayout(gridLayout2);
357     gridLayout2.setColumns(1);
358     jPanel8.setLayout(gridLayout3);
359     jPanel7.setLayout(gridLayout4);
360     jPanel5.setBorder(BorderFactory.createEtchedBorder());
361     jPanel3.setBorder(BorderFactory.createEtchedBorder());
362     jPanel1.setBorder(BorderFactory.createEtchedBorder());
363     jPanel6.setBackground(Color.white);
364     jPanel2.setBackground(Color.white);
365     jPanel9.setBackground(Color.white);
366     jPanel10.setLayout(borderLayout4);
367     butDown4.setText("<");
368     butDown4.addActionListener(new java.awt.event.ActionListener JavaDoc() {
369       public void actionPerformed(ActionEvent JavaDoc e) {
370         setMeterValue( -1.1);
371       }
372     });
373     butUp4.setText(">");
374     butUp4.addActionListener(new java.awt.event.ActionListener JavaDoc() {
375       public void actionPerformed(ActionEvent JavaDoc e) {
376         setMeterValue(1.1);
377       }
378     });
379     jPanel1.add(thermo3, BorderLayout.CENTER);
380     jPanel1.add(jPanel2, BorderLayout.SOUTH);
381     jPanel2.add(butDown3, null);
382     jPanel2.add(butUp3, null);
383     jPanel1.add(jPanel9, BorderLayout.NORTH);
384     jPanel9.add(pickShow2, null);
385     jPanel9.add(pickAxis2, null);
386     add(jPanel10, null);
387     jPanel10.add(jPanel11, BorderLayout.SOUTH);
388     jPanel11.add(butDown4, null);
389     jPanel11.add(butUp4, null);
390     jPanel4.add(butDown2, null);
391     jPanel4.add(butUp2, null);
392     jPanel3.add(jPanel8, BorderLayout.NORTH);
393     jPanel8.add(pickShow1, null);
394     jPanel8.add(pickAxis1, null);
395     jPanel3.add(thermo2, BorderLayout.CENTER);
396     jPanel3.add(jPanel4, BorderLayout.SOUTH);
397     add(jPanel5, null);
398     jPanel5.add(thermo1, BorderLayout.CENTER);
399     jPanel5.add(jPanel6, BorderLayout.SOUTH);
400     jPanel6.add(butDown1, null);
401     jPanel6.add(butUp1, null);
402     jPanel5.add(jPanel7, BorderLayout.NORTH);
403     jPanel7.add(pickShow0, null);
404     jPanel7.add(pickAxis0, null);
405     add(jPanel3, null);
406     add(jPanel1, null);
407     jPanel10.add(panelMeter, BorderLayout.CENTER);
408   }
409
410   /**
411    * Starting point for the demo application.
412    *
413    * @param args ignored.
414    */

415   public static void main(String JavaDoc[] args) {
416
417     final ThermometerDemo panel = new ThermometerDemo();
418
419     JFrame frame = new JFrame();
420     frame.getContentPane().setLayout(new BorderLayout JavaDoc(5, 5));
421     frame.setDefaultCloseOperation(3);
422     frame.setTitle("Thermometer Test");
423     frame.getContentPane().add(panel, BorderLayout.CENTER);
424     frame.setSize(700, 400);
425     Dimension JavaDoc d = Toolkit.getDefaultToolkit().getScreenSize();
426     frame.setLocation( (d.width - frame.getSize().width) / 2,
427                       (d.height - frame.getSize().height) / 2);
428     frame.setVisible(true);
429
430   }
431
432   /**
433    * Sets the value of one of the thermometers.
434    *
435    * @param thermometer the thermometer index.
436    * @param value the value.
437    */

438   private void setValue(int thermometer, double value) {
439     if ( (thermometer >= 0) && (thermometer < 3)) {
440       try {
441         thermo[thermometer].setValue(thermo[thermometer].getValue().doubleValue() +
442                                      value);
443       }
444       catch (Exception JavaDoc ex) {
445         ex.printStackTrace();
446       }
447     }
448   }
449
450   /**
451    * Sets the meter value.
452    *
453    * @param value the value.
454    */

455   private void setMeterValue(double value) {
456     try {
457       double newValue = value;
458       if (data.getValue() != null) {
459         newValue += data.getValue().doubleValue();
460       }
461       data.setValue(new Double JavaDoc(newValue));
462     }
463     catch (Exception JavaDoc ex) {
464       System.err.println(ex.getMessage());
465     }
466   }
467
468   /**
469    * Sets the value label position for one of the thermometers.
470    *
471    * @param thermometer the thermometer index.
472    */

473   private void setShowValue(int thermometer) {
474     if ( (thermometer >= 0) && (thermometer < 3)) {
475       thermo[thermometer].setValueLocation(pickShow[thermometer].
476                                            getSelectedIndex());
477     }
478   }
479
480   /**
481    * Sets the axis position for one of the thermometers.
482    *
483    * @param thermometer the thermometer index.
484    */

485   private void setShowAxis(int thermometer) {
486     if ( (thermometer >= 0) && (thermometer < 3)) {
487       thermo[thermometer].setShowAxisLocation(pickAxis[thermometer].
488                                            getSelectedIndex());
489     }
490   }
491
492 }
493
Popular Tags