1 6 7 package org.jfree.experimental.chart.demo; 8 9 import java.awt.BorderLayout ; 10 import java.awt.Color ; 11 import java.awt.Dimension ; 12 import java.awt.Font ; 13 import java.awt.GradientPaint ; 14 import java.awt.GridLayout ; 15 import java.awt.Point ; 16 17 import javax.swing.JFrame ; 18 import javax.swing.JLabel ; 19 import javax.swing.JPanel ; 20 import javax.swing.JSlider ; 21 import javax.swing.event.ChangeEvent ; 22 import javax.swing.event.ChangeListener ; 23 24 import org.jfree.chart.ChartPanel; 25 import org.jfree.chart.JFreeChart; 26 import org.jfree.data.general.DefaultValueDataset; 27 import org.jfree.experimental.chart.plot.dial.DialBackground; 28 import org.jfree.experimental.chart.plot.dial.DialCap; 29 import org.jfree.experimental.chart.plot.dial.DialPlot; 30 import org.jfree.experimental.chart.plot.dial.DialPointer; 31 import org.jfree.experimental.chart.plot.dial.DialTextAnnotation; 32 import org.jfree.experimental.chart.plot.dial.DialValueIndicator; 33 import org.jfree.experimental.chart.plot.dial.SimpleDialFrame; 34 import org.jfree.experimental.chart.plot.dial.StandardDialScale; 35 import org.jfree.ui.GradientPaintTransformType; 36 import org.jfree.ui.StandardGradientPaintTransformer; 37 38 41 public class DialDemo2 extends JFrame implements ChangeListener { 42 43 44 DefaultValueDataset dataset1; 45 46 47 DefaultValueDataset dataset2; 48 49 50 JSlider slider1; 51 52 53 JSlider slider2; 54 55 60 public DialDemo2(String title) { 61 super(title); 62 63 dataset1 = new DefaultValueDataset(10.0); 64 dataset2 = new DefaultValueDataset(50.0); 65 66 DialPlot plot = new DialPlot(); 68 plot.setView(0.0, 0.0, 1.0, 1.0); 69 plot.setDataset(0, dataset1); 70 plot.setDataset(1, dataset2); 71 SimpleDialFrame dialFrame = new SimpleDialFrame(); 72 dialFrame.setBackgroundPaint(Color.lightGray); 73 dialFrame.setForegroundPaint(Color.darkGray); 74 plot.setDialFrame(dialFrame); 75 76 GradientPaint gp = new GradientPaint (new Point (), 77 new Color (255, 255, 255), new Point (), 78 new Color (170, 170, 220)); 79 DialBackground db = new DialBackground(gp); 80 db.setGradientPaintTransformer(new StandardGradientPaintTransformer( 81 GradientPaintTransformType.VERTICAL)); 82 plot.setBackground(db); 83 84 DialTextAnnotation annotation1 = new DialTextAnnotation("Temperature"); 85 annotation1.setFont(new Font ("Dialog", Font.BOLD, 14)); 86 annotation1.setRadius(0.7); 87 88 plot.addLayer(annotation1); 89 90 DialValueIndicator dvi = new DialValueIndicator(0, "c"); 91 dvi.setFont(new Font ("Dialog", Font.PLAIN, 10)); 92 dvi.setOutlinePaint(Color.darkGray); 93 dvi.setRadius(0.60); 94 dvi.setAngle(-103.0); 95 plot.addLayer(dvi); 96 97 DialValueIndicator dvi2 = new DialValueIndicator(1, "c"); 98 dvi2.setFont(new Font ("Dialog", Font.PLAIN, 10)); 99 dvi2.setOutlinePaint(Color.red); 100 dvi2.setRadius(0.60); 101 dvi2.setAngle(-77.0); 102 plot.addLayer(dvi2); 103 104 StandardDialScale scale = new StandardDialScale(-40, 60, -120, -300); 105 scale.setTickRadius(0.88); 106 scale.setTickLabelOffset(0.15); 107 scale.setTickLabelFont(new Font ("Dialog", Font.PLAIN, 14)); 108 plot.addScale(0, scale); 109 110 StandardDialScale scale2 = new StandardDialScale(0, 100, -120, -300); 111 scale2.setTickRadius(0.50); 112 scale2.setTickLabelOffset(0.15); 113 scale2.setTickLabelFont(new Font ("Dialog", Font.PLAIN, 10)); 114 scale2.setMajorTickPaint(Color.red); 115 plot.addScale(1, scale2); 116 plot.mapDatasetToScale(1, 1); 117 118 DialPointer needle2 = new DialPointer.Pin(1); 119 needle2.setRadius(0.55); 120 plot.addLayer(needle2); 121 122 DialPointer needle = new DialPointer.Pointer(0); 123 plot.addLayer(needle); 124 125 DialCap cap = new DialCap(); 126 cap.setRadius(0.10); 127 plot.setCap(cap); 128 129 JFreeChart chart1 = new JFreeChart(plot); 130 chart1.setTitle("Dial Demo 2"); 131 ChartPanel cp1 = new ChartPanel(chart1); 132 cp1.setPreferredSize(new Dimension (400, 400)); 133 134 JPanel sliderPanel = new JPanel (new GridLayout (2, 2)); 135 sliderPanel.add(new JLabel ("Outer Needle:")); 136 sliderPanel.add(new JLabel ("Inner Needle:")); 137 slider1 = new JSlider (-40, 60); 138 slider1.setMajorTickSpacing(20); 139 slider1.setPaintTicks(true); 140 slider1.setPaintLabels(true); 141 slider1.addChangeListener(this); 142 sliderPanel.add(slider1); 143 sliderPanel.add(slider1); 144 slider2 = new JSlider (0, 100); 145 slider2.setMajorTickSpacing(20); 146 slider2.setPaintTicks(true); 147 slider2.setPaintLabels(true); 148 slider2.addChangeListener(this); 149 sliderPanel.add(slider2); 150 JPanel content = new JPanel (new BorderLayout ()); 151 content.add(cp1); 152 content.add(sliderPanel, BorderLayout.SOUTH); 153 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 154 setContentPane(content); 155 } 156 157 163 public void stateChanged(ChangeEvent e) { 164 this.dataset1.setValue(new Integer (slider1.getValue())); 165 this.dataset2.setValue(new Integer (slider2.getValue())); 166 } 167 168 173 public static void main(String [] args) { 174 DialDemo2 app = new DialDemo2("JFreeChart - Dial Demo 2"); 175 app.pack(); 176 app.setVisible(true); 177 } 178 179 } 180 | Popular Tags |