1 / 50

Appendix F – Elevator View

Appendix F – Elevator View. Outline F.1 Introduction F.2 Class Objects F.3 Class Constants F.4 Class Constructor F.5 Event Handling F.5.1 ElevatorMoveEvent types F.5.2 PersonMoveEvent types F.5.3 DoorEvent types F.5.4 ButtonEvent types

rdonohue
Download Presentation

Appendix F – Elevator View

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Appendix F – Elevator View OutlineF.1 IntroductionF.2 Class ObjectsF.3 Class ConstantsF.4 Class Constructor F.5 Event Handling F.5.1 ElevatorMoveEvent types F.5.2 PersonMoveEvent types F.5.3 DoorEvent types F.5.4 ButtonEvent types F.5.5 BellEvent types F.5.6 LightEvent types F.6 Artifacts Revisited F.7 Conclusion

  2. F.1 Introduction • Class ElevatorView • Graphical representation of elevator-simulation model • Largest class in simulation

  3. ElevatorView implements ElevatorSimulationListener, which inherits from all listener interfaces Constants for width and height of ElevatorView 1 // ElevatorView.java 2 // View for ElevatorSimulation 3 package com.deitel.jhtp5.elevator.view; 4 5 // Java core packages 6 import java.awt.*; 7 import java.awt.event.*; 8 import java.util.*; 9 import java.applet.*; 10 11 // Java extension package 12 import javax.swing.*; 13 14 // Deitel packages 15 import com.deitel.jhtp5.elevator.event.*; 16 import com.deitel.jhtp5.elevator.ElevatorConstants; 17 18 publicclass ElevatorView extends JPanel 19 implements ActionListener, ElevatorSimulationListener, 20 ElevatorConstants { 21 22 // ElevatorView dimensions 23 privatestaticfinalintVIEW_WIDTH = 800; 24 privatestaticfinalintVIEW_HEIGHT = 435; 25 26 // offset for positioning Panels in ElevatorView 27 privatestaticfinalintOFFSET = 10; ElevatorView.javaElevatorView displays the elevator simulation model.Lines 18-20Lines 23-24

  4. Constants for distances that Person must travel Time constants for distances Person travels Constant for time required to travel between Floors Constants for names of graphics files for Door and Person Constant for animation (refresh) rate 28 29 // Elevator repaints components every 50 ms 30 privatestaticfinalintANIMATION_DELAY = 50; 31 32 // horizontal distance constants 33 privatestaticfinalintPERSON_TO_BUTTON_DISTANCE = 400; 34 privatestaticfinalintBUTTON_TO_ELEVATOR_DISTANCE = 50; 35 privatestaticfinalintPERSON_TO_ELEVATOR_DISTANCE = 36 PERSON_TO_BUTTON_DISTANCE + BUTTON_TO_ELEVATOR_DISTANCE; 37 38 // times walking to Floor's Button and Elevator 39 privatestaticfinalintTIME_TO_BUTTON = 3000; // 3 seconds 40 privatestaticfinalintTIME_TO_ELEVATOR = 1000; // 1 second 41 42 // time traveling in Elevator (5 seconds) 43 privatestaticfinalintELEVATOR_TRAVEL_TIME = 5000; 44 45 // Door images for animation 46 privatestaticfinal String doorFrames[] = { 47 "images/door1.png", "images/door2.png", "images/door3.png", 48 "images/door4.png", "images/door5.png" }; 49 50 // Person images for animation 51 privatestaticfinal String personFrames[] = { 52 "images/bug1.png", "images/bug2.png", "images/bug3.png", 53 "images/bug4.png", "images/bug5.png", "images/bug6.png", 54 "images/bug7.png", "images/bug8.png" }; 55 ElevatorView.javaElevatorView displays the elevator simulation model. Line 30 Lines 33-36Lines 39-40Line 43Lines 46-54

  5. Constants for names of graphics files for Light, Button and Bell 56 // Light images for animation 57 privatestaticfinal String lightFrames[] = { 58 "images/lightOff.png", "images/lightOn.png" }; 59 60 // Floor Light images for animation 61 privatestaticfinal String firstFloorLightFrames[] = { 62 "images/firstFloorLightOff.png", 63 "images/firstFloorLightOn.png" }; 64 65 privatestaticfinal String secondFloorLightFrames[] = { 66 "images/secondFloorLightOff.png", 67 "images/secondFloorLightOn.png", }; 68 69 // Floor Button images for animation 70 privatestaticfinal String floorButtonFrames[] = { 71 "images/floorButtonUnpressed.png", 72 "images/floorButtonPressed.png", 73 "images/floorButtonLit.png" }; 74 75 // Elevator Button images for animation 76 privatestaticfinal String elevatorButtonFrames[] = { 77 "images/elevatorButtonUnpressed.png", 78 "images/elevatorButtonPressed.png", 79 "images/elevatorButtonLit.png" }; 80 81 // Bell images for animation 82 privatestaticfinal String bellFrames[] = { 83 "images/bell1.png", "images/bell2.png", 84 "images/bell3.png" }; ElevatorView.javaElevatorView displays the elevator simulation model. Lines 57-84

  6. Constants for names of sound-clip files Constant for name of “elevator music” file ImagePanels represent stationary objects in model (e.g., Floor, ElevatorShaft) Ceiling and wall are not in model, but we display them for realism 85 86 privatestaticfinal String floorImage = 87 "images/floor.png"; 88 privatestaticfinal String ceilingImage = 89 "images/ceiling.png"; 90 privatestaticfinal String elevatorImage = 91 "images/elevator.png"; 92 privatestaticfinal String wallImage = 93 "images/wall.jpg"; 94 privatestaticfinal String elevatorShaftImage = 95 "images/elevatorShaft.png"; 96 97 // audio files 98 privatestaticfinal String bellSound = "bell.wav"; 99 privatestaticfinal String doorOpenSound = "doorOpen.wav"; 100 privatestaticfinal String doorCloseSound = "doorClose.wav"; 101 privatestaticfinal String elevatorSound = "elevator.au"; 102 privatestaticfinal String buttonSound = "button.wav"; 103 privatestaticfinal String walkingSound = "walk.wav"; 104 privatestaticfinal String elevatorMusicSound = "liszt.mid"; 105 106 // ImagePanels for Floors, ElevatorShaft, wall and ceiling 107 private ImagePanel firstFloorPanel; 108 private ImagePanel secondFloorPanel; 109 private ImagePanel elevatorShaftPanel; 110 private ImagePanel wallPanel; 111 private ImagePanel ceilingPanel; 112 ElevatorView.javaElevatorView displays the elevator simulation model. Lines 88-89 and 92-93Lines 98-103Line 104Lines 107-111

  7. AudioClips for playing sound clips elevatorMusicClip plays music when Person rides Elevator MovingPanels represent objects that can move and have only one associated image (e.g., Elevator) AnimatedPanels represent objects in model with multiple images (e.g., Button, Person, Light, Bell and Door) List stores AnimatedPanels associated with Persons 113 // MovingPanels for Elevator 114 private MovingPanel elevatorPanel; 115 116 // AnimatedPanels for Buttons, Bell, Lights and Door 117 private AnimatedPanel firstFloorButtonPanel; 118 private AnimatedPanel secondFloorButtonPanel; 119 private AnimatedPanel elevatorButtonPanel; 120 private AnimatedPanel bellPanel; 121 private AnimatedPanel elevatorLightPanel; 122 private AnimatedPanel firstFloorLightPanel; 123 private AnimatedPanel secondFloorLightPanel; 124 private AnimatedPanel doorPanel; 125 126 // List containing AnimatedPanels for all Person objects 127 private java.util.List personAnimatedPanels; 128 129 // AudioClips for sound effects 130 private AudioClip bellClip; 131 private AudioClip doorOpenClip; 132 private AudioClip doorCloseClip; 133 private AudioClip elevatorClip; 134 private AudioClip buttonClip; 135 private AudioClip walkClip; 136 137 // ElevatorMusic to play in Elevator 138 private AudioClip elevatorMusicClip; 139 ElevatorView.javaElevatorView displays the elevator simulation model. Line 114Lines 117-124Line 127Lines 130-135Line 138

  8. Calculate velocity used by Elevator’s ImagePanel Timer determines when to redraw images Using null layout allows us to display images anywhere on ElevatorView 140 // Timer for animation controller; 141 private javax.swing.Timer animationTimer; 142 143 // distance from top of screen to display Floors 144 privateint firstFloorPosition; 145 privateint secondFloorPosition; 146 147 // Elevator's velocity 148 privatedouble elevatorVelocity; 149 150 // ElevatorView constructor 151 public ElevatorView() 152 { 153 // specifiy null Layout 154 super( null ); 155 156 instantiatePanels(); 157 placePanelsOnView(); 158 initializeAudio(); 159 160 // calculate distance Elevator travels 161 double floorDistance = 162 firstFloorPosition - secondFloorPosition; 163 164 // calculate time needed for travel 165 double time = ELEVATOR_TRAVEL_TIME / ANIMATION_DELAY; 166 167 // determine Elevator velocity (rate = distance / time) 168 elevatorVelocity = ( floorDistance + OFFSET ) / time; ElevatorView.javaElevatorView displays the elevator simulation model. Line 141Line 154Lines 165-168

  9. Starting Timer starts animation Instantiate ImagePanels for Floors Instantiate ImagePanel for wall (not used in model) Instantiate ImagePanel for ElevatorShaft 169 170 // start animation Thread 171 startAnimation(); 172 173 } // end ElevatorView constructor 174 175 // instantiate all Panels (Floors, Elevator, etc.) 176 privatevoid instantiatePanels() 177 { 178 // instantiate ImagePanels representing Floors 179 firstFloorPanel = new ImagePanel( 0, floorImage ); 180 secondFloorPanel = new ImagePanel( 0, floorImage ); 181 182 // calculate first and second Floor positions 183 firstFloorPosition = 184 VIEW_HEIGHT - firstFloorPanel.getHeight(); 185 secondFloorPosition = 186 ( int ) ( firstFloorPosition / 2 ) - OFFSET; 187 188 firstFloorPanel.setPosition( 0, firstFloorPosition ); 189 secondFloorPanel.setPosition( 0, secondFloorPosition ); 190 191 wallPanel = new ImagePanel( 0, wallImage ); 192 193 // create and position ImagePanel for ElevatorShaft 194 elevatorShaftPanel = 195 new ImagePanel( 0, elevatorShaftImage ); 196 ElevatorView.javaElevatorView displays the elevator simulation model. Line 171Lines 179-180Line 191Lines 194-195

  10. Instantiate ImagePanel for ceiling (not used in model) Instantiate MovingPanel for Elevator Instantiate AnimatedPanel for Button on first Floor 197 double xPosition = PERSON_TO_ELEVATOR_DISTANCE + OFFSET; 198 double yPosition = 199 firstFloorPosition - elevatorShaftPanel.getHeight(); 200 201 elevatorShaftPanel.setPosition( xPosition, yPosition ); 202 203 // create and position ImagePanel for ceiling 204 ceilingPanel = new ImagePanel( 0, ceilingImage ); 205 206 yPosition = elevatorShaftPanel.getPosition().getY() - 207 ceilingPanel.getHeight(); 208 209 ceilingPanel.setPosition( xPosition, yPosition ); 210 211 // create and position MovingPanel for Elevator 212 elevatorPanel = new MovingPanel( 0, elevatorImage ); 213 214 yPosition = firstFloorPosition - elevatorPanel.getHeight(); 215 216 elevatorPanel.setPosition( xPosition, yPosition ); 217 218 // create and position first Floor Button 219 firstFloorButtonPanel = 220 new AnimatedPanel( 0, floorButtonFrames ); 221 222 xPosition = PERSON_TO_BUTTON_DISTANCE + 2 * OFFSET; 223 yPosition = firstFloorPosition - 5 * OFFSET; 224 firstFloorButtonPanel.setPosition( xPosition, yPosition ); ElevatorView.javaElevatorView displays the elevator simulation model. Line 204Line 212Lines 219-220

  11. Instantiate AnimatedPanel for Button on second Floor Instantiate AnimatedPanel for Light on first Floor Instantiate AnimatedPanel for Light on second Floor AnimatedPanels use int arrays that determine their image sequences 225 226 int floorButtonPressedFrameOrder[] = { 0, 1, 2 }; 227 firstFloorButtonPanel.addFrameSequence( 228 floorButtonPressedFrameOrder ); 229 230 // create and position second Floor Button 231 secondFloorButtonPanel = 232 new AnimatedPanel( 1, floorButtonFrames ); 233 234 xPosition = PERSON_TO_BUTTON_DISTANCE + 2 * OFFSET; 235 yPosition = secondFloorPosition - 5 * OFFSET; 236 secondFloorButtonPanel.setPosition( xPosition, yPosition ); 237 238 secondFloorButtonPanel.addFrameSequence( 239 floorButtonPressedFrameOrder ); 240 241 // create and position Floor Lights 242 firstFloorLightPanel = 243 new AnimatedPanel( 0, firstFloorLightFrames ); 244 245 xPosition = elevatorPanel.getLocation().x - 4 * OFFSET; 246 yPosition = 247 firstFloorButtonPanel.getLocation().y - 10 * OFFSET; 248 firstFloorLightPanel.setPosition( xPosition, yPosition ); 249 250 secondFloorLightPanel = 251 new AnimatedPanel( 1, secondFloorLightFrames ); 252 ElevatorView.javaElevatorView displays the elevator simulation model. Lines 226-228 Lines 231-232Lines 242-243Lines 250-251

  12. Instantiate AnimatedPanel for Light inside Elevator Instantiate AnimatedPanel for Bell inside Elevator Instantiate AnimatedPanel for Door in Elevator (Note: we do not show Doors on Floors, because they would obscure Person when riding Elevator) 253 yPosition = 254 secondFloorButtonPanel.getLocation().y - 10 * OFFSET; 255 secondFloorLightPanel.setPosition( xPosition, yPosition ); 256 257 // create and position Door AnimatedPanels 258 doorPanel = new AnimatedPanel( 0, doorFrames ); 259 int doorOpenedFrameOrder[] = { 0, 1, 2, 3, 4 }; 260 int doorClosedFrameOrder[] = { 4, 3, 2, 1, 0 }; 261 doorPanel.addFrameSequence( doorOpenedFrameOrder ); 262 doorPanel.addFrameSequence( doorClosedFrameOrder ); 263 264 // determine where Door is located relative to Elevator 265 yPosition = 266 elevatorPanel.getHeight() - doorPanel.getHeight(); 267 268 doorPanel.setPosition( 0, yPosition ); 269 270 // create and position Light AnimatedPanel 271 elevatorLightPanel = new AnimatedPanel( 0, lightFrames ); 272 elevatorLightPanel.setPosition( OFFSET, 5 * OFFSET ); 273 274 // create and position Bell AnimatedPanel 275 bellPanel = new AnimatedPanel( 0, bellFrames ); 276 277 yPosition = elevatorLightPanel.getPosition().getY() + 278 elevatorLightPanel.getHeight() + OFFSET; 279 280 bellPanel.setPosition( OFFSET, yPosition ); 281 int bellRingAnimation[] = { 0, 1, 0, 2 }; 282 bellPanel.addFrameSequence( bellRingAnimation ); ElevatorView.javaElevatorView displays the elevator simulation model. Line 258 Line 271Line 275

  13. Instantiate AnimatedPanel for Button inside Elevator Add ImagePanels to ElevatorView Instantiate ArrayList to store references to AnimatedPanel’s associated with Person’s 283 284 // create and position Elevator's Button AnimatedPanel 285 elevatorButtonPanel = 286 new AnimatedPanel( 0, elevatorButtonFrames ); 287 288 yPosition = elevatorPanel.getHeight() - 6 * OFFSET; 289 elevatorButtonPanel.setPosition( 10 * OFFSET, yPosition ); 290 291 int buttonPressedFrameOrder[] = { 0, 1, 2 }; 292 elevatorButtonPanel.addFrameSequence( 293 buttonPressedFrameOrder ); 294 295 // create List to store Person AnimatedPanels 296 personAnimatedPanels = new ArrayList(); 297 298 } // end method instantiatePanels 299 300 // place all Panels on ElevatorView 301 privatevoid placePanelsOnView() 302 { 303 // add Panels to ElevatorView 304 add( firstFloorPanel ); 305 add( secondFloorPanel ); 306 add( ceilingPanel ); 307 add( elevatorPanel ); 308 add( firstFloorButtonPanel ); 309 add( secondFloorButtonPanel ); 310 add( firstFloorLightPanel ); 311 add( secondFloorLightPanel ); 312 add( elevatorShaftPanel ); 313 add( wallPanel ); ElevatorView.javaElevatorView displays the elevator simulation model. Lines 285-286Line 296Lines 305-314

  14. Use SoundEffects object to obtain references to AudioClips Add ImagePanels for elevator’s door, light bell and button to the ImagePanel associated with Elevator SoundEffects object creates AudioClips that play sound clips 314 315 // add Panels to Elevator's MovingPanel 316 elevatorPanel.add( doorPanel ); 317 elevatorPanel.add( elevatorLightPanel ); 318 elevatorPanel.add( bellPanel ); 319 elevatorPanel.add( elevatorButtonPanel ); 320 321 } // end method placePanelsOnView 322 323 // get sound effects and elevatorMusic 324 privatevoid initializeAudio() 325 { 326 // create AudioClip sound effects from audio files 327 SoundEffects sounds = new SoundEffects(); 328 sounds.setPathPrefix( "sounds/" ); 329 330 bellClip = sounds.getAudioClip( bellSound ); 331 doorOpenClip = sounds.getAudioClip( doorOpenSound ); 332 doorCloseClip = sounds.getAudioClip( doorCloseSound ); 333 elevatorClip = sounds.getAudioClip( elevatorSound ); 334 buttonClip = sounds.getAudioClip( buttonSound ); 335 walkClip = sounds.getAudioClip( walkingSound ); 336 elevatorMusicClip = sounds.getAudioClip( elevatorMusicSound ); 337 338 } // end method initializeAudio 339 ElevatorView.javaElevatorView displays the elevator simulation model. Lines 316-319Line 327Lines 330-336

  15. Timer invokes method actionPerformed every 50 (ANIMATION_DELAY) milliseconds Animate ImagePanels for Elevator and Floors Method startAnimation starts Timer, which starts animation Method stopAnimation stops Timer, which stops animation 340 // starts animation by repeatedly drawing images to screen 341 publicvoid startAnimation() 342 { 343 if ( animationTimer == null ) { 344 animationTimer = 345 new javax.swing.Timer( ANIMATION_DELAY, this ); 346 animationTimer.start(); 347 } 348 else 349 350 if ( !animationTimer.isRunning() ) 351 animationTimer.restart(); 352 } 353 354 // stop animation 355 publicvoid stopAnimation() 356 { 357 animationTimer.stop(); 358 } 359 360 // update AnimatedPanels animation in response to Timer 361 publicvoid actionPerformed( ActionEvent actionEvent ) 362 { 363 elevatorPanel.animate(); 364 365 firstFloorButtonPanel.animate(); 366 secondFloorButtonPanel.animate(); 367 ElevatorView.javaElevatorView displays the elevator simulation model. Lines 341-352Lines 355-358Lines 361-381Lines 363-366

  16. Stop sound clip played by Elevator-View when a Person walks Animate ImagePanels for Persons Obtain the Iterator of the ArrayList of (Person) AnimatedPanels 368 Iterator iterator = getPersonAnimatedPanelsIterator(); 369 370 while ( iterator.hasNext() ) { 371 372 // get Person's AnimatedPanel from Set 373 AnimatedPanel personPanel = 374 ( AnimatedPanel ) iterator.next(); 375 376 personPanel.animate(); // update panel 377 } 378 379 repaint(); // paint all Components 380 381 } // end method actionPerformed 382 383 private Iterator getPersonAnimatedPanelsIterator() 384 { 385 // obtain iterator from List 386 synchronized( personAnimatedPanels ) 387 { 388 returnnew ArrayList( personAnimatedPanels ).iterator(); 389 } 390 } 391 392 // stop sound clip of Person walking 393 privatevoid stopWalkingSound() 394 { 395 // stop playing walking sound 396 walkClip.stop(); ElevatorView.javaElevatorView displays the elevator simulation model. Lines 370-377Line 388Lines 393-407

  17. If a Person is still walking, continue the sound clip Obtain AnimatedPanel associated with Person that sent the PersonMoveEvent 397 398 Iterator iterator = getPersonAnimatedPanelsIterator(); 399 400 // but if Person is still walking, then keep playing 401 while ( iterator.hasNext() ) { 402 AnimatedPanel panel = ( AnimatedPanel ) iterator.next(); 403 404 if ( panel.getXVelocity() != 0 ) 405 walkClip.loop(); 406 } 407 } // end method stopWalkingSound 408 409 // returns Person AnimatedPanel with proper identifier 410 private AnimatedPanel getPersonPanel( PersonMoveEvent event ) 411 { 412 Iterator iterator = getPersonAnimatedPanelsIterator(); 413 414 while ( iterator.hasNext() ) { 415 416 // get next AnimatedPanel 417 AnimatedPanel personPanel = 418 ( AnimatedPanel ) iterator.next(); 419 420 // return AnimatedPanel with identifier that matches 421 if ( personPanel.getID() == event.getID() ) 422 return personPanel; 423 } 424 ElevatorView.javaElevatorView displays the elevator simulation model. Lines 401-406Lines 410-428

  18. Invoked when Elevator has departed from Floor Determine whether Person is on Elevator 425 // return null if no match with correct identifier 426 returnnull; 427 428 } // end method getPersonPanel 429 430 // invoked when Elevator has departed from Floor 431 publicvoid elevatorDeparted( ElevatorMoveEvent moveEvent ) 432 { 433 String location = 434 moveEvent.getLocation().getLocationName(); 435 436 // determine if Person is on Elevator 437 Iterator iterator = getPersonAnimatedPanelsIterator(); 438 439 while ( iterator.hasNext() ) { 440 441 AnimatedPanel personPanel = 442 ( AnimatedPanel ) iterator.next(); 443 444 double yPosition = personPanel.getPosition().getY(); 445 String panelLocation; 446 447 // determine on which Floor the Person entered 448 if ( yPosition > secondFloorPosition ) 449 panelLocation = FIRST_FLOOR_NAME; 450 else 451 panelLocation = SECOND_FLOOR_NAME; 452 ElevatorView.javaElevatorView displays the elevator simulation model. Line 431Lines 439-471

  19. If Person is inside Elevator, remove Person from Elevator-View and add Person to Elevator Determine Elevator velocity depending on Floor 453 int xPosition = 454 ( int ) personPanel.getPosition().getX(); 455 456 // if Person is inside Elevator 457 if ( panelLocation.equals( location ) 458 && xPosition > PERSON_TO_BUTTON_DISTANCE + OFFSET ) { 459 460 // remove Person AnimatedPanel from ElevatorView 461 remove( personPanel ); 462 463 // add Person AnimatedPanel to Elevator 464 elevatorPanel.add( personPanel, 1 ); 465 personPanel.setLocation( 2 * OFFSET, 9 * OFFSET ); 466 personPanel.setMoving( false ); 467 personPanel.setAnimating( false ); 468 personPanel.setVelocity( 0, 0 ); 469 personPanel.setCurrentFrame( 1 ); 470 } 471 } // end while loop 472 473 // determine Elevator velocity depending on Floor 474 if ( location.equals( FIRST_FLOOR_NAME ) ) 475 elevatorPanel.setVelocity( 0, -elevatorVelocity ); 476 else 477 478 if ( location.equals( SECOND_FLOOR_NAME ) ) 479 elevatorPanel.setVelocity( 0, elevatorVelocity ); 480 ElevatorView.javaElevatorView displays the elevator simulation model. Lines 457-470Lines 474-479

  20. Invoked when Elevator has arrived at Floor Set Elevator to moving state, then play sound effect and music associated with the Elevator’s movement Set Elevator to waiting state and stop music associated with the Elevator’s movement Set Elevator’s y-coordinate to that of the Floor on which the Elevator arrived 481 // begin moving Elevator and play Elevator music 482 elevatorPanel.setMoving( true ); 483 484 if ( elevatorClip != null ) 485 elevatorClip.play(); 486 487 elevatorMusicClip.play(); 488 489 } // end method elevatorDeparted 490 491 // invoked when Elevator has arrived at destination Floor 492 publicvoid elevatorArrived( ElevatorMoveEvent moveEvent ) 493 { 494 // stop Elevator and music 495 elevatorPanel.setMoving( false ); 496 elevatorMusicClip.stop(); 497 498 double xPosition = elevatorPanel.getPosition().getX(); 499 double yPosition; 500 501 // set Elevator's position to either first or second Floor 502 if ( elevatorPanel.getYVelocity() < 0 ) 503 yPosition = 504 secondFloorPosition - elevatorPanel.getHeight(); 505 else 506 yPosition = 507 firstFloorPosition - elevatorPanel.getHeight(); 508 509 elevatorPanel.setPosition( xPosition, yPosition ); 510 511 } // end method elevatorArrived ElevatorView.javaElevatorView displays the elevator simulation model. Lines 482-487 Line 492Lines 495-496Lines 502-509

  21. Position Person’s AnimatedPanel outside the view, so the Person does not suddenly “appear.” Invoked when user creates Person in simulation Determine which Person was created and on what Floor Create AnimatedPanel to represent Person in view 512 513 // invoked when Person has been created in model 514 publicvoid personCreated( PersonMoveEvent personEvent ) 515 { 516 int personID = personEvent.getID(); 517 518 String floorLocation = 519 personEvent.getLocation().getLocationName(); 520 521 // create AnimatedPanel representing Person 522 AnimatedPanel personPanel = 523 new AnimatedPanel( personID, personFrames ); 524 525 // determine where Person should be drawn initially 526 // negative xPosition ensures Person drawn offscreen 527 double xPosition = - personPanel.getWidth(); 528 double yPosition = 0; 529 530 if ( floorLocation.equals( FIRST_FLOOR_NAME ) ) 531 yPosition = firstFloorPosition + 532 ( firstFloorPanel.getHeight() / 2 ); 533 else 534 535 if ( floorLocation.equals( SECOND_FLOOR_NAME ) ) 536 yPosition = secondFloorPosition + 537 ( secondFloorPanel.getHeight() / 2 ); 538 539 yPosition -= personPanel.getHeight(); 540 541 personPanel.setPosition( xPosition, yPosition ); ElevatorView.javaElevatorView displays the elevator simulation model. Line 514Lines 516-519Lines 522-523Lines 530-541

  22. Calculate Person’s velocity and distance to Elevator Add animation sequences (e.g., walking and pressing buttons) for the Person Set the Person’s AnimatedPanel to its moving state (i.e., walking to Elevator) 542 543 // add some animations for each Person 544 int walkFrameOrder[] = { 1, 0, 1, 2 }; 545 int pressButtonFrameOrder[] = { 1, 3, 3, 4, 4, 1 }; 546 int walkAwayFrameOrder[] = { 6, 5, 6, 7 }; 547 personPanel.addFrameSequence( walkFrameOrder ); 548 personPanel.addFrameSequence( pressButtonFrameOrder ); 549 personPanel.addFrameSequence( walkAwayFrameOrder ); 550 551 // have Person begin walking to Elevator 552 personPanel.playAnimation( 0 ); 553 personPanel.setLoop( true ); 554 personPanel.setAnimating( true ); 555 personPanel.setMoving( true ); 556 557 // determine Person velocity 558 double time = 559 ( double ) ( TIME_TO_BUTTON / ANIMATION_DELAY ); 560 561 double xDistance = PERSON_TO_BUTTON_DISTANCE - 562 2 * OFFSET + personPanel.getSize().width; 563 double xVelocity = xDistance / time; 564 565 personPanel.setVelocity( xVelocity, 0 ); 566 personPanel.setAnimationRate( 1 ); 567 568 walkClip.loop(); // play sound clip of Person walking 569 ElevatorView.javaElevatorView displays the elevator simulation model. Lines 544-549Lines 552-555Lines 558-563

  23. Add Person’s AnimatedPanel to ElevatorView Set Person’s Animated-Panel to waiting state (waiting for at Elevator) Invoked when Person has arrived at Elevator Find AnimatedPanel associated with Person that issued event 570 // store in personAnimatedPanels 571 synchronized( personAnimatedPanels ) 572 { 573 personAnimatedPanels.add( personPanel ); 574 } 575 576 add( personPanel, 0 ); 577 578 } // end method personCreated 579 580 // invoked when Person has arrived at Elevator 581 publicvoid personArrived( PersonMoveEvent personEvent ) 582 { 583 // find Panel associated with Person that issued event 584 AnimatedPanel panel = getPersonPanel( personEvent ); 585 586 if ( panel != null ) { // if Person exists 587 588 // Person stops at Floor Button 589 panel.setMoving( false ); 590 panel.setAnimating( false ); 591 panel.setCurrentFrame( 1 ); 592 stopWalkingSound(); 593 594 double xPosition = PERSON_TO_BUTTON_DISTANCE - 595 ( panel.getSize().width / 2 ); 596 double yPosition = panel.getPosition().getY(); 597 598 panel.setPosition( xPosition, yPosition ); 599 } 600 } // end method personArrived ElevatorView.javaElevatorView displays the elevator simulation model. Line 573Line 581Line 584Lines 589-592

  24. Invoked when Person is entering Elevator Invoked when Person is pressing Button Find AnimatedPanel associated with Person that issued event Find AnimatedPanel associated with Person that issued event Start animation of Person pressing Button 601 602 // invoked when Person has pressed Button 603 publicvoid personPressedButton( PersonMoveEvent personEvent ) 604 { 605 // find Panel associated with Person that issued event 606 AnimatedPanel panel = getPersonPanel( personEvent ); 607 608 if ( panel != null ) { // if Person exists 609 610 // Person stops walking and presses Button 611 panel.setLoop( false ); 612 panel.playAnimation( 1 ); 613 614 panel.setVelocity( 0, 0 ); 615 panel.setMoving( false ); 616 panel.setAnimating( true ); 617 stopWalkingSound(); 618 } 619 } // end method personPressedButton 620 621 // invoked when Person has started to enter Elevator 622 publicvoid personEntered( PersonMoveEvent personEvent ) 623 { 624 // find Panel associated with Person that issued event 625 AnimatedPanel panel = getPersonPanel( personEvent ); 626 ElevatorView.javaElevatorView displays the elevator simulation model. Line 603Line 606Lines 611-612Line 622Line 625

  25. Set Person velocity to the opposite of that when the Person was created Determine Person’s velocity when entering Elevator, then set Person’s Animated-Panel to moving state Invoked when Person has exited Elevator Find AnimatedPanel associated with Person that issued event 627 if ( panel != null ) { 628 629 // determine velocity 630 double time = TIME_TO_ELEVATOR / ANIMATION_DELAY; 631 632 double distance = 633 elevatorPanel.getPosition().getX() - 634 panel.getPosition().getX() + 2 * OFFSET; 635 636 panel.setVelocity( distance / time, -1.5 ); 637 638 // Person starts walking 639 panel.setMoving( true ); 640 panel.playAnimation( 0 ); 641 panel.setLoop( true ); 642 } 643 } // end method personEntered 644 645 // invoked when Person has departed from Elevator 646 publicvoid personDeparted( PersonMoveEvent personEvent) 647 { 648 // find Panel associated with Person that issued event 649 AnimatedPanel panel = getPersonPanel( personEvent ); 650 651 if ( panel != null ) { // if Person exists 652 653 // determine velocity (in opposite direction) 654 double time = TIME_TO_BUTTON / ANIMATION_DELAY; 655 double xVelocity = - PERSON_TO_BUTTON_DISTANCE / time; 656 657 panel.setVelocity( xVelocity, 0 ); ElevatorView.javaElevatorView displays the elevator simulation model. Lines 627-642Line 646Line 649Line 657

  26. Set Person’s y-coordinate to that of the Floor on which the Elevator is located Add Person’s AnimatedPanel to ElevatorView 658 659 // remove Person from Elevator 660 elevatorPanel.remove( panel ); 661 662 double xPosition = 663 PERSON_TO_ELEVATOR_DISTANCE + 3 * OFFSET; 664 double yPosition = 0; 665 666 String floorLocation = 667 personEvent.getLocation().getLocationName(); 668 669 // determine Floor onto which Person exits 670 if ( floorLocation.equals( FIRST_FLOOR_NAME ) ) 671 yPosition = firstFloorPosition + 672 ( firstFloorPanel.getHeight() / 2 ); 673 else 674 675 if ( floorLocation.equals( SECOND_FLOOR_NAME ) ) 676 yPosition = secondFloorPosition + 677 ( secondFloorPanel.getHeight() / 2 ); 678 679 yPosition -= panel.getHeight(); 680 681 panel.setPosition( xPosition, yPosition ); 682 683 // add Person to ElevatorView 684 add( panel, 0 ); 685 ElevatorView.javaElevatorView displays the elevator simulation model. Lines 670-677Line 684

  27. Set Person’s AnimatedPanel to moving state Invoked when Person has exited simulation Find Panel associated with Person that issued moveEvent Remove Person’s AnimatedPanel from ElevatorView 686 // Person starts walking 687 panel.setMoving( true ); 688 panel.setAnimating( true ); 689 panel.playAnimation( 2 ); 690 panel.setLoop( true ); 691 walkClip.loop(); 692 } 693 } // end method PersonDeparted 694 695 // invoked when Person has exited simulation 696 publicvoid personExited( PersonMoveEvent personEvent) 697 { 698 // find Panel associated with Person that issued moveEvent 699 AnimatedPanel panel = getPersonPanel( personEvent ); 700 701 if ( panel != null ) { // if Person exists 702 703 panel.setMoving( false ); 704 panel.setAnimating( false ); 705 706 // remove Person permanently and stop walking sound 707 synchronized( personAnimatedPanels ) 708 { 709 personAnimatedPanels.remove( panel ); 710 } 711 remove( panel ); 712 stopWalkingSound(); 713 } 714 } // end method personExited ElevatorView.javaElevatorView displays the elevator simulation model. Lines 687-691Line 696Line 699Line 709

  28. Invoked when Door has opened Invoked when Door has closed Play animation of Door opening Play sound effect of Door opening Play animation of Door closing 715 716 // invoked when Door has opened in model 717 publicvoid doorOpened( DoorEvent doorEvent ) 718 { 719 // get DoorEvent Location 720 String location = 721 doorEvent.getLocation().getLocationName(); 722 723 // play animation of Door opening 724 doorPanel.playAnimation( 0 ); 725 doorPanel.setAnimationRate( 2 ); 726 doorPanel.setDisplayLastFrame( true ); 727 728 // play sound clip of Door opening 729 if ( doorOpenClip != null ) 730 doorOpenClip.play(); 731 732 } // end method doorOpened 733 734 // invoked when Door has closed in model 735 publicvoid doorClosed( DoorEvent doorEvent ) 736 { 737 // get DoorEvent Location 738 String location = 739 doorEvent.getLocation().getLocationName(); 740 741 // play animation of Door closing 742 doorPanel.playAnimation( 1 ); 743 doorPanel.setAnimationRate( 2 ); 744 doorPanel.setDisplayLastFrame( true ); ElevatorView.javaElevatorView displays the elevator simulation model. Line 717Lines 724-726Lines 729-730Line 739Lines 742-744

  29. If Button was pressed in Elevator, play animation of Elevator’s Button being pressed If Button was pressed on first Floor, play animation of first Floor’s Button being pressed Invoked when Button has been pressed Obtain Location where Button was pressed Play sound effect of Door closing 745 746 // play sound clip of Door closing 747 if ( doorCloseClip != null ) 748 doorCloseClip.play(); 749 750 } // end method doorClosed 751 752 // invoked when Button has been pressed in model 753 publicvoid buttonPressed( ButtonEvent buttonEvent ) 754 { 755 // get ButtonEvent Location 756 String location = 757 buttonEvent.getLocation().getLocationName(); 758 759 // press Elevator Button if from Elevator 760 if ( location.equals( ELEVATOR_NAME ) ) { 761 elevatorButtonPanel.playAnimation( 0 ); 762 elevatorButtonPanel.setDisplayLastFrame( true ); 763 } 764 765 // press Floor Button if from Floor 766 else 767 768 if ( location.equals( FIRST_FLOOR_NAME ) ) { 769 firstFloorButtonPanel.playAnimation( 0 ); 770 firstFloorButtonPanel.setDisplayLastFrame( true ); 771 } ElevatorView.javaElevatorView displays the elevator simulation model. Lines 747-748Line 753Lines 756-757Lines 760-763Lines 768-771

  30. If Button was reset in Elevator, play animation of Elevator’s Button being reset If Button was pressed on second Floor, play animation of second Floor’s Button being pressed Invoked when Button has been reset Obtain Location where Button was reset 772 else 773 774 if ( location.equals( SECOND_FLOOR_NAME ) ) { 775 secondFloorButtonPanel.playAnimation( 0 ); 776 secondFloorButtonPanel.setDisplayLastFrame( true ); 777 } 778 779 if ( buttonClip != null ) 780 buttonClip.play(); // play button press sound clip 781 782 } // end method buttonPressed 783 784 // invoked when Button has been reset in model 785 publicvoid buttonReset( ButtonEvent buttonEvent ) 786 { 787 // get ButtonEvent Location 788 String location = 789 buttonEvent.getLocation().getLocationName(); 790 791 // reset Elevator Button if from Elevator 792 if ( location.equals( ELEVATOR_NAME ) ) { 793 794 // return to first frame if still animating 795 if ( elevatorButtonPanel.isAnimating() ) 796 elevatorButtonPanel.setDisplayLastFrame( false ); 797 else 798 elevatorButtonPanel.setCurrentFrame( 0 ); 799 } ElevatorView.javaElevatorView displays the elevator simulation model. Lines 774-777Line 785Lines 788-789Lines 792-799

  31. If Button was reset on first Floor, play animation of first Floor’s Button being reset If Button was reset on second Floor, play animation of second Floor’s Button being reset 800 801 // reset Floor Button if from Floor 802 else 803 804 if ( location.equals( FIRST_FLOOR_NAME ) ) { 805 806 // return to first frame if still animating 807 if ( firstFloorButtonPanel.isAnimating() ) 808 firstFloorButtonPanel.setDisplayLastFrame( false ); 809 810 else 811 firstFloorButtonPanel.setCurrentFrame( 0 ); 812 } 813 else 814 815 if ( location.equals( SECOND_FLOOR_NAME ) ) { 816 817 // return to first frame if still animating 818 if ( secondFloorButtonPanel.isAnimating() ) 819 secondFloorButtonPanel.setDisplayLastFrame( 820 false ); 821 else 822 secondFloorButtonPanel.setCurrentFrame( 0 ); 823 } 824 825 } // end method buttonReset 826 ElevatorView.javaElevatorView displays the elevator simulation model. Lines 804-812Lines 815-823

  32. Invoked when Bell has rung Play animation and sound effect of Bell ringing Invoked when Light has turned on If Light was illuminated on first Floor, play animation of first Floor’s Light being turned on If Light was illuminated on second Floor, play animation of second Floor’s Light being turned on 827 // invoked when Bell has rung in model 828 publicvoid bellRang( BellEvent bellEvent ) 829 { 830 bellPanel.playAnimation( 0 ); // animate Bell 831 832 if ( bellClip != null ) // play Bell sound clip 833 bellClip.play(); 834 } 835 836 // invoked when Light turned on in model 837 publicvoid lightTurnedOn( LightEvent lightEvent ) 838 { 839 // turn on Light in Elevator 840 elevatorLightPanel.setCurrentFrame( 1 ); 841 842 String location = 843 lightEvent.getLocation().getLocationName(); 844 845 // turn on Light on either first or second Floor 846 if ( location.equals( FIRST_FLOOR_NAME ) ) 847 firstFloorLightPanel.setCurrentFrame( 1 ); 848 849 else 850 851 if ( location.equals( SECOND_FLOOR_NAME ) ) 852 secondFloorLightPanel.setCurrentFrame( 1 ); 853 854 } // end method lightTurnedOn 855 ElevatorView.javaElevatorView displays the elevator simulation model. Line 828Lines 830-833Line 837Lines 846-847Lines 851-852

  33. If Light was turned off first Floor, play animation of first Floor’s Light being turned off If Light was turned off on second Floor, play animation of second Floor’s Light being turned off Set ElevatorView’s preferred size to VIEW_WIDTH and VIEW_HEIGHT Invoked when Light has turned off 856 // invoked when Light turned off in model 857 publicvoid lightTurnedOff( LightEvent lightEvent ) 858 { 859 // turn off Light in Elevator 860 elevatorLightPanel.setCurrentFrame( 0 ); 861 862 String location = 863 lightEvent.getLocation().getLocationName(); 864 865 // turn off Light on either first or second Floor 866 if ( location.equals( FIRST_FLOOR_NAME ) ) 867 firstFloorLightPanel.setCurrentFrame( 0 ); 868 869 else 870 871 if ( location.equals( SECOND_FLOOR_NAME ) ) 872 secondFloorLightPanel.setCurrentFrame( 0 ); 873 874 } // end method lightTurnedOff 875 876 // return preferred size of ElevatorView 877 public Dimension getPreferredSize() 878 { 879 returnnew Dimension( VIEW_WIDTH, VIEW_HEIGHT ); 880 } 881 ElevatorView.javaElevatorView displays the elevator simulation model. Line 857 Lines 866-867Lines 871-872Lines 877-880

  34. Set ElevatorView’s minimum and maximum sizes to VIEW_WIDTH and VIEW_HEIGHT 882 // return minimum size of ElevatorView 883 public Dimension getMinimumSize() 884 { 885 return getPreferredSize(); 886 } 887 888 // return maximum size of ElevatorView 889 public Dimension getMaximumSize() 890 { 891 return getPreferredSize(); 892 } 893 } ElevatorView.javaElevatorView displays the elevator simulation model. Lines 883-892

  35. F.2 Class Objects • ImagePanel • Used for objects that are stationary in model • e.g., Floor, ElevatorShaft • MovingPanel • Used for objects that “move” in model • e.g., Elevator • AnimatedPanel • Used for objects that “animate” in model • e.g., Person, Door, Button, Bell, Light

  36. F.2 Class Objects (cont.)

  37. F.2 Class Objects (cont.)

  38. F.3 Class Constants • Constants specify such information as: • Initial placement of objects in ElevatorView • Rate at which ElevatorView redraws screen • Names of image files used by ImagePanels • Names of sound files used by SoundEffects • Distances in pixels the ImagePanels representing Elevator and Person must travel • Times needed to travel these distances

  39. F.4 Class Constructor • ElevatorView constructor’s responsibilities: • Instantiate ImagePanels • Add all ImagePanels to ElevatorView • Initialize audio objects • Compute initial velocity and distance traveled • Start animation Timer

  40. doorPanel : AnimatedPanel elevatorButtonPanel : AnimatedPanel doorOpenClip : AudioClip elevatorShaftPanel : ImagePanel secondFloorButtonPanel : AnimatedPanel bellPanel : AnimatedPanel buttonClip : AudioClip walkClip : AudioClip wallPanel : ImagePanel ceilingPanel : ImagePanel elevatorPanel : MovingPanel firstFloorLightPanel : AnimatedPanel firstFloorPanel : ImagePanel secondFloorPanel : ImagePanel lightPanel : AnimatedPanel bellClip : AudioClip doorCloseClip : AudioClip elevatorClip : AudioClip firstFloorButtonPanel : AnimatedPanel secondFloorLightPanel : AnimatedPanel : SoundEffects : ElevatorView F.4 Class Constructor (cont.) Fig F.4 Object diagram for the ElevatorView after initialization.

  41. F.5 Event Handling • Event Handling in the view • ElevatorView implements interface Elevator-SimulationListener • ElevatorView implements all interfaces • ElevatorSimulation sends events to ElevatorView • ElevatorCaseStudy registers ElevatorView for events from ElevatorSimulation

  42. F.5.1ElevatorMoveEventtypes • ElevatorSimulation • Sends ElevatorMoveEvent when Elevator departed or arrived in the model • Invokes method elevatorDeparted when Elevator departed from Floor • Invokes method elevatorArrived when Elevator arrived at Floor

  43. F.5.2PersonMoveEventtypes • ElevatorSimulation • Sends PersonMoveEvent when Person performes actions • Invokes method personCreated when model instantiates new Person • Invokes method personArrived when Person arrives at Elevator • Invokes method personPressedButton when Person presses Button • Invokes method personEntered when Person enters Elevator • Invokes method personDeparted when Person exits Elevator • Invokes method personExited when Person exits simulation

  44. F.5.3DoorEventtypes • ElevatorSimulation • Sends DoorEvent when Door opened or closed in the model • Invokes method doorOpened when Door opened • Invokes method doorClosed when Door closed

  45. F.5.4ButtonEventtypes • ElevatorSimulation • Sends ButtonEvent when Button pressed or reset in the model • Invokes method buttonPressed when Button pressed • Invokes method buttonReset when Button reset

  46. F.5.5BellEventtypes • ElevatorSimulation • Sends BellEvent when Bell rung • Invoking method bellRang

  47. F.5.6LightEventtypes • ElevatorSimulation • Sends LightEvent when Light changed state in the model • Invokes method lightTurnedOn when Light turned on • Invokes method lightTurnedOff when Light turned off

  48. F.6 Artifacts Revisited • Component Diagram for view • Package view contains six artifacts • ElevatorView.java • Aggregates (imports) packages images, sounds, events • ImagePanel.java • MovingPanel.java • AnimatedPanel.java • ElevatorMusic.java • SoundEffects.java

  49. view <<file>> <<imports>> <<imports>> ElevatorView.java <<imports>> <<file>> <<file>> ImagePanel.java MovingPanel.java <<file>> <<file>> AnimatedPanel.java SoundEffects.java 1 event images sounds 1 1 F.6 Component Diagrams Revisited (cont.) Fig F.5 Component diagram for package view.

  50. F.7 Conclusion • OOD/UML case study • Implement object-oriented system designs generated by UML • Using Java GUI, graphics and sound capabilities

More Related