001: /* --------------
002: * DialDemo2.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.GradientPaint;
014: import java.awt.GridLayout;
015: import java.awt.Point;
016:
017: import javax.swing.JFrame;
018: import javax.swing.JLabel;
019: import javax.swing.JPanel;
020: import javax.swing.JSlider;
021: import javax.swing.event.ChangeEvent;
022: import javax.swing.event.ChangeListener;
023:
024: import org.jfree.chart.ChartPanel;
025: import org.jfree.chart.JFreeChart;
026: import org.jfree.data.general.DefaultValueDataset;
027: import org.jfree.experimental.chart.plot.dial.DialBackground;
028: import org.jfree.experimental.chart.plot.dial.DialCap;
029: import org.jfree.experimental.chart.plot.dial.DialPlot;
030: import org.jfree.experimental.chart.plot.dial.DialPointer;
031: import org.jfree.experimental.chart.plot.dial.DialTextAnnotation;
032: import org.jfree.experimental.chart.plot.dial.DialValueIndicator;
033: import org.jfree.experimental.chart.plot.dial.SimpleDialFrame;
034: import org.jfree.experimental.chart.plot.dial.StandardDialScale;
035: import org.jfree.ui.GradientPaintTransformType;
036: import org.jfree.ui.StandardGradientPaintTransformer;
037:
038: /**
039: * A sample application showing the use of a {@link DialPlot}.
040: */
041: public class DialDemo2 extends JFrame implements ChangeListener {
042:
043: /** The first dataset. */
044: DefaultValueDataset dataset1;
045:
046: /** The second dataset. */
047: DefaultValueDataset dataset2;
048:
049: /** A slider to update the first dataset value. */
050: JSlider slider1;
051:
052: /** A slider to update the second dataset value. */
053: JSlider slider2;
054:
055: /**
056: * Creates a new instance.
057: *
058: * @param title the frame title.
059: */
060: public DialDemo2(String title) {
061: super (title);
062:
063: this .dataset1 = new DefaultValueDataset(10.0);
064: this .dataset2 = new DefaultValueDataset(50.0);
065:
066: // get data for diagrams
067: DialPlot plot = new DialPlot();
068: plot.setView(0.0, 0.0, 1.0, 1.0);
069: plot.setDataset(0, this .dataset1);
070: plot.setDataset(1, this .dataset2);
071: SimpleDialFrame dialFrame = new SimpleDialFrame();
072: dialFrame.setBackgroundPaint(Color.lightGray);
073: dialFrame.setForegroundPaint(Color.darkGray);
074: plot.setDialFrame(dialFrame);
075:
076: GradientPaint gp = new GradientPaint(new Point(), new Color(
077: 255, 255, 255), new Point(), new Color(170, 170, 220));
078: DialBackground db = new DialBackground(gp);
079: db
080: .setGradientPaintTransformer(new StandardGradientPaintTransformer(
081: GradientPaintTransformType.VERTICAL));
082: plot.setBackground(db);
083:
084: DialTextAnnotation annotation1 = new DialTextAnnotation(
085: "Temperature");
086: annotation1.setFont(new Font("Dialog", Font.BOLD, 14));
087: annotation1.setRadius(0.7);
088:
089: plot.addLayer(annotation1);
090:
091: DialValueIndicator dvi = new DialValueIndicator(0, "c");
092: dvi.setFont(new Font("Dialog", Font.PLAIN, 10));
093: dvi.setOutlinePaint(Color.darkGray);
094: dvi.setRadius(0.60);
095: dvi.setAngle(-103.0);
096: plot.addLayer(dvi);
097:
098: DialValueIndicator dvi2 = new DialValueIndicator(1, "c");
099: dvi2.setFont(new Font("Dialog", Font.PLAIN, 10));
100: dvi2.setOutlinePaint(Color.red);
101: dvi2.setRadius(0.60);
102: dvi2.setAngle(-77.0);
103: plot.addLayer(dvi2);
104:
105: StandardDialScale scale = new StandardDialScale(-40, 60, -120,
106: -300);
107: scale.setTickRadius(0.88);
108: scale.setTickLabelOffset(0.15);
109: scale.setTickLabelFont(new Font("Dialog", Font.PLAIN, 14));
110: plot.addScale(0, scale);
111:
112: StandardDialScale scale2 = new StandardDialScale(0, 100, -120,
113: -300);
114: scale2.setTickRadius(0.50);
115: scale2.setTickLabelOffset(0.15);
116: scale2.setTickLabelFont(new Font("Dialog", Font.PLAIN, 10));
117: scale2.setMajorTickPaint(Color.red);
118: plot.addScale(1, scale2);
119: plot.mapDatasetToScale(1, 1);
120:
121: DialPointer needle2 = new DialPointer.Pin(1);
122: needle2.setRadius(0.55);
123: plot.addLayer(needle2);
124:
125: DialPointer needle = new DialPointer.Pointer(0);
126: plot.addLayer(needle);
127:
128: DialCap cap = new DialCap();
129: cap.setRadius(0.10);
130: plot.setCap(cap);
131:
132: JFreeChart chart1 = new JFreeChart(plot);
133: chart1.setTitle("Dial Demo 2");
134: ChartPanel cp1 = new ChartPanel(chart1);
135: cp1.setPreferredSize(new Dimension(400, 400));
136:
137: JPanel sliderPanel = new JPanel(new GridLayout(2, 2));
138: sliderPanel.add(new JLabel("Outer Needle:"));
139: sliderPanel.add(new JLabel("Inner Needle:"));
140: this .slider1 = new JSlider(-40, 60);
141: this .slider1.setMajorTickSpacing(20);
142: this .slider1.setPaintTicks(true);
143: this .slider1.setPaintLabels(true);
144: this .slider1.addChangeListener(this );
145: sliderPanel.add(this .slider1);
146: sliderPanel.add(this .slider1);
147: this .slider2 = new JSlider(0, 100);
148: this .slider2.setMajorTickSpacing(20);
149: this .slider2.setPaintTicks(true);
150: this .slider2.setPaintLabels(true);
151: this .slider2.addChangeListener(this );
152: sliderPanel.add(this .slider2);
153: JPanel content = new JPanel(new BorderLayout());
154: content.add(cp1);
155: content.add(sliderPanel, BorderLayout.SOUTH);
156: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
157: setContentPane(content);
158: }
159:
160: /**
161: * Handle a change in the slider by updating the dataset value. This
162: * automatically triggers a chart repaint.
163: *
164: * @param e the event.
165: */
166: public void stateChanged(ChangeEvent e) {
167: this .dataset1.setValue(new Integer(this .slider1.getValue()));
168: this .dataset2.setValue(new Integer(this .slider2.getValue()));
169: }
170:
171: /**
172: * Starting point for the demo application.
173: *
174: * @param args ignored.
175: */
176: public static void main(String[] args) {
177: DialDemo2 app = new DialDemo2("JFreeChart - Dial Demo 2");
178: app.pack();
179: app.setVisible(true);
180: }
181:
182: }
|