2003 Prentice Hall, Inc. All rights reserved. 1 Appendix F – Elevator View Outline F.1Introduction...

50
2003 Prentice Hall, Inc. All rights reserved. 1 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 F.5.5 BellEvent types F.5.6 LightEvent types F.6 Artifacts Revisited F.7 Conclusion

Transcript of 2003 Prentice Hall, Inc. All rights reserved. 1 Appendix F – Elevator View Outline F.1Introduction...

2003 Prentice Hall, Inc. All rights reserved.

1

Appendix F – Elevator View

OutlineF.1 IntroductionF.2 Class ObjectsF.3 Class ConstantsF.4 Class ConstructorF.5 Event Handling

F.5.1 ElevatorMoveEvent typesF.5.2 PersonMoveEvent typesF.5.3 DoorEvent typesF.5.4 ButtonEvent typesF.5.5 BellEvent typesF.5.6 LightEvent types

F.6 Artifacts RevisitedF.7 Conclusion

2003 Prentice Hall, Inc. All rights reserved.

2

F.1 Introduction

• Class ElevatorView– Graphical representation of elevator-simulation model

– Largest class in simulation

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView displays the elevator simulation model.

Lines 18-20

Lines 23-24

1 // ElevatorView.java2 // View for ElevatorSimulation3 package com.deitel.jhtp5.elevator.view;4 5 // Java core packages6 import java.awt.*;7 import java.awt.event.*;8 import java.util.*;9 import java.applet.*;10 11 // Java extension package12 import javax.swing.*;13 14 // Deitel packages15 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 dimensions23 private static final int VIEW_WIDTH = 800;24 private static final int VIEW_HEIGHT = 435;25 26 // offset for positioning Panels in ElevatorView27 private static final int OFFSET = 10;

ElevatorView implements ElevatorSimulationListener,

which inherits from all listener interfaces

Constants for width and height of ElevatorView

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView 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 ms30 private static final int ANIMATION_DELAY = 50;31 32 // horizontal distance constants33 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 Elevator39 private static final int TIME_TO_BUTTON = 3000; // 3 seconds40 private static final int TIME_TO_ELEVATOR = 1000; // 1 second41 42 // time traveling in Elevator (5 seconds)43 private static final int ELEVATOR_TRAVEL_TIME = 5000;44 45 // Door images for animation46 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 animation51 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 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

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView displays the elevator simulation model.

Lines 57-84

56 // Light images for animation57 private static final String lightFrames[] = {58 "images/lightOff.png", "images/lightOn.png" };59 60 // Floor Light images for animation61 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 animation70 private static final String floorButtonFrames[] = { 71 "images/floorButtonUnpressed.png",72 "images/floorButtonPressed.png",73 "images/floorButtonLit.png" };74 75 // Elevator Button images for animation76 private static final String elevatorButtonFrames[] = {77 "images/elevatorButtonUnpressed.png",78 "images/elevatorButtonPressed.png",79 "images/elevatorButtonLit.png" };80 81 // Bell images for animation82 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

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView 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 files98 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 ceiling107 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

ImagePanels represent stationary objects in model (e.g., Floor, ElevatorShaft)

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView displays the elevator simulation model.

Line 114

Lines 117-124

Line 127

Lines 130-135

Line 138

113 // MovingPanels for Elevator114 private MovingPanel elevatorPanel;115 116 // AnimatedPanels for Buttons, Bell, Lights and Door117 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 objects127 private java.util.List personAnimatedPanels;128 129 // AudioClips for sound effects130 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 Elevator138 private AudioClip elevatorMusicClip;139

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

AudioClips for playing sound clips

elevatorMusicClip plays music when Person rides Elevator

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView 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 Floors144 private int firstFloorPosition;145 private int secondFloorPosition;146 147 // Elevator's velocity148 private double elevatorVelocity;149 150 // ElevatorView constructor151 public ElevatorView()152 {153 // specifiy null Layout154 super( null );155 156 instantiatePanels();157 placePanelsOnView();158 initializeAudio();159 160 // calculate distance Elevator travels161 double floorDistance = 162 firstFloorPosition - secondFloorPosition;163 164 // calculate time needed for travel165 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

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView displays the elevator simulation model.

Line 171

Lines 179-180

Line 191

Lines 194-195

169 170 // start animation Thread171 startAnimation();172 173 } // end ElevatorView constructor174 175 // instantiate all Panels (Floors, Elevator, etc.)176 private void instantiatePanels()177 {178 // instantiate ImagePanels representing Floors179 firstFloorPanel = new ImagePanel( 0, floorImage );180 secondFloorPanel = new ImagePanel( 0, floorImage );181 182 // calculate first and second Floor positions183 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 ElevatorShaft194 elevatorShaftPanel = 195 new ImagePanel( 0, elevatorShaftImage );196

Starting Timer starts animation

Instantiate ImagePanels for Floors

Instantiate ImagePanel for wall (not used in model)

Instantiate ImagePanel for ElevatorShaft

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView 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 ceiling204 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 Elevator212 elevatorPanel = new MovingPanel( 0, elevatorImage );213 214 yPosition = firstFloorPosition - elevatorPanel.getHeight();215 216 elevatorPanel.setPosition( xPosition, yPosition );217 218 // create and position first Floor Button219 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

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView 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 Button231 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 Lights242 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

AnimatedPanels use int arrays that determine

their image sequences

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView 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 AnimatedPanels258 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 Elevator265 yPosition = 266 elevatorPanel.getHeight() - doorPanel.getHeight();267 268 doorPanel.setPosition( 0, yPosition );269 270 // create and position Light AnimatedPanel271 elevatorLightPanel = new AnimatedPanel( 0, lightFrames );272 elevatorLightPanel.setPosition( OFFSET, 5 * OFFSET );273 274 // create and position Bell AnimatedPanel275 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 Doors on Floors, because they would

obscure Person when riding Elevator)

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView displays the elevator simulation model.

Lines 285-286

Line 296

Lines 305-314

283 284 // create and position Elevator's Button AnimatedPanel285 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 AnimatedPanels296 personAnimatedPanels = new ArrayList();297 298 } // end method instantiatePanels299 300 // place all Panels on ElevatorView301 private void placePanelsOnView()302 {303 // add Panels to ElevatorView304 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 ImagePanels to ElevatorView

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView displays the elevator simulation model.

Lines 316-319

Line 327

Lines 330-336

314 315 // add Panels to Elevator's MovingPanel316 elevatorPanel.add( doorPanel );317 elevatorPanel.add( elevatorLightPanel );318 elevatorPanel.add( bellPanel );319 elevatorPanel.add( elevatorButtonPanel );320 321 } // end method placePanelsOnView322 323 // get sound effects and elevatorMusic324 private void initializeAudio()325 {326 // create AudioClip sound effects from audio files327 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 initializeAudio339

Add ImagePanels for elevator’s door, light bell and button to the ImagePanel associated with Elevator

SoundEffects object creates AudioClips that play sound clips

Use SoundEffects object to obtain references

to AudioClips

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView 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 screen341 public void startAnimation()342 {343 if ( animationTimer == null ) {344 animationTimer = 345 new javax.swing.Timer( ANIMATION_DELAY, this );346 animationTimer.start();347 }348 else349 350 if ( !animationTimer.isRunning() )351 animationTimer.restart();352 }353 354 // stop animation355 public void stopAnimation()356 {357 animationTimer.stop();358 }359 360 // update AnimatedPanels animation in response to Timer361 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) millisecondsAnimate ImagePanels for Elevator and Floors

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView 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 Set373 AnimatedPanel personPanel = 374 ( AnimatedPanel ) iterator.next();375 376 personPanel.animate(); // update panel377 }378 379 repaint(); // paint all Components380 381 } // end method actionPerformed382 383 private Iterator getPersonAnimatedPanelsIterator()384 {385 // obtain iterator from List386 synchronized( personAnimatedPanels )387 {388 return new ArrayList( personAnimatedPanels ).iterator();389 }390 }391 392 // stop sound clip of Person walking393 private void stopWalkingSound()394 {395 // stop playing walking sound396 walkClip.stop();

Animate ImagePanels for Persons

Obtain the Iterator of the ArrayList of (Person)

AnimatedPanels

Stop sound clip played by Elevator-View when a Person walks

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView 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 playing401 while ( iterator.hasNext() ) {402 AnimatedPanel panel = ( AnimatedPanel ) iterator.next();403 404 if ( panel.getXVelocity() != 0 )405 walkClip.loop();406 }407 } // end method stopWalkingSound408 409 // returns Person AnimatedPanel with proper identifier410 private AnimatedPanel getPersonPanel( PersonMoveEvent event )411 {412 Iterator iterator = getPersonAnimatedPanelsIterator();413 414 while ( iterator.hasNext() ) {415 416 // get next AnimatedPanel417 AnimatedPanel personPanel = 418 ( AnimatedPanel ) iterator.next();419 420 // return AnimatedPanel with identifier that matches421 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

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView displays the elevator simulation model.

Line 431

Lines 439-471

425 // return null if no match with correct identifier426 return null;427 428 } // end method getPersonPanel429 430 // invoked when Elevator has departed from Floor431 public void elevatorDeparted( ElevatorMoveEvent moveEvent )432 {433 String location = 434 moveEvent.getLocation().getLocationName();435 436 // determine if Person is on Elevator437 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 entered448 if ( yPosition > secondFloorPosition )449 panelLocation = FIRST_FLOOR_NAME;450 else451 panelLocation = SECOND_FLOOR_NAME;452

Invoked when Elevator has departed from Floor

Determine whether Person is on Elevator

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView 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 Elevator457 if ( panelLocation.equals( location ) 458 && xPosition > PERSON_TO_BUTTON_DISTANCE + OFFSET ) {459 460 // remove Person AnimatedPanel from ElevatorView461 remove( personPanel );462 463 // add Person AnimatedPanel to Elevator464 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 loop472 473 // determine Elevator velocity depending on Floor474 if ( location.equals( FIRST_FLOOR_NAME ) )475 elevatorPanel.setVelocity( 0, -elevatorVelocity );476 else477 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

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView displays the elevator simulation model.

Lines 482-487

Line 492

Lines 495-496

Lines 502-509

481 // begin moving Elevator and play Elevator music482 elevatorPanel.setMoving( true );483 484 if ( elevatorClip != null )485 elevatorClip.play();486 487 elevatorMusicClip.play();488 489 } // end method elevatorDeparted490 491 // invoked when Elevator has arrived at destination Floor492 public void elevatorArrived( ElevatorMoveEvent moveEvent )493 {494 // stop Elevator and music495 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 Floor502 if ( elevatorPanel.getYVelocity() < 0 )503 yPosition = 504 secondFloorPosition - elevatorPanel.getHeight();505 else506 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

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView 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 model514 public void personCreated( PersonMoveEvent personEvent )515 {516 int personID = personEvent.getID();517 518 String floorLocation = 519 personEvent.getLocation().getLocationName();520 521 // create AnimatedPanel representing Person522 AnimatedPanel personPanel = 523 new AnimatedPanel( personID, personFrames );524 525 // determine where Person should be drawn initially526 // negative xPosition ensures Person drawn offscreen527 double xPosition = - personPanel.getWidth();528 double yPosition = 0;529 530 if ( floorLocation.equals( FIRST_FLOOR_NAME ) )531 yPosition = firstFloorPosition +532 ( firstFloorPanel.getHeight() / 2 );533 else534 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.”

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView displays the elevator simulation model.

Lines 544-549

Lines 552-555

Lines 558-563

542 543 // add some animations for each Person544 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 Elevator552 personPanel.playAnimation( 0 );553 personPanel.setLoop( true );554 personPanel.setAnimating( true );555 personPanel.setMoving( true );556 557 // determine Person velocity558 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 walking569

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

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView displays the elevator simulation model.

Line 573

Line 581

Line 584

Lines 589-592

570 // store in personAnimatedPanels571 synchronized( personAnimatedPanels )572 {573 personAnimatedPanels.add( personPanel );574 }575 576 add( personPanel, 0 );577 578 } // end method personCreated579 580 // invoked when Person has arrived at Elevator581 public void personArrived( PersonMoveEvent personEvent )582 {583 // find Panel associated with Person that issued event584 AnimatedPanel panel = getPersonPanel( personEvent );585 586 if ( panel != null ) { // if Person exists587 588 // Person stops at Floor Button589 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)

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView displays the elevator simulation model.

Line 603

Line 606

Lines 611-612

Line 622

Line 625

601 602 // invoked when Person has pressed Button603 public void personPressedButton( PersonMoveEvent personEvent )604 {605 // find Panel associated with Person that issued event606 AnimatedPanel panel = getPersonPanel( personEvent );607 608 if ( panel != null ) { // if Person exists609 610 // Person stops walking and presses Button611 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 personPressedButton620 621 // invoked when Person has started to enter Elevator622 public void personEntered( PersonMoveEvent personEvent )623 {624 // find Panel associated with Person that issued event625 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

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView displays the elevator simulation model.

Lines 627-642

Line 646

Line 649

Line 657

627 if ( panel != null ) {628 629 // determine velocity630 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 walking639 panel.setMoving( true );640 panel.playAnimation( 0 );641 panel.setLoop( true );642 }643 } // end method personEntered644 645 // invoked when Person has departed from Elevator646 public void personDeparted( PersonMoveEvent personEvent)647 {648 // find Panel associated with Person that issued event649 AnimatedPanel panel = getPersonPanel( personEvent );650 651 if ( panel != null ) { // if Person exists652 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

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView displays the elevator simulation model.

Lines 670-677

Line 684

658 659 // remove Person from Elevator660 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 exits670 if ( floorLocation.equals( FIRST_FLOOR_NAME ) )671 yPosition = firstFloorPosition +672 ( firstFloorPanel.getHeight() / 2 );673 else674 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 ElevatorView684 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

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView displays the elevator simulation model.

Lines 687-691

Line 696

Line 699

Line 709

686 // Person starts walking687 panel.setMoving( true );688 panel.setAnimating( true );689 panel.playAnimation( 2 );690 panel.setLoop( true );691 walkClip.loop();692 }693 } // end method PersonDeparted694 695 // invoked when Person has exited simulation696 public void personExited( PersonMoveEvent personEvent)697 {698 // find Panel associated with Person that issued moveEvent699 AnimatedPanel panel = getPersonPanel( personEvent );700 701 if ( panel != null ) { // if Person exists702 703 panel.setMoving( false );704 panel.setAnimating( false );705 706 // remove Person permanently and stop walking sound707 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

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView 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 model717 public void doorOpened( DoorEvent doorEvent )718 {719 // get DoorEvent Location720 String location = 721 doorEvent.getLocation().getLocationName();722 723 // play animation of Door opening724 doorPanel.playAnimation( 0 );725 doorPanel.setAnimationRate( 2 );726 doorPanel.setDisplayLastFrame( true );727 728 // play sound clip of Door opening729 if ( doorOpenClip != null )730 doorOpenClip.play();731 732 } // end method doorOpened733 734 // invoked when Door has closed in model735 public void doorClosed( DoorEvent doorEvent )736 {737 // get DoorEvent Location738 String location = 739 doorEvent.getLocation().getLocationName();740 741 // play animation of Door closing742 doorPanel.playAnimation( 1 );743 doorPanel.setAnimationRate( 2 );744 doorPanel.setDisplayLastFrame( true );

Invoked when Door has opened

Play animation of Door opening

Play sound effect of Door opening

Invoked when Door has closed

Play animation of Door closing

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView 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 closing747 if ( doorCloseClip != null )748 doorCloseClip.play();749 750 } // end method doorClosed751 752 // invoked when Button has been pressed in model753 public void buttonPressed( ButtonEvent buttonEvent )754 {755 // get ButtonEvent Location756 String location = 757 buttonEvent.getLocation().getLocationName();758 759 // press Elevator Button if from Elevator760 if ( location.equals( ELEVATOR_NAME ) ) {761 elevatorButtonPanel.playAnimation( 0 );762 elevatorButtonPanel.setDisplayLastFrame( true );763 }764 765 // press Floor Button if from Floor766 else767 768 if ( location.equals( FIRST_FLOOR_NAME ) ) {769 firstFloorButtonPanel.playAnimation( 0 );770 firstFloorButtonPanel.setDisplayLastFrame( true );771 }

Play sound effect of Door closing

Invoked 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

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView displays the elevator simulation model.

Lines 774-777

Line 785

Lines 788-789

Lines 792-799

772 else773 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 clip781 782 } // end method buttonPressed783 784 // invoked when Button has been reset in model785 public void buttonReset( ButtonEvent buttonEvent )786 {787 // get ButtonEvent Location788 String location = 789 buttonEvent.getLocation().getLocationName();790 791 // reset Elevator Button if from Elevator792 if ( location.equals( ELEVATOR_NAME ) ) {793 794 // return to first frame if still animating795 if ( elevatorButtonPanel.isAnimating() )796 elevatorButtonPanel.setDisplayLastFrame( false );797 else798 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

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView displays the elevator simulation model.

Lines 804-812

Lines 815-823

800 801 // reset Floor Button if from Floor802 else803 804 if ( location.equals( FIRST_FLOOR_NAME ) ) {805 806 // return to first frame if still animating807 if ( firstFloorButtonPanel.isAnimating() )808 firstFloorButtonPanel.setDisplayLastFrame( false );809 810 else811 firstFloorButtonPanel.setCurrentFrame( 0 );812 }813 else814 815 if ( location.equals( SECOND_FLOOR_NAME ) ) {816 817 // return to first frame if still animating818 if ( secondFloorButtonPanel.isAnimating() )819 secondFloorButtonPanel.setDisplayLastFrame( 820 false );821 else822 secondFloorButtonPanel.setCurrentFrame( 0 );823 }824 825 } // end method buttonReset826

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

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView 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 model828 public void bellRang( BellEvent bellEvent )829 {830 bellPanel.playAnimation( 0 ); // animate Bell831 832 if ( bellClip != null ) // play Bell sound clip833 bellClip.play();834 }835 836 // invoked when Light turned on in model837 public void lightTurnedOn( LightEvent lightEvent )838 {839 // turn on Light in Elevator840 elevatorLightPanel.setCurrentFrame( 1 );841 842 String location = 843 lightEvent.getLocation().getLocationName();844 845 // turn on Light on either first or second Floor846 if ( location.equals( FIRST_FLOOR_NAME ) )847 firstFloorLightPanel.setCurrentFrame( 1 );848 849 else850 851 if ( location.equals( SECOND_FLOOR_NAME ) )852 secondFloorLightPanel.setCurrentFrame( 1 );853 854 } // end method lightTurnedOn855

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

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView displays the elevator simulation model.

Line 857

Lines 866-867

Lines 871-872

Lines 877-880

856 // invoked when Light turned off in model857 public void lightTurnedOff( LightEvent lightEvent )858 {859 // turn off Light in Elevator860 elevatorLightPanel.setCurrentFrame( 0 );861 862 String location = 863 lightEvent.getLocation().getLocationName();864 865 // turn off Light on either first or second Floor866 if ( location.equals( FIRST_FLOOR_NAME ) )867 firstFloorLightPanel.setCurrentFrame( 0 );868 869 else870 871 if ( location.equals( SECOND_FLOOR_NAME ) )872 secondFloorLightPanel.setCurrentFrame( 0 );873 874 } // end method lightTurnedOff875 876 // return preferred size of ElevatorView877 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

2003 Prentice Hall, Inc.All rights reserved.

Outline

ElevatorView.javaElevatorView displays the elevator simulation model.

Lines 883-892

882 // return minimum size of ElevatorView883 public Dimension getMinimumSize()884 {885 return getPreferredSize();886 }887 888 // return maximum size of ElevatorView889 public Dimension getMaximumSize()890 {891 return getPreferredSize();892 }893 }

Set ElevatorView’s minimum and maximum sizes to

VIEW_WIDTH and VIEW_HEIGHT

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

2003 Prentice Hall, Inc. All rights reserved.

36

F.2 Class Objects (cont.)

The object (in model) of Class...

is represented by the object (in view)...

of Class...

Floor firstFloorPanel secondFloorPanel

ImagePanel ImagePanel

ElevatorShaft elevatorShaftPanel ImagePanel Elevator elevatorPanel MovingPanel Button (on Floor) firstFloorButtonPanel

secondFloorButtonPanel AnimatedPanel AnimatedPanel

Button (in Elevator) elevatorButtonPanel AnimatedPanel Bell bellPanel AnimatedPanel Light firstFloorLightPanel

secondFloorLightPanel AnimatedPanel AnimatedPanel

Door (in Elevator) doorPanel AnimatedPanel

Door (on Floor) <not represented> <not represented> Person personAnimatedPanels List (of

AnimatedPanels) Fig. F.2 Objects in the ElevatorView representing objects in the model.

2003 Prentice Hall, Inc. All rights reserved.

37

F.2 Class Objects (cont.)

The object (in model) of Class...

is represented by the object (in view)...

of Class...

<not represented> elevatorLightPanel AnimatedPanel <not represented> ceilingPanel ImagePanel <not represented> wallPanel ImagePanel Fig. F.3 Objects in the ElevatorView not represented in the model.

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 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

2003 Prentice Hall, Inc. All rights reserved.

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

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 : AnimatedPanelelevatorShaftPanel : 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

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

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

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

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

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

2003 Prentice Hall, Inc. All rights reserved.

46

F.5.5 BellEvent types

• ElevatorSimulation– Sends BellEvent when Bell rung

• Invoking method bellRang

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

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

2003 Prentice Hall, Inc. All rights reserved.

49F.6 Component Diagrams Revisited (cont.)

Fig F.5 Component diagram for package view.

view

ElevatorView.java

<<file>>

ImagePanel.java

<<file>>

SoundEffects.java

<<file>>

AnimatedPanel.java

<<file>>

MovingPanel.java

<<file>>

images sounds event1

11

<<imports>><<imports>>

<<imports>>

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