001: /* --------------
002: * DialDemo5.java
003: * --------------
004: * (C) Copyright 2006, by Object Refinery Limited.
005: */
006:
007: package org.jfree.experimental.chart.demo;
008:
009: import java.awt.BorderLayout;
010: import java.awt.Color;
011: import java.awt.Dimension;
012: import java.awt.Font;
013: import java.awt.GridLayout;
014:
015: import javax.swing.JFrame;
016: import javax.swing.JLabel;
017: import javax.swing.JPanel;
018: import javax.swing.JSlider;
019: import javax.swing.event.ChangeEvent;
020: import javax.swing.event.ChangeListener;
021:
022: import org.jfree.chart.ChartPanel;
023: import org.jfree.chart.JFreeChart;
024: import org.jfree.data.general.DefaultValueDataset;
025: import org.jfree.experimental.chart.plot.dial.DialBackground;
026: import org.jfree.experimental.chart.plot.dial.DialCap;
027: import org.jfree.experimental.chart.plot.dial.DialPlot;
028: import org.jfree.experimental.chart.plot.dial.DialPointer;
029: import org.jfree.experimental.chart.plot.dial.SimpleDialFrame;
030: import org.jfree.experimental.chart.plot.dial.StandardDialScale;
031: import org.jfree.ui.GradientPaintTransformType;
032: import org.jfree.ui.StandardGradientPaintTransformer;
033:
034: /**
035: * A sample application showing the use of a {@link DialPlot}.
036: */
037: public class DialDemo5 extends JFrame implements ChangeListener {
038:
039: /** The first dataset. */
040: DefaultValueDataset hoursDataset;
041:
042: /** The second dataset. */
043: DefaultValueDataset dataset2;
044:
045: /** A slider to update the first dataset value. */
046: JSlider slider1;
047:
048: /** A slider to update the second dataset value. */
049: JSlider slider2;
050:
051: /**
052: * Creates a new instance.
053: *
054: * @param title the frame title.
055: */
056: public DialDemo5(String title) {
057: super (title);
058:
059: this .hoursDataset = new DefaultValueDataset(6.0);
060: this .dataset2 = new DefaultValueDataset(15.0);
061:
062: // get data for diagrams
063: DialPlot plot = new DialPlot();
064: plot.setView(0.0, 0.0, 1.0, 1.0);
065: plot.setDataset(0, this .hoursDataset);
066: plot.setDataset(1, this .dataset2);
067: SimpleDialFrame dialFrame = new SimpleDialFrame();
068: dialFrame.setBackgroundPaint(Color.lightGray);
069: dialFrame.setForegroundPaint(Color.darkGray);
070: plot.setDialFrame(dialFrame);
071:
072: DialBackground db = new DialBackground(Color.white);
073: db
074: .setGradientPaintTransformer(new StandardGradientPaintTransformer(
075: GradientPaintTransformType.VERTICAL));
076: plot.setBackground(db);
077:
078: StandardDialScale hourScale = new StandardDialScale(0, 12, 90,
079: -360);
080: hourScale.setFirstTickLabelVisible(false);
081: hourScale.setMajorTickIncrement(1.0);
082: hourScale.setTickRadius(0.88);
083: hourScale.setTickLabelOffset(0.15);
084: hourScale.setTickLabelFont(new Font("Dialog", Font.PLAIN, 14));
085: plot.addScale(0, hourScale);
086:
087: StandardDialScale scale2 = new StandardDialScale(0, 60, 90,
088: -360);
089: scale2.setVisible(false);
090: scale2.setMajorTickIncrement(5.0);
091: scale2.setTickRadius(0.68);
092: scale2.setTickLabelOffset(0.15);
093: scale2.setTickLabelFont(new Font("Dialog", Font.PLAIN, 14));
094:
095: plot.addScale(1, scale2);
096:
097: DialPointer needle2 = new DialPointer.Pointer(0);
098: needle2.setRadius(0.55);
099: plot.addLayer(needle2);
100:
101: plot.mapDatasetToScale(1, 1);
102:
103: DialPointer needle = new DialPointer.Pointer(1);
104: plot.addLayer(needle);
105:
106: DialCap cap = new DialCap();
107: cap.setRadius(0.10);
108: plot.setCap(cap);
109:
110: JFreeChart chart1 = new JFreeChart(plot);
111: chart1.setTitle("Dial Demo 5");
112: ChartPanel cp1 = new ChartPanel(chart1);
113: cp1.setPreferredSize(new Dimension(400, 400));
114:
115: JPanel sliderPanel = new JPanel(new GridLayout(2, 2));
116: sliderPanel.add(new JLabel("Hours:"));
117: sliderPanel.add(new JLabel("Minutes:"));
118: this .slider1 = new JSlider(0, 12);
119: this .slider1.setMajorTickSpacing(2);
120: this .slider1.setPaintTicks(true);
121: this .slider1.setPaintLabels(true);
122: this .slider1.addChangeListener(this );
123: sliderPanel.add(this .slider1);
124: sliderPanel.add(this .slider1);
125: this .slider2 = new JSlider(0, 60);
126: this .slider2.setValue(15);
127: this .slider2.setMajorTickSpacing(10);
128: this .slider2.setPaintTicks(true);
129: this .slider2.setPaintLabels(true);
130: this .slider2.addChangeListener(this );
131: sliderPanel.add(this .slider2);
132: JPanel content = new JPanel(new BorderLayout());
133: content.add(cp1);
134: content.add(sliderPanel, BorderLayout.SOUTH);
135: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
136: setContentPane(content);
137: }
138:
139: /**
140: * Handle a change in the slider by updating the dataset value. This
141: * automatically triggers a chart repaint.
142: *
143: * @param e the event.
144: */
145: public void stateChanged(ChangeEvent e) {
146: this .hoursDataset
147: .setValue(new Integer(this .slider1.getValue()));
148: this .dataset2.setValue(new Integer(this .slider2.getValue()));
149: }
150:
151: /**
152: * Starting point for the demo application.
153: *
154: * @param args ignored.
155: */
156: public static void main(String[] args) {
157: DialDemo5 app = new DialDemo5("JFreeChart - Dial Demo 5");
158: app.pack();
159: app.setVisible(true);
160: }
161:
162: }
|