1 31 package org.objectweb.proactive.examples.cruisecontrol; 32 33 41 public class Interface { 42 43 45 48 final static int ACTIVE = 1; 49 50 final static int INACTIVE = 0; 51 54 final static int CRUISING = 2; 55 56 int state = INACTIVE; 57 58 double desiredSpeed = 0; 59 60 double acc = 0; 61 62 double brake = 0; 63 64 double alpha = 0; 65 66 CarModel activeSpeed = null; 67 68 CruiseControl activeCruise = null; 69 70 73 74 CruiseControlApplet applet; 75 76 77 78 public Interface() { 79 80 } 81 82 83 84 public Interface(CruiseControlApplet m_applet) { 85 this.applet = m_applet; 86 87 } 88 89 90 91 public void initialize() { 92 Object [] arg; 93 arg = new Object [1]; 94 arg[0] = org.objectweb.proactive.ProActive.getStubOnThis(); 95 96 try { 98 activeSpeed = (CarModel)org.objectweb.proactive.ProActive.newActive(CarModel.class.getName(), arg); 99 100 } catch (Exception e) { 101 e.printStackTrace(); 102 System.exit(0); 103 } 104 105 arg[0] = org.objectweb.proactive.ProActive.getStubOnThis(); 106 107 try { 108 109 activeCruise = (CruiseControl)org.objectweb.proactive.ProActive.newActive(CruiseControl.class.getName(), arg); 110 } catch (Exception e) { 111 e.printStackTrace(); 112 System.exit(0); 113 } 114 115 116 127 128 } 129 130 131 133 public void displayMessage(String msg) { 134 applet.receiveMessage(msg); 135 } 136 137 138 139 public void engineOn() { 140 if (this.state == INACTIVE) { 143 this.state = ACTIVE; 144 activeSpeed.engineOn(); 146 applet.engineOn(); 147 } 148 } 149 150 151 152 public void engineOff() { 153 activeSpeed.engineOff(); 154 activeCruise.engineOff(); 155 156 this.state = INACTIVE; 157 this.acc = 0; 158 this.desiredSpeed = 0; 159 setAcceleration(0); 160 applet.engineOff(); 161 162 } 164 165 166 170 public void controlOn() { 171 if (this.state == ACTIVE) { 172 this.state = CRUISING; 173 desiredSpeed = getSpeed().doubleValue(); 174 activeCruise.controlOn(desiredSpeed, this.acc); 178 } 181 182 } 183 184 185 188 public void controlOff() { 189 if (this.state == CRUISING) { 190 this.state = ACTIVE; 191 acc = 0; 192 setAcceleration(0); 193 194 activeCruise.controlOff(); 195 } 196 } 197 198 199 200 204 public void deactiveControl() { 205 if (this.state == CRUISING) { 206 this.state = ACTIVE; 207 acc = 0; 208 activeSpeed.brake(); 209 applet.controlPaneOff(); 210 } 211 } 212 213 214 219 public void accelerate() { 220 221 if (this.state == ACTIVE) { 222 if (this.acc < 50) { 223 this.acc += 1; 224 activeSpeed.setAcceleration(this.acc); 226 applet.setAcceleration(this.acc); 227 228 } 229 } 230 231 } 232 233 234 238 public void decelerate() { 239 if (this.state == ACTIVE) { 240 if (this.acc > 0) { 241 this.acc -= 1; 242 activeSpeed.setAcceleration(this.acc); 243 applet.setAcceleration(this.acc); 244 } 245 246 } 247 248 } 249 250 251 252 257 public void accelerateCruise() { 258 if (this.state == CRUISING) { 259 desiredSpeed += 1; 260 activeCruise.setDesiredSpeed(desiredSpeed); 261 applet.setDesiredSpeed(desiredSpeed); 262 } 264 265 } 266 267 268 272 public void decelerateCruise() { 273 if (this.state == CRUISING) { 274 desiredSpeed -= 1; 275 activeCruise.setDesiredSpeed(desiredSpeed); 276 applet.setDesiredSpeed(desiredSpeed); 277 } 279 280 } 281 282 286 public void incAlpha() { 287 if ((this.state == ACTIVE) || (this.state == CRUISING)) { 290 if (alpha < 0.6) 292 alpha += 0.09; 293 else 294 alpha = 0.6; 295 setAlpha(alpha); 296 } 298 } 299 300 301 304 public void decAlpha() { 305 if ((this.state == ACTIVE) || (this.state == CRUISING)) { 306 if (alpha > -0.6) 307 alpha -= 0.09; 308 else 309 alpha = -0.6; 310 setAlpha(alpha); 311 } 313 } 314 315 316 317 319 324 public void brake() { 325 if ((this.state == ACTIVE) || (this.state == CRUISING)) { 327 activeCruise.controlOff(); 328 activeSpeed.brake(); 329 applet.brake(); 330 acc = 0; 331 this.state = ACTIVE; 332 } 333 334 } 335 336 337 338 public void setSpeed(double speed) { 339 applet.setSpeed(speed); 340 } 341 342 343 344 public void setDistance(double distance) { 345 applet.setDistance(distance); 346 } 347 348 349 350 public void setDesiredSpeed(double m_speed) { 351 applet.setDesiredSpeed(m_speed); 352 } 354 355 356 357 public Double getSpeed() { 358 return activeSpeed.getSpeed(); 360 } 361 362 363 366 public void setAcceleration(double m_acc) { 367 this.acc = m_acc; 369 activeSpeed.setAcceleration(m_acc); 370 applet.setAcceleration(m_acc); 371 } 372 373 374 375 public Double getAcceleration() { 376 return new Double (this.acc); 378 } 379 380 381 383 386 public void setAlpha(double m_alpha) { 387 if ((this.state == ACTIVE) || (this.state == CRUISING)) { 388 activeSpeed.setAlpha(m_alpha); 389 applet.setAlpha(m_alpha); 390 } 392 393 } 394 } 395 396 397 | Popular Tags |