1 39 package org.jfree.chart.demo; 40 41 import java.awt.BorderLayout ; 42 import java.awt.Color ; 43 import java.awt.Dimension ; 44 import java.awt.Font ; 45 import java.awt.GridLayout ; 46 import java.awt.Toolkit ; 47 import java.awt.event.ActionEvent ; 48 import java.text.DecimalFormat ; 49 50 import org.jfree.chart.ChartPanel; 51 import org.jfree.chart.JFreeChart; 52 import org.jfree.chart.plot.JThermometer; 53 import org.jfree.chart.plot.MeterPlot; 54 import org.jfree.data.DefaultValueDataset; 55 import javax.swing.*; 56 57 62 public class ThermometerDemo 63 extends JPanel { 64 65 66 protected static final String [] OPTIONS = { 67 "None", "Right", "Left", "Bulb"}; 68 69 protected static final String [] AXIS_OPTIONS = { 70 "None", "Right", "Left"}; 71 72 73 private DefaultValueDataset data = new DefaultValueDataset(20.0); 74 75 76 private MeterPlot meterplot = new MeterPlot(data); 77 78 79 private JFreeChart meterchart = new JFreeChart("Meter Chart", 80 JFreeChart.DEFAULT_TITLE_FONT, 81 meterplot, false); 82 83 84 private ChartPanel panelMeter = new ChartPanel(meterchart); 85 86 87 private JPanel jPanel1 = new JPanel(); 88 89 90 private JButton butUp3 = new JButton(); 91 92 93 private JButton butDown3 = new JButton(); 94 95 96 private JPanel jPanel2 = new JPanel(); 97 98 99 private BorderLayout borderLayout2 = new BorderLayout (); 100 101 102 private JPanel jPanel3 = new JPanel(); 103 104 105 private BorderLayout borderLayout3 = new BorderLayout (); 106 107 108 private JPanel jPanel4 = new JPanel(); 109 110 111 private JButton butDown2 = new JButton(); 112 113 114 private JButton butUp2 = new JButton(); 115 116 117 private JPanel jPanel5 = new JPanel(); 118 119 120 private GridLayout gridLayout1 = new GridLayout (); 121 122 123 private JPanel jPanel6 = new JPanel(); 124 125 126 private JButton butUp1 = new JButton(); 127 128 129 private JButton butDown1 = new JButton(); 130 131 132 private JThermometer thermo1 = new JThermometer(); 133 134 135 private JThermometer thermo2 = new JThermometer(); 136 137 138 private JThermometer thermo3 = new JThermometer(); 139 140 141 private JThermometer[] thermo = new JThermometer[3]; 142 143 144 private BorderLayout borderLayout1 = new BorderLayout (); 145 146 147 private JPanel jPanel7 = new JPanel(); 148 149 150 private JPanel jPanel8 = new JPanel(); 151 152 153 private JPanel jPanel9 = new JPanel(); 154 155 156 private GridLayout gridLayout2 = new GridLayout (); 157 158 159 private GridLayout gridLayout3 = new GridLayout (); 160 161 162 private GridLayout gridLayout4 = new GridLayout (); 163 164 165 private JComboBox pickShow0 = new JComboBox(OPTIONS); 166 167 168 private JComboBox pickShow1 = new JComboBox(OPTIONS); 169 170 171 private JComboBox pickShow2 = new JComboBox(OPTIONS); 172 173 174 private JComboBox pickAxis0 = new JComboBox(AXIS_OPTIONS); 175 176 private JComboBox pickAxis1 = new JComboBox(AXIS_OPTIONS); 177 178 private JComboBox pickAxis2 = new JComboBox(AXIS_OPTIONS); 179 180 181 private JComboBox[] pickShow = new JComboBox[3]; 182 183 184 private JComboBox[] pickAxis = new JComboBox[3]; 185 186 187 private JPanel jPanel10 = new JPanel(); 188 189 190 private BorderLayout borderLayout4 = new BorderLayout (); 191 192 193 private JPanel jPanel11 = new JPanel(); 194 195 196 private JButton butDown4 = new JButton(); 197 198 199 private JButton butUp4 = new JButton(); 200 201 204 public ThermometerDemo() { 205 try { 206 jbInit(); 207 } 208 catch (Exception ex) { 209 ex.printStackTrace(); 210 } 211 } 212 213 224 229 void jbInit() throws Exception { 230 231 thermo[0] = thermo1; 233 thermo[1] = thermo2; 234 thermo[2] = thermo3; 235 236 thermo[0].setValue(0.0); 237 thermo[1].setValue(0.2); 238 thermo[2].setValue(0.3); 239 240 thermo[0].setBackground(Color.white); 241 thermo[2].setBackground(Color.white); 242 243 thermo[0].setOutlinePaint(null); 244 thermo[1].setOutlinePaint(null); 245 thermo[2].setOutlinePaint(null); 246 247 thermo[0].setUnits(0); 248 thermo[1].setUnits(1); 249 thermo[2].setUnits(2); 250 251 thermo[0].setShowValueLines(true); 253 thermo[0].setFollowDataInSubranges(true); 254 thermo[1].setValueLocation(1); 255 256 thermo[1].setForeground(Color.blue); 257 thermo[2].setForeground(Color.pink); 258 259 thermo[0].setRange( -10.0, 40.0); 260 thermo[0].setSubrangeInfo(0, -50.0, 20.0, -10.0, 22.0); 261 thermo[0].setSubrangeInfo(1, 20.0, 24.0, 18.0, 26.0); 262 thermo[0].setSubrangeInfo(2, 24.0, 100.0, 22.0, 40.0); 263 264 thermo[0].addSubtitle("Sea Water Temp"); 265 thermo[1].addSubtitle("Air Temp", new Font ("SansSerif", Font.PLAIN, 16)); 266 thermo[2].addSubtitle("Ship Temp", 267 new Font ("SansSerif", Font.ITALIC + Font.BOLD, 20)); 268 269 thermo[1].setValueFormat(new DecimalFormat ("#0.0")); 270 thermo[2].setValueFormat(new DecimalFormat ("#0.00")); 271 272 pickShow[0] = pickShow0; 273 pickShow[1] = pickShow1; 274 pickShow[2] = pickShow2; 275 276 pickAxis[0] = pickAxis0; 277 pickAxis[1] = pickAxis1; 278 pickAxis[2] = pickAxis2; 279 280 pickAxis[0].setSelectedIndex(2); 281 pickAxis[1].setSelectedIndex(2); 282 pickAxis[2].setSelectedIndex(2); 283 284 setLayout(gridLayout1); 285 butDown3.setText("<"); 286 butDown3.addActionListener(new java.awt.event.ActionListener () { 287 public void actionPerformed(ActionEvent e) { 288 setValue(2, -1); 289 } 290 }); 291 butUp3.setText(">"); 292 butUp3.addActionListener(new java.awt.event.ActionListener () { 293 public void actionPerformed(ActionEvent e) { 294 setValue(2, 1); 295 } 296 }); 297 jPanel1.setLayout(borderLayout2); 298 jPanel3.setLayout(borderLayout3); 299 butDown2.setText("<"); 300 butDown2.addActionListener(new java.awt.event.ActionListener () { 301 public void actionPerformed(ActionEvent e) { 302 setValue(1, -1); 303 } 304 }); 305 butUp2.setText(">"); 306 butUp2.addActionListener(new java.awt.event.ActionListener () { 307 public void actionPerformed(ActionEvent e) { 308 setValue(1, 1); 309 } 310 }); 311 butUp1.setText(">"); 312 butUp1.addActionListener(new java.awt.event.ActionListener () { 313 public void actionPerformed(ActionEvent e) { 314 setValue(0, 1); 315 } 316 }); 317 butDown1.setText("<"); 318 butDown1.addActionListener(new java.awt.event.ActionListener () { 319 public void actionPerformed(ActionEvent e) { 320 setValue(0, -1); 321 } 322 }); 323 jPanel5.setLayout(borderLayout1); 324 pickShow0.addActionListener(new java.awt.event.ActionListener () { 325 public void actionPerformed(ActionEvent e) { 326 setShowValue(0); 327 } 328 }); 329 pickShow1.addActionListener(new java.awt.event.ActionListener () { 330 public void actionPerformed(ActionEvent e) { 331 setShowValue(1); 332 } 333 }); 334 pickShow2.addActionListener(new java.awt.event.ActionListener () { 335 public void actionPerformed(ActionEvent e) { 336 setShowValue(2); 337 } 338 }); 339 340 pickAxis0.addActionListener(new java.awt.event.ActionListener () { 341 public void actionPerformed(ActionEvent e) { 342 setShowAxis(0); 343 } 344 }); 345 pickAxis1.addActionListener(new java.awt.event.ActionListener () { 346 public void actionPerformed(ActionEvent e) { 347 setShowAxis(1); 348 } 349 }); 350 pickAxis2.addActionListener(new java.awt.event.ActionListener () { 351 public void actionPerformed(ActionEvent e) { 352 setShowAxis(2); 353 } 354 }); 355 356 jPanel9.setLayout(gridLayout2); 357 gridLayout2.setColumns(1); 358 jPanel8.setLayout(gridLayout3); 359 jPanel7.setLayout(gridLayout4); 360 jPanel5.setBorder(BorderFactory.createEtchedBorder()); 361 jPanel3.setBorder(BorderFactory.createEtchedBorder()); 362 jPanel1.setBorder(BorderFactory.createEtchedBorder()); 363 jPanel6.setBackground(Color.white); 364 jPanel2.setBackground(Color.white); 365 jPanel9.setBackground(Color.white); 366 jPanel10.setLayout(borderLayout4); 367 butDown4.setText("<"); 368 butDown4.addActionListener(new java.awt.event.ActionListener () { 369 public void actionPerformed(ActionEvent e) { 370 setMeterValue( -1.1); 371 } 372 }); 373 butUp4.setText(">"); 374 butUp4.addActionListener(new java.awt.event.ActionListener () { 375 public void actionPerformed(ActionEvent e) { 376 setMeterValue(1.1); 377 } 378 }); 379 jPanel1.add(thermo3, BorderLayout.CENTER); 380 jPanel1.add(jPanel2, BorderLayout.SOUTH); 381 jPanel2.add(butDown3, null); 382 jPanel2.add(butUp3, null); 383 jPanel1.add(jPanel9, BorderLayout.NORTH); 384 jPanel9.add(pickShow2, null); 385 jPanel9.add(pickAxis2, null); 386 add(jPanel10, null); 387 jPanel10.add(jPanel11, BorderLayout.SOUTH); 388 jPanel11.add(butDown4, null); 389 jPanel11.add(butUp4, null); 390 jPanel4.add(butDown2, null); 391 jPanel4.add(butUp2, null); 392 jPanel3.add(jPanel8, BorderLayout.NORTH); 393 jPanel8.add(pickShow1, null); 394 jPanel8.add(pickAxis1, null); 395 jPanel3.add(thermo2, BorderLayout.CENTER); 396 jPanel3.add(jPanel4, BorderLayout.SOUTH); 397 add(jPanel5, null); 398 jPanel5.add(thermo1, BorderLayout.CENTER); 399 jPanel5.add(jPanel6, BorderLayout.SOUTH); 400 jPanel6.add(butDown1, null); 401 jPanel6.add(butUp1, null); 402 jPanel5.add(jPanel7, BorderLayout.NORTH); 403 jPanel7.add(pickShow0, null); 404 jPanel7.add(pickAxis0, null); 405 add(jPanel3, null); 406 add(jPanel1, null); 407 jPanel10.add(panelMeter, BorderLayout.CENTER); 408 } 409 410 415 public static void main(String [] args) { 416 417 final ThermometerDemo panel = new ThermometerDemo(); 418 419 JFrame frame = new JFrame(); 420 frame.getContentPane().setLayout(new BorderLayout (5, 5)); 421 frame.setDefaultCloseOperation(3); 422 frame.setTitle("Thermometer Test"); 423 frame.getContentPane().add(panel, BorderLayout.CENTER); 424 frame.setSize(700, 400); 425 Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); 426 frame.setLocation( (d.width - frame.getSize().width) / 2, 427 (d.height - frame.getSize().height) / 2); 428 frame.setVisible(true); 429 430 } 431 432 438 private void setValue(int thermometer, double value) { 439 if ( (thermometer >= 0) && (thermometer < 3)) { 440 try { 441 thermo[thermometer].setValue(thermo[thermometer].getValue().doubleValue() + 442 value); 443 } 444 catch (Exception ex) { 445 ex.printStackTrace(); 446 } 447 } 448 } 449 450 455 private void setMeterValue(double value) { 456 try { 457 double newValue = value; 458 if (data.getValue() != null) { 459 newValue += data.getValue().doubleValue(); 460 } 461 data.setValue(new Double (newValue)); 462 } 463 catch (Exception ex) { 464 System.err.println(ex.getMessage()); 465 } 466 } 467 468 473 private void setShowValue(int thermometer) { 474 if ( (thermometer >= 0) && (thermometer < 3)) { 475 thermo[thermometer].setValueLocation(pickShow[thermometer]. 476 getSelectedIndex()); 477 } 478 } 479 480 485 private void setShowAxis(int thermometer) { 486 if ( (thermometer >= 0) && (thermometer < 3)) { 487 thermo[thermometer].setShowAxisLocation(pickAxis[thermometer]. 488 getSelectedIndex()); 489 } 490 } 491 492 } 493 | Popular Tags |