1 39 40 package org.jfree.chart.demo; 41 42 import java.awt.BorderLayout ; 43 import java.awt.Color ; 44 import java.awt.Dimension ; 45 import java.awt.GridBagConstraints ; 46 import java.awt.GridBagLayout ; 47 import java.awt.GridLayout ; 48 import java.awt.Insets ; 49 import java.awt.Toolkit ; 50 import java.awt.event.ActionEvent ; 51 import java.beans.PropertyChangeEvent ; 52 import java.beans.PropertyChangeListener ; 53 54 import javax.swing.JCheckBox ; 55 import javax.swing.JComboBox ; 56 import javax.swing.JFrame ; 57 import javax.swing.JPanel ; 58 import javax.swing.SwingConstants ; 59 import javax.swing.border.TitledBorder ; 60 61 import org.jfree.chart.ChartPanel; 62 import org.jfree.chart.JFreeChart; 63 import org.jfree.chart.plot.CompassPlot; 64 import org.jfree.data.DefaultValueDataset; 65 import org.jfree.ui.Spinner; 66 67 72 public class CompassDemo extends JPanel { 73 74 75 public static final String [] NEEDLE_TYPES 76 = {"Arrow", "Line", "Long", "Pin", "Plum", "Pointer", "Ship", "Wind", "Arrow"}; 77 78 79 private DefaultValueDataset compassData 80 = new DefaultValueDataset(new Double (0.0)); 81 82 83 private DefaultValueDataset shipData 84 = new DefaultValueDataset(new Double (0.0)); 85 86 87 private CompassPlot compassPlot = new CompassPlot(compassData); 88 89 90 private JFreeChart compassChart = new JFreeChart("Compass Test", 91 JFreeChart.DEFAULT_TITLE_FONT, 92 compassPlot, false); 93 94 95 private ChartPanel panelCompass = new ChartPanel(compassChart); 96 97 98 private GridLayout gridLayout1 = new GridLayout (); 99 100 101 private JPanel panelCompassHolder = new JPanel (); 102 103 104 private BorderLayout borderLayout = new BorderLayout (); 105 106 107 private JPanel jPanel12 = new JPanel (); 108 109 110 private JCheckBox windNullCheckBox = new JCheckBox (); 111 112 113 private JCheckBox shipNullCheckBox = new JCheckBox (); 114 115 120 121 private Spinner spinWind = new Spinner(270); 122 123 124 private Spinner spinShip = new Spinner(45); 125 126 127 private JPanel jPanel1 = new JPanel (); 128 129 130 private JComboBox pick1Pointer = new JComboBox (NEEDLE_TYPES); 131 132 133 private JPanel jPanel2 = new JPanel (); 134 135 136 private JComboBox pick2Pointer = new JComboBox (NEEDLE_TYPES); 137 138 139 private TitledBorder titledBorder1; 140 141 142 private TitledBorder titledBorder2; 143 144 145 private GridBagLayout gridBagLayout1 = new GridBagLayout (); 146 147 148 private GridBagLayout gridBagLayout2 = new GridBagLayout (); 149 150 151 private TitledBorder titledBorder3; 152 153 154 private GridLayout gridLayout2 = new GridLayout (); 155 156 159 public CompassDemo() { 160 try { 161 compassPlot.addData(shipData); 162 compassPlot.setSeriesNeedle(0, 7); 163 compassPlot.setSeriesNeedle(1, 5); 164 compassPlot.setSeriesPaint(0, Color.blue); 165 compassPlot.setSeriesOutlinePaint(0, Color.blue); 166 compassPlot.setSeriesPaint(1, Color.red); 167 compassPlot.setSeriesOutlinePaint(1, Color.red); 168 pick1Pointer.setSelectedIndex(7); 169 pick2Pointer.setSelectedIndex(5); 170 jbInit(); 171 } 172 catch (Exception ex) { 173 ex.printStackTrace(); 174 } 175 } 176 177 182 void jbInit() throws Exception { 183 titledBorder1 = new TitledBorder (""); 184 titledBorder2 = new TitledBorder (""); 185 titledBorder3 = new TitledBorder (""); 186 setLayout(gridLayout1); 187 panelCompassHolder.setLayout(borderLayout); 188 windNullCheckBox.setHorizontalTextPosition(SwingConstants.LEADING); 189 windNullCheckBox.setText("Null"); 190 windNullCheckBox.addActionListener(new java.awt.event.ActionListener () { 191 public void actionPerformed(ActionEvent e) { 192 checkWindNullActionPerformed(e); 193 } 194 }); 195 shipNullCheckBox.setHorizontalTextPosition(SwingConstants.LEFT); 196 shipNullCheckBox.setText("Null"); 197 shipNullCheckBox.addActionListener(new java.awt.event.ActionListener () { 198 public void actionPerformed(ActionEvent e) { 199 checkShipNullActionPerformed(e); 200 } 201 }); 202 203 spinShip.addPropertyChangeListener(new PropertyChangeListener () { 204 public void propertyChange(PropertyChangeEvent evt) { 205 Spinner spinner = (Spinner) evt.getSource(); 206 shipData.setValue(new Double (spinner.getValue())); 208 } 209 }); 210 211 spinWind.addPropertyChangeListener(new PropertyChangeListener () { 212 public void propertyChange(PropertyChangeEvent evt) { 213 Spinner spinner = (Spinner) evt.getSource(); 214 compassData.setValue(new Double (spinner.getValue())); 216 } 217 }); 218 jPanel12.setLayout(gridLayout2); 219 jPanel2.setBorder(titledBorder1); 220 jPanel2.setLayout(gridBagLayout2); 221 jPanel1.setBorder(titledBorder2); 222 jPanel1.setLayout(gridBagLayout1); 223 titledBorder1.setTitle("Second Pointer"); 224 titledBorder2.setTitle("First Pointer"); 225 titledBorder3.setTitle("Plot Options"); 226 pick2Pointer.addActionListener(new java.awt.event.ActionListener () { 227 public void actionPerformed(ActionEvent e) { 228 pick2PointerActionPerformed(e); 229 } 230 }); 231 pick1Pointer.addActionListener(new java.awt.event.ActionListener () { 232 public void actionPerformed(ActionEvent e) { 233 pick1PointerActionPerformed(e); 234 } 235 }); 236 add(panelCompassHolder, null); 237 panelCompassHolder.add(jPanel12, BorderLayout.SOUTH); 238 jPanel12.add(jPanel1, null); 239 240 jPanel1.add(pick1Pointer, 241 new GridBagConstraints (0, 0, 1, 1, 1.0, 0.0, 242 GridBagConstraints.CENTER, 243 GridBagConstraints.HORIZONTAL, 244 new Insets (0, 0, 0, 0), 245 0, 0)); 246 247 jPanel1.add(windNullCheckBox, 248 new GridBagConstraints (1, 0, 1, 1, 0.0, 0.0, 249 GridBagConstraints.CENTER, 250 GridBagConstraints.NONE, 251 new Insets (0, 0, 0, 0), 252 0, 0)); 253 254 jPanel1.add(spinWind, 255 new GridBagConstraints (2, 0, 1, 1, 1.0, 0.0, 256 GridBagConstraints.CENTER, 257 GridBagConstraints.BOTH, 258 new Insets (0, 0, 0, 0), 259 0, 0)); 260 261 jPanel12.add(jPanel2, null); 262 263 jPanel2.add(pick2Pointer, 264 new GridBagConstraints (0, 0, 1, 1, 1.0, 0.0, 265 GridBagConstraints.CENTER, 266 GridBagConstraints.HORIZONTAL, 267 new Insets (0, 0, 0, 0), 268 0, 0)); 269 270 jPanel2.add(shipNullCheckBox, 271 new GridBagConstraints (1, 0, 1, 1, 0.0, 0.0, 272 GridBagConstraints.CENTER, 273 GridBagConstraints.NONE, 274 new Insets (0, 0, 0, 0), 275 0, 0)); 276 277 jPanel2.add(spinShip, 278 new GridBagConstraints (2, 0, 1, 1, 1.0, 0.0, 279 GridBagConstraints.CENTER, 280 GridBagConstraints.BOTH, 281 new Insets (0, 0, 0, 0), 282 0, 0)); 283 284 panelCompassHolder.add(panelCompass, BorderLayout.CENTER); 285 286 } 287 288 293 public static void main(String [] args) { 294 295 final CompassDemo panel = new CompassDemo(); 296 297 JFrame frame = new JFrame (); 298 frame.getContentPane().setLayout(new BorderLayout (5, 5)); 299 frame.setDefaultCloseOperation(3); 300 frame.setTitle("Compass Demo"); 301 frame.getContentPane().add(panel, BorderLayout.CENTER); 302 frame.setSize(700, 400); 303 Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); 304 frame.setLocation((d.width - frame.getSize().width) / 2, 305 (d.height - frame.getSize().height) / 2); 306 frame.setVisible(true); 307 } 308 309 320 325 public void adjustData(double value) { 326 327 Number val = compassData.getValue(); 328 double newVal = value; 329 330 if (val != null) { 331 newVal += val.doubleValue(); 332 } 333 334 if (newVal > 360) { 335 newVal = 0; 336 } 337 338 if (newVal < 0) { 339 newVal = 360; 340 } 341 342 compassData.setValue(new Double (newVal)); 343 344 } 345 346 351 void checkWindNullActionPerformed(ActionEvent e) { 352 353 if (windNullCheckBox.isSelected()) { 354 compassData.setValue(null); 355 spinWind.setEnabled(false); 356 } 357 else { 358 compassData.setValue(new Double (spinWind.getValue())); 360 spinWind.setEnabled(true); 361 } 362 } 363 364 369 void checkShipNullActionPerformed(ActionEvent e) { 370 371 if (shipNullCheckBox.isSelected()) { 372 shipData.setValue(null); 373 spinShip.setEnabled(false); 374 } 375 else { 376 shipData.setValue(new Double (spinShip.getValue())); 378 spinShip.setEnabled(true); 379 } 380 } 381 382 387 void pick2PointerActionPerformed(ActionEvent e) { 388 compassPlot.setSeriesNeedle(1, pick2Pointer.getSelectedIndex()); 389 compassPlot.setSeriesPaint(1, Color.red); 390 compassPlot.setSeriesOutlinePaint(1, Color.red); 391 } 392 393 398 void pick1PointerActionPerformed(ActionEvent e) { 399 compassPlot.setSeriesNeedle(0, pick1Pointer.getSelectedIndex()); 400 compassPlot.setSeriesPaint(0, Color.blue); 401 compassPlot.setSeriesOutlinePaint(0, Color.blue); 402 } 403 404 } 405 | Popular Tags |