KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > experimental > chart > demo > DialDemo5


1 /* --------------
2  * DialDemo5.java
3  * --------------
4  * (C) Copyright 2006, by Object Refinery Limited.
5  */

6
7 package org.jfree.experimental.chart.demo;
8
9 import java.awt.BorderLayout JavaDoc;
10 import java.awt.Color JavaDoc;
11 import java.awt.Dimension JavaDoc;
12 import java.awt.Font JavaDoc;
13 import java.awt.GridLayout JavaDoc;
14
15 import javax.swing.JFrame JavaDoc;
16 import javax.swing.JLabel JavaDoc;
17 import javax.swing.JPanel JavaDoc;
18 import javax.swing.JSlider JavaDoc;
19 import javax.swing.event.ChangeEvent JavaDoc;
20 import javax.swing.event.ChangeListener JavaDoc;
21
22 import org.jfree.chart.ChartPanel;
23 import org.jfree.chart.JFreeChart;
24 import org.jfree.data.general.DefaultValueDataset;
25 import org.jfree.experimental.chart.plot.dial.DialBackground;
26 import org.jfree.experimental.chart.plot.dial.DialCap;
27 import org.jfree.experimental.chart.plot.dial.DialPlot;
28 import org.jfree.experimental.chart.plot.dial.DialPointer;
29 import org.jfree.experimental.chart.plot.dial.SimpleDialFrame;
30 import org.jfree.experimental.chart.plot.dial.StandardDialScale;
31 import org.jfree.ui.GradientPaintTransformType;
32 import org.jfree.ui.StandardGradientPaintTransformer;
33
34 /**
35  * A sample application showing the use of a {@link DialPlot}.
36  */

37 public class DialDemo5 extends JFrame JavaDoc implements ChangeListener JavaDoc {
38     
39     /** The first dataset. */
40     DefaultValueDataset hoursDataset;
41     
42     /** The second dataset. */
43     DefaultValueDataset dataset2;
44     
45     /** A slider to update the first dataset value. */
46     JSlider JavaDoc slider1;
47     
48     /** A slider to update the second dataset value. */
49     JSlider JavaDoc slider2;
50     
51     /**
52      * Creates a new instance.
53      *
54      * @param title the frame title.
55      */

56     public DialDemo5(String JavaDoc title) {
57         super(title);
58         
59         hoursDataset = new DefaultValueDataset(6.0);
60         dataset2 = new DefaultValueDataset(15.0);
61         
62         // get data for diagrams
63
DialPlot plot = new DialPlot();
64         plot.setView(0.0, 0.0, 1.0, 1.0);
65         plot.setDataset(0, hoursDataset);
66         plot.setDataset(1, dataset2);
67         SimpleDialFrame dialFrame = new SimpleDialFrame();
68         dialFrame.setBackgroundPaint(Color.lightGray);
69         dialFrame.setForegroundPaint(Color.darkGray);
70         plot.setDialFrame(dialFrame);
71         
72         DialBackground db = new DialBackground(Color.white);
73         db.setGradientPaintTransformer(new StandardGradientPaintTransformer(
74                 GradientPaintTransformType.VERTICAL));
75         plot.setBackground(db);
76         
77         StandardDialScale hourScale = new StandardDialScale(0, 12, 90, -360);
78         hourScale.setFirstTickLabelVisible(false);
79         hourScale.setMajorTickIncrement(1.0);
80         hourScale.setTickRadius(0.88);
81         hourScale.setTickLabelOffset(0.15);
82         hourScale.setTickLabelFont(new Font JavaDoc("Dialog", Font.PLAIN, 14));
83         plot.addScale(0, hourScale);
84
85         StandardDialScale scale2 = new StandardDialScale(0, 60, 90, -360);
86         scale2.setVisible(false);
87         scale2.setMajorTickIncrement(5.0);
88         scale2.setTickRadius(0.68);
89         scale2.setTickLabelOffset(0.15);
90         scale2.setTickLabelFont(new Font JavaDoc("Dialog", Font.PLAIN, 14));
91         
92         plot.addScale(1, scale2);
93         
94         DialPointer needle2 = new DialPointer.Pointer(0);
95         needle2.setRadius(0.55);
96         plot.addLayer(needle2);
97         
98         plot.mapDatasetToScale(1, 1);
99
100         DialPointer needle = new DialPointer.Pointer(1);
101         plot.addLayer(needle);
102         
103         DialCap cap = new DialCap();
104         cap.setRadius(0.10);
105         plot.setCap(cap);
106         
107         JFreeChart chart1 = new JFreeChart(plot);
108         chart1.setTitle("Dial Demo 5");
109         ChartPanel cp1 = new ChartPanel(chart1);
110         cp1.setPreferredSize(new Dimension JavaDoc(400, 400));
111         
112         JPanel JavaDoc sliderPanel = new JPanel JavaDoc(new GridLayout JavaDoc(2, 2));
113         sliderPanel.add(new JLabel JavaDoc("Hours:"));
114         sliderPanel.add(new JLabel JavaDoc("Minutes:"));
115         slider1 = new JSlider JavaDoc(0, 12);
116         slider1.setMajorTickSpacing(2);
117         slider1.setPaintTicks(true);
118         slider1.setPaintLabels(true);
119         slider1.addChangeListener(this);
120         sliderPanel.add(slider1);
121         sliderPanel.add(slider1);
122         slider2 = new JSlider JavaDoc(0, 60);
123         slider2.setValue(15);
124         slider2.setMajorTickSpacing(10);
125         slider2.setPaintTicks(true);
126         slider2.setPaintLabels(true);
127         slider2.addChangeListener(this);
128         sliderPanel.add(slider2);
129         JPanel JavaDoc content = new JPanel JavaDoc(new BorderLayout JavaDoc());
130         content.add(cp1);
131         content.add(sliderPanel, BorderLayout.SOUTH);
132         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
133         setContentPane(content);
134     }
135     
136     /**
137      * Handle a change in the slider by updating the dataset value. This
138      * automatically triggers a chart repaint.
139      *
140      * @param e the event.
141      */

142     public void stateChanged(ChangeEvent JavaDoc e) {
143         this.hoursDataset.setValue(new Integer JavaDoc(slider1.getValue()));
144         this.dataset2.setValue(new Integer JavaDoc(slider2.getValue()));
145     }
146
147     /**
148      * Starting point for the demo application.
149      *
150      * @param args ignored.
151      */

152     public static void main(String JavaDoc[] args) {
153         DialDemo5 app = new DialDemo5("JFreeChart - Dial Demo 5");
154         app.pack();
155         app.setVisible(true);
156     }
157
158 }
159
Popular Tags