Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2003 Prentice Hall, Inc. All rights reserved. 1 Appendix F – Elevator View Outline F.1Introduction F.2Class Objects F.3Class Constants F.4Class Constructor.

Similar presentations


Presentation on theme: " 2003 Prentice Hall, Inc. All rights reserved. 1 Appendix F – Elevator View Outline F.1Introduction F.2Class Objects F.3Class Constants F.4Class Constructor."— Presentation transcript:

1  2003 Prentice Hall, Inc. All rights reserved. 1 Appendix F – Elevator View Outline F.1Introduction F.2Class Objects F.3Class Constants F.4Class Constructor F.5Event 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.6Artifacts Revisited F.7 Conclusion

2  2003 Prentice Hall, Inc. All rights reserved. 2 F.1 Introduction Class ElevatorView –Graphical representation of elevator-simulation model –Largest class in simulation

3  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Lines 18-20 Lines 23-24 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 public class ElevatorView extends JPanel 19 implements ActionListener, ElevatorSimulationListener, 20 ElevatorConstants { 21 22 // ElevatorView dimensions 23 private static final int VIEW_WIDTH = 800; 24 private static final int VIEW_HEIGHT = 435; 25 26 // offset for positioning Panels in ElevatorView 27 private static final int OFFSET = 10; ElevatorView implements ElevatorSimulationListener, which inherits from all listener interfaces Constants for width and height of ElevatorView

4  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Line 30 Lines 33-36 Lines 39-40 Line 43 Lines 46-54 28 29 // Elevator repaints components every 50 ms 30 private static final int ANIMATION_DELAY = 50; 31 32 // horizontal distance constants 33 private static final int PERSON_TO_BUTTON_DISTANCE = 400; 34 private static final int BUTTON_TO_ELEVATOR_DISTANCE = 50; 35 private static final int PERSON_TO_ELEVATOR_DISTANCE = 36 PERSON_TO_BUTTON_DISTANCE + BUTTON_TO_ELEVATOR_DISTANCE; 37 38 // times walking to Floor's Button and Elevator 39 private static final int TIME_TO_BUTTON = 3000; // 3 seconds 40 private static final int TIME_TO_ELEVATOR = 1000; // 1 second 41 42 // time traveling in Elevator (5 seconds) 43 private static final int ELEVATOR_TRAVEL_TIME = 5000; 44 45 // Door images for animation 46 private static final 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 private static final 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 Constants for distances that Person must travel Time constants for distances P erson travels Constant for time required to travel between Floor s Constants for names of graphics files for Door and Person Constant for animation (refresh) rate

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

6  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Lines 88-89 and 92-93 Lines 98-103 Line 104 Lines 107-111 85 86 private static final String floorImage = 87 "images/floor.png"; 88 private static final String ceilingImage = 89 "images/ceiling.png"; 90 private static final String elevatorImage = 91 "images/elevator.png"; 92 private static final String wallImage = 93 "images/wall.jpg"; 94 private static final String elevatorShaftImage = 95 "images/elevatorShaft.png"; 96 97 // audio files 98 private static final String bellSound = "bell.wav"; 99 private static final String doorOpenSound = "doorOpen.wav"; 100 private static final String doorCloseSound = "doorClose.wav"; 101 private static final String elevatorSound = "elevator.au"; 102 private static final String buttonSound = "button.wav"; 103 private static final String walkingSound = "walk.wav"; 104 private static final 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 Ceiling and wall are not in model, but we display them for realism Constants for names of sound-clip files Constant for name of “elevator music” file ImagePanel s represent stationary objects in model (e.g., Floor, ElevatorShaft )

7  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Line 114 Lines 117-124 Line 127 Lines 130-135 Line 138 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 MovingPanel s represent objects that can move and have only one associated image (e.g., Elevator ) AnimatedPanel s represent objects in model with multiple images (e.g., Button, Person, Light, Bell and Door ) List stores AnimatedPanel s associated with Person s AudioClip s for playing sound clips elevatorMusicClip plays music when Person rides Elevator

8  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Line 141 Line 154 Lines 165-168 140 // Timer for animation controller; 141 private javax.swing.Timer animationTimer; 142 143 // distance from top of screen to display Floors 144 private int firstFloorPosition; 145 private int secondFloorPosition; 146 147 // Elevator's velocity 148 private double 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; Timer determines when to redraw images Using null layout allows us to display images anywhere on ElevatorView Calculate velocity used by Elevator ’s ImagePanel

9  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Line 171 Lines 179-180 Line 191 Lines 194-195 169 170 // start animation Thread 171 startAnimation(); 172 173 } // end ElevatorView constructor 174 175 // instantiate all Panels (Floors, Elevator, etc.) 176 private void 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 Starting Timer starts animation Instantiate ImagePanel s for Floor s Instantiate ImagePanel for wall (not used in model) Instantiate ImagePanel for ElevatorShaft

10  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Line 204 Line 212 Lines 219-220 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 ); Instantiate ImagePanel for ceiling (not used in model) Instantiate MovingPanel for Elevator Instantiate AnimatedPanel for Button on first Floor

11  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Lines 226-228 Lines 231-232 Lines 242-243 Lines 250-251 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 Instantiate AnimatedPanel for Button on second Floor Instantiate AnimatedPanel for Light on first Floor Instantiate AnimatedPanel for Light on second Floor AnimatedPanel s use int arrays that determine their image sequences

12  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Line 258 Line 271 Line 275 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 ); Instantiate AnimatedPanel for Light inside Elevator Instantiate AnimatedPanel for Bell inside Elevator Instantiate AnimatedPanel for Door in Elevator (Note: we do not show Door s on Floor s, because they would obscure Person when riding Elevator )

13  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Lines 285-286 Line 296 Lines 305-314 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 private void 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 ); Instantiate AnimatedPanel for Button inside Elevator Instantiate ArrayList to store references to AnimatedPanel ’s associated with Person ’s Add ImagePanel s to ElevatorView

14  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Lines 316-319 Line 327 Lines 330-336 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 private void 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 Add ImagePanel s for elevator’s door, light bell and button to the ImagePanel associated with Elevator SoundEffects object creates AudioClip s that play sound clips Use SoundEffects object to obtain references to AudioClip s

15  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Lines 341-352 Lines 355-358 Lines 361-381 Lines 363-366 340 // starts animation by repeatedly drawing images to screen 341 public void 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 public void stopAnimation() 356 { 357 animationTimer.stop(); 358 } 359 360 // update AnimatedPanels animation in response to Timer 361 public void actionPerformed( ActionEvent actionEvent ) 362 { 363 elevatorPanel.animate(); 364 365 firstFloorButtonPanel.animate(); 366 secondFloorButtonPanel.animate(); 367 Method startAnimation starts Timer, which starts animation Method stopAnimation stops Timer, which stops animation Timer invokes method actionPerformed every 50 ( ANIMATION_DELAY ) milliseconds Animate ImagePanel s for Elevator and Floors

16  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Lines 370-377 Line 388 Lines 393-407 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 return new ArrayList( personAnimatedPanels ).iterator(); 389 } 390 } 391 392 // stop sound clip of Person walking 393 private void stopWalkingSound() 394 { 395 // stop playing walking sound 396 walkClip.stop(); Animate ImagePanel s for Person sObtain the Iterator of the ArrayList of ( Person ) AnimatedPanel s Stop sound clip played by Elevator- View when a Person walks

17  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Lines 401-406 Lines 410-428 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 If a Person is still walking, continue the sound clip Obtain AnimatedPanel associated with Person that sent the PersonMoveEvent

18  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Line 431 Lines 439-471 425 // return null if no match with correct identifier 426 return null; 427 428 } // end method getPersonPanel 429 430 // invoked when Elevator has departed from Floor 431 public void 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 Invoked when Elevator has departed from Floor Determine whether Person is on Elevator

19  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Lines 457-470 Lines 474-479 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 If Person is inside Elevator, remove Person from Elevator- View and add Person to Elevator Determine Elevator velocity depending on Floor

20  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Lines 482-487 Line 492 Lines 495-496 Lines 502-509 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 public void 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 Invoked when Elevator has arrived at Floor 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 Set Elevator to moving state, then play sound effect and music associated with the Elevator ’s movement

21  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Line 514 Lines 516-519 Lines 522-523 Lines 530-541 512 513 // invoked when Person has been created in model 514 public void 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 ); Invoked when user creates Person in simulation Determine which Person was created and on what Floor Create AnimatedPanel to represent Person in view Position Person ’s AnimatedPanel outside the view, so the Person does not suddenly “appear.”

22  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Lines 544-549 Lines 552-555 Lines 558-563 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 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 ) Calculate Person ’s velocity and distance to Elevator

23  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Line 573 Line 581 Line 584 Lines 589-592 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 public void 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 Add Person ’s AnimatedPanel to ElevatorView Invoked when Person has arrived at Elevator Find AnimatedPanel associated with Person that issued event Set Person ’s Animated- Panel to waiting state (waiting for at Elevator )

24  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Line 603 Line 606 Lines 611-612 Line 622 Line 625 601 602 // invoked when Person has pressed Button 603 public void 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 public void personEntered( PersonMoveEvent personEvent ) 623 { 624 // find Panel associated with Person that issued event 625 AnimatedPanel panel = getPersonPanel( personEvent ); 626 Invoked when Person is pressing Button Find AnimatedPanel associated with Person that issued event Start animation of Person pressing Button Invoked when Person is entering Elevator Find AnimatedPanel associated with Person that issued event

25  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Lines 627-642 Line 646 Line 649 Line 657 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 public void 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 ); 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 Set Person velocity to the opposite of that when the Person was created

26  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Lines 670-677 Line 684 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 Set Person ’s y-coordinate to that of the Floor on which the Elevator is located Add Person ’s AnimatedPanel to ElevatorView

27  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Lines 687-691 Line 696 Line 699 Line 709 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 public void 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 Invoked when Person has exited simulation Find Panel associated with Person that issued moveEvent Remove Person ’s AnimatedPanel from ElevatorView Set Person ’s AnimatedPanel to moving state

28  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Line 717 Lines 724-726 Lines 729-730 Line 739 Lines 742-744 715 716 // invoked when Door has opened in model 717 public void 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 public void 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 ); Invoked when Door has openedPlay animation of Door openingPlay sound effect of Door openingInvoked when Door has closedPlay animation of Door closing

29  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Lines 747-748 Line 753 Lines 756-757 Lines 760-763 Lines 768-771 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 public void 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 } Play sound effect of Door closingInvoked when Button has been pressed Obtain Location where Button was pressed 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

30  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Lines 774-777 Line 785 Lines 788-789 Lines 792-799 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 public void 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 } 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 If Button was reset in Elevator, play animation of Elevator ’s Button being reset

31  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Lines 804-812 Lines 815-823 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 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

32  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Line 828 Lines 830-833 Line 837 Lines 846-847 Lines 851-852 827 // invoked when Bell has rung in model 828 public void 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 public void 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 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

33  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Line 857 Lines 866-867 Lines 871-872 Lines 877-880 856 // invoked when Light turned off in model 857 public void 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 return new Dimension( VIEW_WIDTH, VIEW_HEIGHT ); 880 } 881 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

34  2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorView.ja va ElevatorView displays the elevator simulation model. Lines 883-892 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 } Set ElevatorView ’s minimum and maximum sizes to VIEW_WIDTH and VIEW_HEIGHT

35  2003 Prentice Hall, Inc. All rights reserved. 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  2003 Prentice Hall, Inc. All rights reserved. 36 F.2 Class Objects (cont.)

37  2003 Prentice Hall, Inc. All rights reserved. 37 F.2 Class Objects (cont.)

38  2003 Prentice Hall, Inc. All rights reserved. 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 ImagePanel s –Names of sound files used by SoundEffects –Distances in pixels the ImagePanel s representing Elevator and Person must travel –Times needed to travel these distances

39  2003 Prentice Hall, Inc. All rights reserved. 39 F.4 Class Constructor ElevatorView constructor’s responsibilities: –Instantiate ImagePanel s –Add all ImagePanel s to ElevatorView –Initialize audio objects –Compute initial velocity and distance traveled –Start animation Timer

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

41  2003 Prentice Hall, Inc. All rights reserved. 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  2003 Prentice Hall, Inc. All rights reserved. 42 F.5.1 ElevatorMoveEvent types 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  2003 Prentice Hall, Inc. All rights reserved. 43 F.5.2 PersonMoveEvent types 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  2003 Prentice Hall, Inc. All rights reserved. 44 F.5.3 DoorEvent types ElevatorSimulation –Sends DoorEvent when Door opened or closed in the model –Invokes method doorOpened when Door opened –Invokes method doorClosed when Door closed

45  2003 Prentice Hall, Inc. All rights reserved. 45 F.5.4 ButtonEvent types ElevatorSimulation –Sends ButtonEvent when Button pressed or reset in the model –Invokes method buttonPressed when Button pressed –Invokes method buttonReset when Button reset

46  2003 Prentice Hall, Inc. All rights reserved. 46 F.5.5 BellEvent types ElevatorSimulation –Sends BellEvent when Bell rung Invoking method bellRang

47  2003 Prentice Hall, Inc. All rights reserved. 47 F.5.6 LightEvent types 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  2003 Prentice Hall, Inc. All rights reserved. 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  2003 Prentice Hall, Inc. All rights reserved. 49 F.6 Component Diagrams Revisited (cont.) Fig F.5 Component diagram for package view. view ElevatorView.java > ImagePanel.java > SoundEffects.java > AnimatedPanel.java > MovingPanel.java > imagessounds event 1 1 1 >

50  2003 Prentice Hall, Inc. All rights reserved. 50 F.7 Conclusion OOD/UML case study –Implement object-oriented system designs generated by UML –Using Java GUI, graphics and sound capabilities


Download ppt " 2003 Prentice Hall, Inc. All rights reserved. 1 Appendix F – Elevator View Outline F.1Introduction F.2Class Objects F.3Class Constants F.4Class Constructor."

Similar presentations


Ads by Google