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 Lines // 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.*; // Java extension package 12 import javax.swing.*; // Deitel packages 15 import com.deitel.jhtp5.elevator.event.*; 16 import com.deitel.jhtp5.elevator.ElevatorConstants; public class ElevatorView extends JPanel 19 implements ActionListener, ElevatorSimulationListener, 20 ElevatorConstants { // ElevatorView dimensions 23 private static final int VIEW_WIDTH = 800; 24 private static final int VIEW_HEIGHT = 435; // 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 Lines Line 43 Lines // Elevator repaints components every 50 ms 30 private static final int ANIMATION_DELAY = 50; // 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; // 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 // time traveling in Elevator (5 seconds) 43 private static final int ELEVATOR_TRAVEL_TIME = 5000; // 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" }; // 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 // Light images for animation 57 private static final String lightFrames[] = { 58 "images/lightOff.png", "images/lightOn.png" }; // Floor Light images for animation 61 private static final String firstFloorLightFrames[] = { 62 "images/firstFloorLightOff.png", 63 "images/firstFloorLightOn.png" }; private static final String secondFloorLightFrames[] = { 66 "images/secondFloorLightOff.png", 67 "images/secondFloorLightOn.png", }; // Floor Button images for animation 70 private static final String floorButtonFrames[] = { 71 "images/floorButtonUnpressed.png", 72 "images/floorButtonPressed.png", 73 "images/floorButtonLit.png" }; // Elevator Button images for animation 76 private static final String elevatorButtonFrames[] = { 77 "images/elevatorButtonUnpressed.png", 78 "images/elevatorButtonPressed.png", 79 "images/elevatorButtonLit.png" }; // 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 and Lines Line 104 Lines 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"; // 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"; // 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 Line 127 Lines Line // MovingPanels for Elevator 114 private MovingPanel elevatorPanel; // 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; // List containing AnimatedPanels for all Person objects 127 private java.util.List personAnimatedPanels; // 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; // 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 // Timer for animation controller; 141 private javax.swing.Timer animationTimer; // distance from top of screen to display Floors 144 private int firstFloorPosition; 145 private int secondFloorPosition; // Elevator's velocity 148 private double elevatorVelocity; // ElevatorView constructor 151 public ElevatorView() 152 { 153 // specifiy null Layout 154 super( null ); instantiatePanels(); 157 placePanelsOnView(); 158 initializeAudio(); // calculate distance Elevator travels 161 double floorDistance = 162 firstFloorPosition - secondFloorPosition; // calculate time needed for travel 165 double time = ELEVATOR_TRAVEL_TIME / ANIMATION_DELAY; // 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 Line 191 Lines // start animation Thread 171 startAnimation(); } // end ElevatorView constructor // 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 ); // calculate first and second Floor positions 183 firstFloorPosition = 184 VIEW_HEIGHT - firstFloorPanel.getHeight(); 185 secondFloorPosition = 186 ( int ) ( firstFloorPosition / 2 ) - OFFSET; firstFloorPanel.setPosition( 0, firstFloorPosition ); 189 secondFloorPanel.setPosition( 0, secondFloorPosition ); wallPanel = new ImagePanel( 0, wallImage ); // 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 double xPosition = PERSON_TO_ELEVATOR_DISTANCE + OFFSET; 198 double yPosition = 199 firstFloorPosition - elevatorShaftPanel.getHeight(); elevatorShaftPanel.setPosition( xPosition, yPosition ); // create and position ImagePanel for ceiling 204 ceilingPanel = new ImagePanel( 0, ceilingImage ); yPosition = elevatorShaftPanel.getPosition().getY() ceilingPanel.getHeight(); ceilingPanel.setPosition( xPosition, yPosition ); // create and position MovingPanel for Elevator 212 elevatorPanel = new MovingPanel( 0, elevatorImage ); yPosition = firstFloorPosition - elevatorPanel.getHeight(); elevatorPanel.setPosition( xPosition, yPosition ); // create and position first Floor Button 219 firstFloorButtonPanel = 220 new AnimatedPanel( 0, floorButtonFrames ); 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 Lines Lines Lines int floorButtonPressedFrameOrder[] = { 0, 1, 2 }; 227 firstFloorButtonPanel.addFrameSequence( 228 floorButtonPressedFrameOrder ); // create and position second Floor Button 231 secondFloorButtonPanel = 232 new AnimatedPanel( 1, floorButtonFrames ); xPosition = PERSON_TO_BUTTON_DISTANCE + 2 * OFFSET; 235 yPosition = secondFloorPosition - 5 * OFFSET; 236 secondFloorButtonPanel.setPosition( xPosition, yPosition ); secondFloorButtonPanel.addFrameSequence( 239 floorButtonPressedFrameOrder ); // create and position Floor Lights 242 firstFloorLightPanel = 243 new AnimatedPanel( 0, firstFloorLightFrames ); xPosition = elevatorPanel.getLocation().x - 4 * OFFSET; 246 yPosition = 247 firstFloorButtonPanel.getLocation().y - 10 * OFFSET; 248 firstFloorLightPanel.setPosition( xPosition, yPosition ); 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 yPosition = 254 secondFloorButtonPanel.getLocation().y - 10 * OFFSET; 255 secondFloorLightPanel.setPosition( xPosition, yPosition ); // 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 ); // determine where Door is located relative to Elevator 265 yPosition = 266 elevatorPanel.getHeight() - doorPanel.getHeight(); doorPanel.setPosition( 0, yPosition ); // create and position Light AnimatedPanel 271 elevatorLightPanel = new AnimatedPanel( 0, lightFrames ); 272 elevatorLightPanel.setPosition( OFFSET, 5 * OFFSET ); // create and position Bell AnimatedPanel 275 bellPanel = new AnimatedPanel( 0, bellFrames ); yPosition = elevatorLightPanel.getPosition().getY() elevatorLightPanel.getHeight() + OFFSET; 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 Line 296 Lines // create and position Elevator's Button AnimatedPanel 285 elevatorButtonPanel = 286 new AnimatedPanel( 0, elevatorButtonFrames ); yPosition = elevatorPanel.getHeight() - 6 * OFFSET; 289 elevatorButtonPanel.setPosition( 10 * OFFSET, yPosition ); int buttonPressedFrameOrder[] = { 0, 1, 2 }; 292 elevatorButtonPanel.addFrameSequence( 293 buttonPressedFrameOrder ); // create List to store Person AnimatedPanels 296 personAnimatedPanels = new ArrayList(); } // end method instantiatePanels // 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 Line 327 Lines // add Panels to Elevator's MovingPanel 316 elevatorPanel.add( doorPanel ); 317 elevatorPanel.add( elevatorLightPanel ); 318 elevatorPanel.add( bellPanel ); 319 elevatorPanel.add( elevatorButtonPanel ); } // end method placePanelsOnView // 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/" ); 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 ); } // 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 Lines Lines Lines // 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 if ( !animationTimer.isRunning() ) 351 animationTimer.restart(); 352 } // stop animation 355 public void stopAnimation() 356 { 357 animationTimer.stop(); 358 } // update AnimatedPanels animation in response to Timer 361 public void actionPerformed( ActionEvent actionEvent ) 362 { 363 elevatorPanel.animate(); 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 Line 388 Lines Iterator iterator = getPersonAnimatedPanelsIterator(); while ( iterator.hasNext() ) { // get Person's AnimatedPanel from Set 373 AnimatedPanel personPanel = 374 ( AnimatedPanel ) iterator.next(); personPanel.animate(); // update panel 377 } repaint(); // paint all Components } // end method actionPerformed private Iterator getPersonAnimatedPanelsIterator() 384 { 385 // obtain iterator from List 386 synchronized( personAnimatedPanels ) 387 { 388 return new ArrayList( personAnimatedPanels ).iterator(); 389 } 390 } // 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 Lines Iterator iterator = getPersonAnimatedPanelsIterator(); // but if Person is still walking, then keep playing 401 while ( iterator.hasNext() ) { 402 AnimatedPanel panel = ( AnimatedPanel ) iterator.next(); if ( panel.getXVelocity() != 0 ) 405 walkClip.loop(); 406 } 407 } // end method stopWalkingSound // returns Person AnimatedPanel with proper identifier 410 private AnimatedPanel getPersonPanel( PersonMoveEvent event ) 411 { 412 Iterator iterator = getPersonAnimatedPanelsIterator(); while ( iterator.hasNext() ) { // get next AnimatedPanel 417 AnimatedPanel personPanel = 418 ( AnimatedPanel ) iterator.next(); // 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 // return null if no match with correct identifier 426 return null; } // end method getPersonPanel // invoked when Elevator has departed from Floor 431 public void elevatorDeparted( ElevatorMoveEvent moveEvent ) 432 { 433 String location = 434 moveEvent.getLocation().getLocationName(); // determine if Person is on Elevator 437 Iterator iterator = getPersonAnimatedPanelsIterator(); while ( iterator.hasNext() ) { AnimatedPanel personPanel = 442 ( AnimatedPanel ) iterator.next(); double yPosition = personPanel.getPosition().getY(); 445 String panelLocation; // 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 Lines int xPosition = 454 ( int ) personPanel.getPosition().getX(); // if Person is inside Elevator 457 if ( panelLocation.equals( location ) 458 && xPosition > PERSON_TO_BUTTON_DISTANCE + OFFSET ) { // remove Person AnimatedPanel from ElevatorView 461 remove( personPanel ); // 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 // determine Elevator velocity depending on Floor 474 if ( location.equals( FIRST_FLOOR_NAME ) ) 475 elevatorPanel.setVelocity( 0, -elevatorVelocity ); 476 else 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 Line 492 Lines Lines // begin moving Elevator and play Elevator music 482 elevatorPanel.setMoving( true ); if ( elevatorClip != null ) 485 elevatorClip.play(); elevatorMusicClip.play(); } // end method elevatorDeparted // 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(); double xPosition = elevatorPanel.getPosition().getX(); 499 double yPosition; // 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(); elevatorPanel.setPosition( xPosition, yPosition ); } // 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 Lines Lines // invoked when Person has been created in model 514 public void personCreated( PersonMoveEvent personEvent ) 515 { 516 int personID = personEvent.getID(); String floorLocation = 519 personEvent.getLocation().getLocationName(); // create AnimatedPanel representing Person 522 AnimatedPanel personPanel = 523 new AnimatedPanel( personID, personFrames ); // determine where Person should be drawn initially 526 // negative xPosition ensures Person drawn offscreen 527 double xPosition = - personPanel.getWidth(); 528 double yPosition = 0; if ( floorLocation.equals( FIRST_FLOOR_NAME ) ) 531 yPosition = firstFloorPosition ( firstFloorPanel.getHeight() / 2 ); 533 else if ( floorLocation.equals( SECOND_FLOOR_NAME ) ) 536 yPosition = secondFloorPosition ( secondFloorPanel.getHeight() / 2 ); yPosition -= personPanel.getHeight(); 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 Lines Lines // 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 ); // have Person begin walking to Elevator 552 personPanel.playAnimation( 0 ); 553 personPanel.setLoop( true ); 554 personPanel.setAnimating( true ); 555 personPanel.setMoving( true ); // determine Person velocity 558 double time = 559 ( double ) ( TIME_TO_BUTTON / ANIMATION_DELAY ); double xDistance = PERSON_TO_BUTTON_DISTANCE * OFFSET + personPanel.getSize().width; 563 double xVelocity = xDistance / time; personPanel.setVelocity( xVelocity, 0 ); 566 personPanel.setAnimationRate( 1 ); 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 // store in personAnimatedPanels 571 synchronized( personAnimatedPanels ) 572 { 573 personAnimatedPanels.add( personPanel ); 574 } add( personPanel, 0 ); } // end method personCreated // 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 ); if ( panel != null ) { // if Person exists // Person stops at Floor Button 589 panel.setMoving( false ); 590 panel.setAnimating( false ); 591 panel.setCurrentFrame( 1 ); 592 stopWalkingSound(); double xPosition = PERSON_TO_BUTTON_DISTANCE ( panel.getSize().width / 2 ); 596 double yPosition = panel.getPosition().getY(); 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 Line 622 Line // 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 ); if ( panel != null ) { // if Person exists // Person stops walking and presses Button 611 panel.setLoop( false ); 612 panel.playAnimation( 1 ); panel.setVelocity( 0, 0 ); 615 panel.setMoving( false ); 616 panel.setAnimating( true ); 617 stopWalkingSound(); 618 } 619 } // end method personPressedButton // 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 Line 646 Line 649 Line if ( panel != null ) { // determine velocity 630 double time = TIME_TO_ELEVATOR / ANIMATION_DELAY; double distance = 633 elevatorPanel.getPosition().getX() panel.getPosition().getX() + 2 * OFFSET; panel.setVelocity( distance / time, -1.5 ); // Person starts walking 639 panel.setMoving( true ); 640 panel.playAnimation( 0 ); 641 panel.setLoop( true ); 642 } 643 } // end method personEntered // 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 ); if ( panel != null ) { // if Person exists // determine velocity (in opposite direction) 654 double time = TIME_TO_BUTTON / ANIMATION_DELAY; 655 double xVelocity = - PERSON_TO_BUTTON_DISTANCE / time; 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 Line // remove Person from Elevator 660 elevatorPanel.remove( panel ); double xPosition = 663 PERSON_TO_ELEVATOR_DISTANCE + 3 * OFFSET; 664 double yPosition = 0; String floorLocation = 667 personEvent.getLocation().getLocationName(); // determine Floor onto which Person exits 670 if ( floorLocation.equals( FIRST_FLOOR_NAME ) ) 671 yPosition = firstFloorPosition ( firstFloorPanel.getHeight() / 2 ); 673 else if ( floorLocation.equals( SECOND_FLOOR_NAME ) ) 676 yPosition = secondFloorPosition ( secondFloorPanel.getHeight() / 2 ); yPosition -= panel.getHeight(); panel.setPosition( xPosition, yPosition ); // 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 Line 696 Line 699 Line // 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 // 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 ); if ( panel != null ) { // if Person exists panel.setMoving( false ); 704 panel.setAnimating( false ); // 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 Lines Line 739 Lines // 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(); // play animation of Door opening 724 doorPanel.playAnimation( 0 ); 725 doorPanel.setAnimationRate( 2 ); 726 doorPanel.setDisplayLastFrame( true ); // play sound clip of Door opening 729 if ( doorOpenClip != null ) 730 doorOpenClip.play(); } // end method doorOpened // 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(); // 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 Line 753 Lines Lines Lines // play sound clip of Door closing 747 if ( doorCloseClip != null ) 748 doorCloseClip.play(); } // end method doorClosed // 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(); // press Elevator Button if from Elevator 760 if ( location.equals( ELEVATOR_NAME ) ) { 761 elevatorButtonPanel.playAnimation( 0 ); 762 elevatorButtonPanel.setDisplayLastFrame( true ); 763 } // press Floor Button if from Floor 766 else 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 Line 785 Lines Lines else if ( location.equals( SECOND_FLOOR_NAME ) ) { 775 secondFloorButtonPanel.playAnimation( 0 ); 776 secondFloorButtonPanel.setDisplayLastFrame( true ); 777 } if ( buttonClip != null ) 780 buttonClip.play(); // play button press sound clip } // end method buttonPressed // 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(); // reset Elevator Button if from Elevator 792 if ( location.equals( ELEVATOR_NAME ) ) { // 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 Lines // reset Floor Button if from Floor 802 else if ( location.equals( FIRST_FLOOR_NAME ) ) { // return to first frame if still animating 807 if ( firstFloorButtonPanel.isAnimating() ) 808 firstFloorButtonPanel.setDisplayLastFrame( false ); else 811 firstFloorButtonPanel.setCurrentFrame( 0 ); 812 } 813 else if ( location.equals( SECOND_FLOOR_NAME ) ) { // return to first frame if still animating 818 if ( secondFloorButtonPanel.isAnimating() ) 819 secondFloorButtonPanel.setDisplayLastFrame( 820 false ); 821 else 822 secondFloorButtonPanel.setCurrentFrame( 0 ); 823 } } // 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 Line 837 Lines Lines // invoked when Bell has rung in model 828 public void bellRang( BellEvent bellEvent ) 829 { 830 bellPanel.playAnimation( 0 ); // animate Bell if ( bellClip != null ) // play Bell sound clip 833 bellClip.play(); 834 } // invoked when Light turned on in model 837 public void lightTurnedOn( LightEvent lightEvent ) 838 { 839 // turn on Light in Elevator 840 elevatorLightPanel.setCurrentFrame( 1 ); String location = 843 lightEvent.getLocation().getLocationName(); // turn on Light on either first or second Floor 846 if ( location.equals( FIRST_FLOOR_NAME ) ) 847 firstFloorLightPanel.setCurrentFrame( 1 ); else if ( location.equals( SECOND_FLOOR_NAME ) ) 852 secondFloorLightPanel.setCurrentFrame( 1 ); } // 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 Lines Lines // invoked when Light turned off in model 857 public void lightTurnedOff( LightEvent lightEvent ) 858 { 859 // turn off Light in Elevator 860 elevatorLightPanel.setCurrentFrame( 0 ); String location = 863 lightEvent.getLocation().getLocationName(); // turn off Light on either first or second Floor 866 if ( location.equals( FIRST_FLOOR_NAME ) ) 867 firstFloorLightPanel.setCurrentFrame( 0 ); else if ( location.equals( SECOND_FLOOR_NAME ) ) 872 secondFloorLightPanel.setCurrentFrame( 0 ); } // end method lightTurnedOff // 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 // return minimum size of ElevatorView 883 public Dimension getMinimumSize() 884 { 885 return getPreferredSize(); 886 } // 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 >

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