1 31 package org.objectweb.proactive.examples.cruisecontrol; 32 33 import javax.swing.JButton ; 34 import javax.swing.JPanel ; 35 36 41 public class CruiseControlPanel extends JPanel { 42 43 45 46 final static int ACTIVE = 1; 47 final static int INACTIVE = 0; 48 49 JButton CruiseOff; 50 51 JButton CruiseOn; 52 53 JButton Inc1; 54 55 JButton Dec1; 56 57 int desiredSpeed = 0; 58 59 java.awt.Font big = new java.awt.Font ("SansSerif", java.awt.Font.BOLD, 18); 60 61 java.awt.Font small = new java.awt.Font ("SansSerif", java.awt.Font.PLAIN, 14); 62 63 int state = INACTIVE; 64 65 66 67 public CruiseControlPanel(final CruiseControlApplet parent, final Interface activeObject) { 68 setLayout(null); 69 71 CruiseOff = new JButton ("Control Off"); 72 CruiseOn = new JButton ("Control On"); 73 Inc1 = new JButton ("++"); 74 Dec1 = new JButton ("--"); 75 76 CruiseOn.setBounds(20, 310, 130, 30); 77 CruiseOff.setBounds(160, 310, 130, 30); 78 Inc1.setBounds(90, 345, 60, 30); 79 Dec1.setBounds(160, 345, 60, 30); 80 81 add(CruiseOn); 82 add(CruiseOff); 83 add(Inc1); 84 add(Dec1); 85 86 CruiseOn.addActionListener(new java.awt.event.ActionListener () { 87 88 public void actionPerformed(java.awt.event.ActionEvent evt) { 89 parent.controlOn(); 90 } 91 }); 92 CruiseOff.addActionListener(new java.awt.event.ActionListener () { 93 94 public void actionPerformed(java.awt.event.ActionEvent evt) { 95 parent.controlOff(); 96 } 97 }); 98 Inc1.addActionListener(new java.awt.event.ActionListener () { 99 100 public void actionPerformed(java.awt.event.ActionEvent evt) { 101 activeObject.accelerateCruise(); 102 } 103 }); 104 Dec1.addActionListener(new java.awt.event.ActionListener () { 105 106 public void actionPerformed(java.awt.event.ActionEvent evt) { 107 activeObject.decelerateCruise(); 108 } 109 }); 110 } 111 112 114 115 public void paintComponent(java.awt.Graphics g) { 116 super.paintComponent(g); 117 118 g.setColor(java.awt.Color.black); 119 g.setFont(big); 120 g.drawString("Control", 100, 40); 121 g.setColor(java.awt.Color.black); 122 123 g.setFont(small); 124 g.drawString("Desired Speed ", 100, 120); 125 g.drawRect(110, 140, 70, 20); 126 g.drawString(String.valueOf(desiredSpeed), 120, 155); 128 129 131 if (state == INACTIVE) { 132 g.setColor(java.awt.Color.black); 133 g.drawString("Controller Off", 80, 230); 134 g.setColor(java.awt.Color.red); 135 g.fillOval(180, 210, 30, 30); 136 } else { 137 g.setColor(java.awt.Color.black); 138 g.drawString("Controller On", 80, 230); 139 g.setColor(java.awt.Color.green); 140 g.fillOval(180, 210, 30, 30); 141 } 142 } 143 144 146 147 public void controlOn() { 148 if (this.state == INACTIVE) { 149 this.state = ACTIVE; 150 repaint(); 151 } 152 } 153 154 155 156 public void controlOff() { 157 if (this.state == ACTIVE) { 158 this.state = INACTIVE; 159 repaint(); 160 } 161 } 162 163 164 165 public void setDesiredSpeed(double m_speed) { 166 desiredSpeed = (int)m_speed; 167 168 } 169 } | Popular Tags |