1 31 package org.objectweb.proactive.examples.cruisecontrol; 32 33 36 public class CarPanel extends javax.swing.JPanel { 37 38 static final int INACTIVE = 0; 40 static final int ACTIVE = 1; 41 42 double speed; 43 44 double distance = 0; 45 46 int state = INACTIVE; 47 48 javax.swing.JLabel Accelerate; 49 50 javax.swing.JProgressBar accBar; 51 52 javax.swing.JProgressBar roadBar; 53 54 javax.swing.JLabel speedLabel; 55 56 javax.swing.JLabel distanceLabel; 57 58 59 60 61 public CarPanel(final CruiseControlApplet parent, final Interface activeObject) { 62 63 setLayout(null); 64 setBorder(new javax.swing.border.LineBorder (java.awt.Color.black)); 65 66 speedLabel = new javax.swing.JLabel ("0"); 70 distanceLabel = new javax.swing.JLabel ("0"); 71 javax.swing.JLabel speedDisplay = new javax.swing.JLabel ("Speed"); 72 javax.swing.JLabel distanceDisplay = new javax.swing.JLabel ("Dist."); 73 74 75 javax.swing.JButton EngineOn = new javax.swing.JButton ("Engine On"); 77 javax.swing.JButton EngineOff = new javax.swing.JButton ("Engine Off"); 78 Accelerate = new javax.swing.JLabel ("Accelerate"); 79 javax.swing.JLabel Up = new javax.swing.JLabel ("Up"); 80 javax.swing.JLabel Down = new javax.swing.JLabel ("Down"); 81 javax.swing.JButton Brake = new javax.swing.JButton ("Brake"); 82 javax.swing.JButton Inc = new javax.swing.JButton ("+"); 83 javax.swing.JButton Dec = new javax.swing.JButton ("-"); 84 javax.swing.JButton UpAlpha = new javax.swing.JButton ("Up"); 85 javax.swing.JButton DownAlpha = new javax.swing.JButton ("Down"); 86 87 accBar = new javax.swing.JProgressBar (javax.swing.JProgressBar.VERTICAL, 0, 50); 88 accBar.setValue(0); 89 accBar.setStringPainted(true); 90 91 roadBar = new javax.swing.JProgressBar (javax.swing.JProgressBar.VERTICAL, -60, 60); 92 roadBar.setValue(0); 93 roadBar.setStringPainted(true); 94 95 EngineOn.setBounds(10, 310, 120, 30); 96 EngineOff.setBounds(140, 310, 130, 30); 97 98 Accelerate.setBounds(390, 310, 120, 30); 99 Up.setBounds(450, 10, 30, 30); 100 Down.setBounds(450, 120, 50, 30); 101 UpAlpha.setBounds(370, 20, 70, 30); 102 DownAlpha.setBounds(350, 110, 90, 30); 103 104 Brake.setBounds(280, 310, 80, 30); 105 Inc.setBounds(365, 345, 60, 30); 106 Dec.setBounds(430, 345, 60, 30); 107 accBar.setBounds(420, 200, 40, 80); 108 roadBar.setBounds(450, 40, 40, 80); 109 110 speedDisplay.setBounds(200, 230, 50, 30); 111 distanceDisplay.setBounds(200, 260, 50, 30); 112 speedLabel.setBounds(270, 230, 50, 30); 113 distanceLabel.setBounds(270, 260, 50, 30); 114 115 116 add(EngineOn); 118 add(EngineOff); 119 120 add(Accelerate); 121 add(Up); 122 add(Down); 123 124 add(UpAlpha); 125 add(DownAlpha); 126 127 add(Brake); 128 add(Inc); 129 add(Dec); 130 add(accBar); 131 add(roadBar); 132 133 add(speedDisplay); 134 add(distanceDisplay); 135 add(speedLabel); 136 add(distanceLabel); 137 138 EngineOn.addActionListener(new java.awt.event.ActionListener () { 139 140 public void actionPerformed(java.awt.event.ActionEvent evt) { 141 activeObject.engineOn(); 142 } 143 }); 144 EngineOff.addActionListener(new java.awt.event.ActionListener () { 145 146 public void actionPerformed(java.awt.event.ActionEvent evt) { 147 activeObject.engineOff(); 148 } 149 }); 150 Brake.addActionListener(new java.awt.event.ActionListener () { 151 152 public void actionPerformed(java.awt.event.ActionEvent evt) { 153 activeObject.brake(); 154 brake(); 155 parent.controlPaneOff(); 156 } 157 }); 158 Inc.addActionListener(new java.awt.event.ActionListener () { 159 160 public void actionPerformed(java.awt.event.ActionEvent evt) { 161 activeObject.accelerate(); 162 } 163 }); 164 Dec.addActionListener(new java.awt.event.ActionListener () { 165 166 public void actionPerformed(java.awt.event.ActionEvent evt) { 167 activeObject.decelerate(); 168 } 169 }); 170 UpAlpha.addActionListener(new java.awt.event.ActionListener () { 171 172 public void actionPerformed(java.awt.event.ActionEvent evt) { 173 activeObject.incAlpha(); 174 } 175 }); 176 DownAlpha.addActionListener(new java.awt.event.ActionListener () { 177 178 public void actionPerformed(java.awt.event.ActionEvent evt) { 179 activeObject.decAlpha(); 180 } 181 }); 182 } 183 184 185 188 189 public void engineOn() { 190 if (this.state == INACTIVE) { 191 this.state = ACTIVE; 192 repaint(); 193 } 194 } 195 196 197 198 public void engineOff() { 199 setSpeed(0); 200 setAcceleration(0); 201 this.state = INACTIVE; 202 repaint(); 203 204 } 205 206 207 208 public void controlOn() { 209 if (this.state == ACTIVE) { 210 } 211 } 212 213 214 215 public void controlOff() { 216 } 217 218 219 220 public void accelerate() { 221 } 222 223 224 225 public void brake() { 226 setAcceleration(0); 227 } 228 229 230 232 233 public void paintComponent(java.awt.Graphics g) { 234 super.paintComponent(g); 235 if (state == INACTIVE) { 236 g.setColor(java.awt.Color.black); 237 g.drawString("Engine Off", 40, 245); 238 g.setColor(java.awt.Color.red); 239 g.fillOval(110, 230, 20, 20); 240 } else { 241 g.setColor(java.awt.Color.black); 242 g.drawString("Engine On", 40, 245); 243 g.setColor(java.awt.Color.green); 244 g.fillOval(110, 230, 20, 20); 245 } 246 247 g.setColor(java.awt.Color.black); 248 g.drawOval(150, 40, 170, 170); 249 250 for (int i = 0; i <= 160; i += 10) { 251 drawMark(g, 150 + 85, 40 + 85, 85, i); 252 } 253 drawSpeed(g, 150 + 85, 40 + 85, 85); 254 255 } 256 257 258 261 private void drawSpeed(java.awt.Graphics g, int x, int y, int len) { 262 g.setColor(java.awt.Color.black); 263 double fangle = ((40 + (speed * 7) / 4) * Math.PI) / 180; 264 265 int mx = x - (int)((len - 30) * Math.sin(fangle)); 266 int my = y + (int)((len - 30) * Math.cos(fangle)); 267 268 g.drawLine(mx, my, x, y); 269 } 270 271 272 273 private void drawMark(java.awt.Graphics g, int x, int y, int len, int n) { 274 double flen = len; 275 double fangle = ((40 + n * 1.75) * Math.PI) / 180; 276 int mx = x - (int)(flen * Math.sin(fangle)); 277 int my = y + (int)(flen * Math.cos(fangle)); 278 int mx2 = x - (int)((flen - 5) * Math.sin(fangle)); 279 int my2 = y + (int)((flen - 5) * Math.cos(fangle)); 280 281 g.drawLine(mx, my, mx2, my2); 282 283 flen = flen + 12; 284 mx = x - 7 - (int)(flen * Math.sin(fangle)); 285 my = y + 7 + (int)(flen * Math.cos(fangle)); 286 g.drawString(String.valueOf(n), mx, my); 287 288 } 289 290 291 292 public void setSpeed(double m_speed) { 293 this.speed = m_speed; 294 this.speedLabel.setText(Integer.toString((int)speed)); 295 repaint(150, 40, 170, 170); 296 297 } 298 299 300 301 public void setDistance(double m_distance) { 302 this.distance = m_distance; 303 this.distanceLabel.setText(Integer.toString((int)distance)); 304 305 } 306 307 308 309 public void setAcceleration(double m_acc) { 310 accBar.setValue((int)m_acc); 311 } 312 313 314 315 public void setAlpha(double m_alpha) { 316 roadBar.setValue((int)(m_alpha * 100)); 317 } 318 } | Popular Tags |