Appendix F Elevator View Outline F 1 F

  • Slides: 50
Download presentation
Appendix F – Elevator View Outline F. 1 F. 2 F. 3 F. 4

Appendix F – Elevator View Outline F. 1 F. 2 F. 3 F. 4 F. 5 F. 6 F. 7 Introduction Class Objects Class Constants Class Constructor Event Handling F. 5. 1 Elevator. Move. Event types F. 5. 2 Person. Move. Event types F. 5. 3 Door. Event types F. 5. 4 Button. Event types F. 5. 5 Bell. Event types F. 5. 6 Light. Event types Artifacts Revisited Conclusion 2003 Prentice Hall, Inc. All rights reserved. 1

2 F. 1 Introduction • Class Elevator. View – Graphical representation of elevator-simulation model

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 // Elevator. View. java // View for Elevator. Simulation package com. deitel. jhtp 5. elevator. view; Outline // Java core packages import java. awt. *; import java. awt. event. *; import java. util. *; import java. applet. *; Elevator. View. ja Elevator. View implements va Elevator. Simulation. Listener, Elevator. View which inherits from all listener interfaces // Deitel packages displays the elevator import com. deitel. jhtp 5. elevator. event. *; simulation model. import com. deitel. jhtp 5. elevator. Elevator. Constants; // Java extension package import javax. swing. *; public class Elevator. View extends JPanel implements Action. Listener, Elevator. Simulation. Listener, Elevator. Constants { // Elevator. View dimensions private static final int VIEW_WIDTH = 800; private static final int VIEW_HEIGHT = 435; Lines 18 -20 Lines 23 -24 Constants for width and height of Elevator. View // offset for positioning Panels in Elevator. View private static final int OFFSET = 10; 2003 Prentice Hall, Inc. All rights reserved.

28 29 30 31 32 33 34 35 36 37 38 39 40 41

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 // Elevator repaints components every 50 ms private static final int ANIMATION_DELAY = 50; Outline Constant for animation (refresh) rate // horizontal distance constants private static final int PERSON_TO_BUTTON_DISTANCE = 400; private static final int BUTTON_TO_ELEVATOR_DISTANCE = 50; private static final int PERSON_TO_ELEVATOR_DISTANCE = PERSON_TO_BUTTON_DISTANCE + BUTTON_TO_ELEVATOR_DISTANCE; // times walking to Floor's Button and Elevator private static final int TIME_TO_BUTTON = 3000; // 3 seconds private static final int TIME_TO_ELEVATOR = 1000; // 1 second // time traveling in Elevator (5 seconds) private static final int ELEVATOR_TRAVEL_TIME = 5000; // Door images for animation private static final String door. Frames[] = { "images/door 1. png", "images/door 2. png", "images/door 3. png", "images/door 4. png", "images/door 5. png" }; // Person images for animation private static final String person. Frames[] = { "images/bug 1. png", "images/bug 2. png", "images/bug 3. png", "images/bug 4. png", "images/bug 5. png", "images/bug 6. png", "images/bug 7. png", "images/bug 8. png" }; Constants for distances that Person must travel Elevator. View. ja va Time constants for Elevator. View distances Person displays the elevator travels simulation model. Line 30 Constant for time Lines 33 -36 required to travel between Floors Lines 39 -40 Constants for names of Linefiles 43 for Door graphics and Person Lines 46 -54 2003 Prentice Hall, Inc. All rights reserved.

56 57 58 59 60 61 62 63 64 65 66 67 68 69

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 // Light images for animation private static final String light. Frames[] = { "images/light. Off. png", "images/light. On. png" }; // Floor Light images for animation private static final String first. Floor. Light. Frames[] = { "images/first. Floor. Light. Off. png", "images/first. Floor. Light. On. png" }; private static final String second. Floor. Light. Frames[] = { "images/second. Floor. Light. Off. png", "images/second. Floor. Light. On. png", }; // Floor Button images for animation private static final String floor. Button. Frames[] = { "images/floor. Button. Unpressed. png", "images/floor. Button. Pressed. png", "images/floor. Button. Lit. png" }; // Elevator Button images for animation private static final String elevator. Button. Frames[] = { "images/elevator. Button. Unpressed. png", "images/elevator. Button. Pressed. png", "images/elevator. Button. Lit. png" }; Outline Constants for names of graphics files for Light, Button and Bell Elevator. View. ja va Elevator. View displays the elevator simulation model. Lines 57 -84 // Bell images for animation private static final String bell. Frames[] = { "images/bell 1. png", "images/bell 2. png", "images/bell 3. png" }; 2003 Prentice Hall, Inc. All rights reserved.

85 86 87 88 89 90 91 92 93 94 95 96 97 98

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 Outline private static final String floor. Image = "images/floor. png"; private static final String ceiling. Image = "images/ceiling. png"; private static final String elevator. Image = "images/elevator. png"; private static final String wall. Image = "images/wall. jpg"; private static final String elevator. Shaft. Image = "images/elevator. Shaft. png"; // audio files private static private static final final // Image. Panels for private Image. Panel String String Ceiling and wall are not in model, but we display them for realism bell. Sound = "bell. wav"; door. Open. Sound = "door. Open. wav"; door. Close. Sound = "door. Close. wav"; elevator. Sound = "elevator. au"; button. Sound = "button. wav"; walking. Sound = "walk. wav"; elevator. Music. Sound = "liszt. mid"; Floors, Elevator. Shaft, wall and first. Floor. Panel; second. Floor. Panel; elevator. Shaft. Panel; wall. Panel; ceiling. Panel; Elevator. View. ja va Elevator. View Constants for displays names the elevator of sound-clipsimulation files model. Lines 88 -89 and 92 -93 Constant for name Lines 98 -103 of “elevator music” file ceiling Image. Panels represent Line 104 stationary objects in model (e. g. , Lines 107 -111 Floor, Elevator. Shaft) 2003 Prentice Hall, Inc. All rights reserved.

113 114 115 116 117 118 119 120 121 122 123 124 125 126

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 // Moving. Panels for Elevator private Moving. Panel elevator. Panel; // Animated. Panels for private Animated. Panel private Animated. Panel Buttons, Bell, Lights and Door first. Floor. Button. Panel; second. Floor. Button. Panel; elevator. Button. Panel; bell. Panel; elevator. Light. Panel; first. Floor. Light. Panel; second. Floor. Light. Panel; door. Panel; // List containing Animated. Panels for all Person objects private java. util. List person. Animated. Panels; // Audio. Clips for private Audio. Clip sound effects bell. Clip; door. Open. Clip; door. Close. Clip; elevator. Clip; button. Clip; walk. Clip; // Elevator. Music to play in Elevator private Audio. Clip elevator. Music. Clip; Outline Moving. Panels represent objects that can move and have only one associated Animated. Panels represent image (e. g. , Elevator) objects in model with multiple images (e. g. , Button, Person, Light, Bell Elevator. View. ja and Door) va Elevator. View displays the elevator simulation model. List stores Animated. Panels Line 114 associated with Persons Audio. Clips for playing sound Linesclips 117 -124 Line 127 Lines 130 -135 elevator. Music. Clip plays music Line 138 when Person rides Elevator 2003 Prentice Hall, Inc. All rights reserved.

140 141 142 143 144 145 146 147 148 149 150 151 152 153

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 // Timer for animation controller; private javax. swing. Timer animation. Timer; // distance from top of screen to display Floors private int first. Floor. Position; private int second. Floor. Position; Outline Timer determines when to redraw images // Elevator's velocity private double elevator. Velocity; // Elevator. View constructor public Elevator. View() { // specifiy null Layout super( null ); instantiate. Panels(); place. Panels. On. View(); initialize. Audio(); Elevator. View. ja va Elevator. View Using null layout displays the elevator allows us to display simulation model. images anywhere on Elevator. View Line 141 Line 154 // calculate distance Elevator travels double floor. Distance = first. Floor. Position - second. Floor. Position; // calculate time needed for travel double time = ELEVATOR_TRAVEL_TIME / ANIMATION_DELAY; // determine Elevator velocity (rate = distance / time) elevator. Velocity = ( floor. Distance + OFFSET ) / time; Lines 165 -168 Calculate velocity used by Elevator’s Image. Panel 2003 Prentice Hall, Inc. All rights reserved.

169 170 171 172 173 174 175 176 177 178 179 180 181 182

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 Outline // start animation Thread start. Animation(); Starting Timer starts animation } // end Elevator. View constructor // instantiate all Panels (Floors, Elevator, etc. ) private void instantiate. Panels() { // instantiate Image. Panels representing Floors first. Floor. Panel = new Image. Panel( 0, floor. Image ); second. Floor. Panel = new Image. Panel( 0, floor. Image ); Elevator. View. ja Instantiate Image. Panels va for Floors Elevator. View displays the elevator simulation model. // calculate first and second Floor positions first. Floor. Position = VIEW_HEIGHT - first. Floor. Panel. get. Height(); second. Floor. Position = ( int ) ( first. Floor. Position / 2 ) - OFFSET; Line 171 first. Floor. Panel. set. Position( 0, first. Floor. Position ); second. Floor. Panel. set. Position( 0, second. Floor. Position ); wall. Panel = new Image. Panel( 0, wall. Image ); // create and position Image. Panel for Elevator. Shaft elevator. Shaft. Panel = new Image. Panel( 0, elevator. Shaft. Image ); Lines 179 -180 Line 191 Instantiate Image. Panel for wall (not used in model) Lines 194 -195 Instantiate Image. Panel for Elevator. Shaft 2003 Prentice Hall, Inc. All rights reserved.

197 198 199 200 201 202 203 204 205 206 207 208 209 210

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 Outline double x. Position = PERSON_TO_ELEVATOR_DISTANCE + OFFSET; double y. Position = first. Floor. Position - elevator. Shaft. Panel. get. Height(); elevator. Shaft. Panel. set. Position( x. Position, y. Position ); // create and position Image. Panel for ceiling. Panel = new Image. Panel( 0, ceiling. Image ); y. Position = elevator. Shaft. Panel. get. Position(). get. Y() ceiling. Panel. get. Height(); ceiling. Panel. set. Position( x. Position, y. Position ); // create and position Moving. Panel for Elevator elevator. Panel = new Moving. Panel( 0, elevator. Image ); Instantiate Image. Panel for ceiling (not used in model) Elevator. View. ja va Elevator. View displays the elevator Instantiate simulation model. Moving. Panel for Elevator Line 204 y. Position = first. Floor. Position - elevator. Panel. get. Height(); elevator. Panel. set. Position( x. Position, y. Position ); // create and position first Floor Button first. Floor. Button. Panel = new Animated. Panel( 0, floor. Button. Frames ); Line 212 Lines 219 -220 Instantiate Animated. Panel for Button on first Floor x. Position = PERSON_TO_BUTTON_DISTANCE + 2 * OFFSET; y. Position = first. Floor. Position - 5 * OFFSET; first. Floor. Button. Panel. set. Position( x. Position, y. Position ); 2003 Prentice Hall, Inc. All rights reserved.

225 226 227 228 229 230 231 232 233 234 235 236 237 238

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 Outline int floor. Button. Pressed. Frame. Order[] = { 0, 1, 2 }; first. Floor. Button. Panel. add. Frame. Sequence( floor. Button. Pressed. Frame. Order ); Animated. Panels use int arrays that determine their image sequences // create and position second Floor Button Instantiate Animated. Panel second. Floor. Button. Panel = for Button on second Floor new Animated. Panel( 1, floor. Button. Frames ); Elevator. View. ja va x. Position = PERSON_TO_BUTTON_DISTANCE + 2 * OFFSET; y. Position = second. Floor. Position - 5 * OFFSET; Elevator. View second. Floor. Button. Panel. set. Position( x. Position, y. Position ); displays the elevator simulation model. second. Floor. Button. Panel. add. Frame. Sequence( floor. Button. Pressed. Frame. Order ); Instantiate Animated. Panel Lines 226 -228 for Light on first Floor Lines 231 -232 // create and position Floor Lights first. Floor. Light. Panel = new Animated. Panel( 0, first. Floor. Light. Frames ); x. Position = elevator. Panel. get. Location(). x - 4 * OFFSET; y. Position = first. Floor. Button. Panel. get. Location(). y - 10 * OFFSET; first. Floor. Light. Panel. set. Position( x. Position, y. Position ); second. Floor. Light. Panel = new Animated. Panel( 1, second. Floor. Light. Frames ); Lines 242 -243 Lines 250 -251 Instantiate Animated. Panel for Light on second Floor 2003 Prentice Hall, Inc. All rights reserved.

253 254 255 256 257 258 259 260 261 262 263 264 265 266

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 y. Position = second. Floor. Button. Panel. get. Location(). y - 10 * OFFSET; second. Floor. Light. Panel. set. Position( x. Position, y. Position ); // create and position Door Animated. Panels door. Panel = new Animated. Panel( 0, door. Frames ); int door. Opened. Frame. Order[] = { 0, 1, 2, 3, 4 }; int door. Closed. Frame. Order[] = { 4, 3, 2, 1, 0 }; door. Panel. add. Frame. Sequence( door. Opened. Frame. Order ); door. Panel. add. Frame. Sequence( door. Closed. Frame. Order ); // determine where Door is located relative to Elevator y. Position = elevator. Panel. get. Height() - door. Panel. get. Height(); door. Panel. set. Position( 0, y. Position ); // create and position Light Animated. Panel elevator. Light. Panel = new Animated. Panel( 0, light. Frames ); elevator. Light. Panel. set. Position( OFFSET, 5 * OFFSET ); // create and position Bell Animated. Panel bell. Panel = new Animated. Panel( 0, bell. Frames ); Outline Instantiate Animated. Panel for Door in Elevator (Note: we do not show Doors on Floors, because they would obscure Person when Elevator. View. ja riding Elevator) va Elevator. View displays the elevator simulation model. Instantiate Animated. Panel for Light inside Elevator Line 258 Line 271 Instantiate Animated. Panel Line 275 for Bell inside Elevator y. Position = elevator. Light. Panel. get. Position(). get. Y() + elevator. Light. Panel. get. Height() + OFFSET; bell. Panel. set. Position( OFFSET, y. Position ); int bell. Ring. Animation[] = { 0, 1, 0, 2 }; bell. Panel. add. Frame. Sequence( bell. Ring. Animation ); 2003 Prentice Hall, Inc. All rights reserved.

283 284 285 286 287 288 289 290 291 292 293 294 295 296

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 Outline // create and position Elevator's Button Animated. Panel Instantiate Animated. Panel elevator. Button. Panel = new Animated. Panel( 0, elevator. Button. Frames ); for Button inside Elevator y. Position = elevator. Panel. get. Height() - 6 * OFFSET; elevator. Button. Panel. set. Position( 10 * OFFSET, y. Position ); int button. Pressed. Frame. Order[] = { 0, 1, 2 }; elevator. Button. Panel. add. Frame. Sequence( button. Pressed. Frame. Order ); // create List to store Person Animated. Panels person. Animated. Panels = new Array. List(); } // end method instantiate. Panels // place all Panels on Elevator. View private void place. Panels. On. View() { // add Panels to Elevator. View add( first. Floor. Panel ); add( second. Floor. Panel ); add( ceiling. Panel ); add( elevator. Panel ); add( first. Floor. Button. Panel ); add( second. Floor. Button. Panel ); add( first. Floor. Light. Panel ); add( second. Floor. Light. Panel ); add( elevator. Shaft. Panel ); add( wall. Panel ); Elevator. View. ja va Instantiate Array. List to store Elevator. View references to Animated. Panel’s the elevator associated with displays Person’s simulation model. Lines 285 -286 Line 296 Add Image. Panels to Elevator. View Lines 305 -314 2003 Prentice Hall, Inc. All rights reserved.

314 315 316 317 318 319 320 321 322 323 324 325 326 327

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 // add Panels to Elevator's Moving. Panel elevator. Panel. add( door. Panel ); elevator. Panel. add( elevator. Light. Panel ); elevator. Panel. add( bell. Panel ); elevator. Panel. add( elevator. Button. Panel ); Outline Add Image. Panels for elevator’s door, light bell and button to the Image. Panel associated with Elevator } // end method place. Panels. On. View // get sound effects and elevator. Music private void initialize. Audio() { // create Audio. Clip sound effects from audio files Sound. Effects sounds = new Sound. Effects(); sounds. set. Path. Prefix( "sounds/" ); Elevator. View. ja va Sound. Effects. Elevator. View object creates Audio. Clips displays the elevator that play sound simulation clips model. bell. Clip = sounds. get. Audio. Clip( bell. Sound ); door. Open. Clip = sounds. get. Audio. Clip( door. Open. Sound ); Lines 316 -319 door. Close. Clip = sounds. get. Audio. Clip( door. Close. Sound ); Use Sound. Effects elevator. Clip = sounds. get. Audio. Clip( elevator. Sound ); object to obtain Line references 327 button. Clip = sounds. get. Audio. Clip( button. Sound ); to Audio. Clips walk. Clip = sounds. get. Audio. Clip( walking. Sound ); elevator. Music. Clip = sounds. get. Audio. Clip( elevator. Music. Sound ); Lines 330 -336 } // end method initialize. Audio 2003 Prentice Hall, Inc. All rights reserved.

340 341 342 343 344 345 346 347 348 349 350 351 352 353

340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 Outline // starts animation by repeatedly drawing images to screen public void start. Animation() { if ( animation. Timer == null ) { Method start. Animation starts animation. Timer = Timer, which starts animation new javax. swing. Timer( ANIMATION_DELAY, this ); animation. Timer. start(); } else Elevator. View. ja va Elevator. View displays the elevator simulation model. Method stop. Animation stops Timer, which stops animation Lines 341 -352 if ( !animation. Timer. is. Running() ) animation. Timer. restart(); } // stop animation public void stop. Animation() { animation. Timer. stop(); } Lines 355 -358 // update Animated. Panels animation in response to Timer public void action. Performed( Action. Event action. Event ) { elevator. Panel. animate(); first. Floor. Button. Panel. animate(); second. Floor. Button. Panel. animate(); Lines 361 -381 Timer invokes Lines method 363 -366 action. Performed every 50 Animate Image. Panels for (ANIMATION_DELAY) milliseconds Elevator and Floors 2003 Prentice Hall, Inc. All rights reserved.

368 369 370 371 372 373 374 375 376 377 378 379 380 381

368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 Outline Iterator iterator = get. Person. Animated. Panels. Iterator(); while ( iterator. has. Next() ) { // get Person's Animated. Panel from Set Animated. Panel person. Panel = ( Animated. Panel ) iterator. next(); person. Panel. animate(); // update panel } Animate Image. Panels for Persons repaint(); // paint all Components } // end method action. Performed private Iterator get. Person. Animated. Panels. Iterator() { // obtain iterator from List synchronized( person. Animated. Panels ) { return new Array. List( person. Animated. Panels ). iterator(); } } // stop sound clip of Person walking private void stop. Walking. Sound() { // stop playing walking sound walk. Clip. stop(); Elevator. View. ja va Elevator. View displays the elevator simulation model. Lines 370 -377 Obtain the Iterator of the Line 388 of (Person) Array. List Animated. Panels Lines 393 -407 Stop sound clip played by Elevator. View when a Person walks 2003 Prentice Hall, Inc. All rights reserved.

397 398 399 400 401 402 403 404 405 406 407 408 409 410

397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 Outline Iterator iterator = get. Person. Animated. Panels. Iterator(); // but if Person is still walking, then keep playing while ( iterator. has. Next() ) { Animated. Panel panel = ( Animated. Panel ) iterator. next(); if ( panel. get. XVelocity() != 0 ) walk. Clip. loop(); } } // end method stop. Walking. Sound If a Person is still walking, continue the sound clip // returns Person Animated. Panel with proper identifier private Animated. Panel get. Person. Panel( Person. Move. Event event ) { Iterator iterator = get. Person. Animated. Panels. Iterator(); while ( iterator. has. Next() ) { // get next Animated. Panel person. Panel = ( Animated. Panel ) iterator. next(); Elevator. View. ja va Elevator. View displays the elevator simulation model. Obtain Animated. Panel associated with Person that Lines 401 -406 sent the Person. Move. Event Lines 410 -428 // return Animated. Panel with identifier that matches if ( person. Panel. get. ID() == event. get. ID() ) return person. Panel; } 2003 Prentice Hall, Inc. All rights reserved.

425 426 427 428 429 430 431 432 433 434 435 436 437 438

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 // return null if no match with correct identifier return null; } // end method get. Person. Panel Outline Invoked when Elevator has departed from Floor // invoked when Elevator has departed from Floor public void elevator. Departed( Elevator. Move. Event move. Event ) { String location = move. Event. get. Location(). get. Location. Name(); // determine if Person is on Elevator Iterator iterator = get. Person. Animated. Panels. Iterator(); while ( iterator. has. Next() ) { Animated. Panel person. Panel = ( Animated. Panel ) iterator. next(); Elevator. View. ja va Determine whether Person Elevator. View is on Elevator displays the elevator simulation model. double y. Position = person. Panel. get. Position(). get. Y(); String panel. Location; Line 431 Lines 439 -471 // determine on which Floor the Person entered if ( y. Position > second. Floor. Position ) panel. Location = FIRST_FLOOR_NAME; else panel. Location = SECOND_FLOOR_NAME; 2003 Prentice Hall, Inc. All rights reserved.

453 454 455 456 457 458 459 460 461 462 463 464 465 466

453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 int x. Position = ( int ) person. Panel. get. Position(). get. X(); Outline // if Person is inside Elevator if ( panel. Location. equals( location ) && x. Position > PERSON_TO_BUTTON_DISTANCE + OFFSET ) { // remove Person Animated. Panel from Elevator. View remove( person. Panel ); // add Person Animated. Panel to Elevator. View. ja elevator. Panel. add( person. Panel, 1 ); va person. Panel. set. Location( 2 * OFFSET, 9 * OFFSET ); Elevator. View person. Panel. set. Moving( false ); If Person is inside Elevator, person. Panel. set. Animating( false ); displays the elevator remove Person from Elevatorperson. Panel. set. Velocity( 0, 0 ); simulation model. View and add Person to Elevator person. Panel. set. Current. Frame( 1 ); } } // end while loop // determine Elevator velocity depending on Floor if ( location. equals( FIRST_FLOOR_NAME ) ) elevator. Panel. set. Velocity( 0, -elevator. Velocity ); else if ( location. equals( SECOND_FLOOR_NAME ) ) elevator. Panel. set. Velocity( 0, elevator. Velocity ); Lines 457 -470 Lines 474 -479 Determine Elevator velocity depending on Floor 2003 Prentice Hall, Inc. All rights reserved.

481 482 483 484 485 486 487 488 489 490 491 492 493 494

481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 // begin moving Elevator and play Elevator music elevator. Panel. set. Moving( true ); if ( elevator. Clip != null ) elevator. Clip. play(); Outline Set Elevator to moving state, then play sound effect and music associated with the Elevator’s movement elevator. Music. Clip. play(); } // end method elevator. Departed Elevator. View. ja va Invoked when Elevator. View Elevator has displays the elevator arrived at Floor simulation model. // invoked when Elevator has arrived at destination Floor public void elevator. Arrived( Elevator. Move. Event move. Event ) { // stop Elevator and music elevator. Panel. set. Moving( false ); elevator. Music. Clip. stop(); double x. Position = elevator. Panel. get. Position(). get. X(); double y. Position; Lines 482 -487 Set Elevator to waiting state and stop music associated with Line 492 the Elevator’s movement // set Elevator's position to either first or second Floor if ( elevator. Panel. get. YVelocity() < 0 ) y. Position = second. Floor. Position - elevator. Panel. get. Height(); else y. Position = first. Floor. Position - elevator. Panel. get. Height(); elevator. Panel. set. Position( x. Position, y. Position ); Lines 495 -496 Lines 502 -509 Set Elevator’s ycoordinate to that of the Floor on which the Elevator arrived } // end method elevator. Arrived 2003 Prentice Hall, Inc. All rights reserved.

512 513 514 515 516 517 518 519 520 521 522 523 524 525

512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 // invoked when Person has been created in model public void person. Created( Person. Move. Event person. Event ) { int person. ID = person. Event. get. ID(); String floor. Location = person. Event. get. Location(). get. Location. Name(); Invoked when user Outline creates Person in simulation Determine which Person was created and on what Floor // create Animated. Panel representing Person Animated. Panel person. Panel = new Animated. Panel( person. ID, person. Frames ); Create Animated. Panel to Elevator. View. ja represent Person in view va Elevator. View // determine where Person should be drawn initially displays the elevator // negative x. Position ensures Person drawn offscreen simulation model. double x. Position = - person. Panel. get. Width(); Position Person’s double y. Position = 0; Line 514 outside Animated. Panel if ( floor. Location. equals( FIRST_FLOOR_NAME ) ) the view, so the Person y. Position = first. Floor. Position + Lines 516 -519 does not suddenly “appear. ” ( first. Floor. Panel. get. Height() / 2 ); else if ( floor. Location. equals( SECOND_FLOOR_NAME ) ) y. Position = second. Floor. Position + ( second. Floor. Panel. get. Height() / 2 ); Lines 522 -523 Lines 530 -541 y. Position -= person. Panel. get. Height(); person. Panel. set. Position( x. Position, y. Position ); 2003 Prentice Hall, Inc. All rights reserved.

542 543 544 545 546 547 548 549 550 551 552 553 554 555

542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 // add some animations for each Person int walk. Frame. Order[] = { 1, 0, 1, 2 }; int press. Button. Frame. Order[] = { 1, 3, 3, 4, 4, 1 }; int walk. Away. Frame. Order[] = { 6, 5, 6, 7 }; person. Panel. add. Frame. Sequence( walk. Frame. Order ); person. Panel. add. Frame. Sequence( press. Button. Frame. Order ); person. Panel. add. Frame. Sequence( walk. Away. Frame. Order ); // have Person begin walking to Elevator person. Panel. play. Animation( 0 ); person. Panel. set. Loop( true ); person. Panel. set. Animating( true ); person. Panel. set. Moving( true ); // determine Person velocity double time = ( double ) ( TIME_TO_BUTTON / ANIMATION_DELAY ); double x. Distance = PERSON_TO_BUTTON_DISTANCE 2 * OFFSET + person. Panel. get. Size(). width; double x. Velocity = x. Distance / time; Outline Add animation sequences (e. g. , walking and pressing buttons) for the Person Set the Person’s Elevator. View. ja Animated. Panel to va its moving state (i. e. , Elevator. View walking to Elevator) displays the elevator simulation model. Lines 544 -549 Linesvelocity 552 -555 Calculate Person’s and distance to Elevator Lines 558 -563 person. Panel. set. Velocity( x. Velocity, 0 ); person. Panel. set. Animation. Rate( 1 ); walk. Clip. loop(); // play sound clip of Person walking 2003 Prentice Hall, Inc. All rights reserved.

570 571 572 573 574 575 576 577 578 579 580 581 582 583

570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 // store in person. Animated. Panels synchronized( person. Animated. Panels ) { person. Animated. Panels. add( person. Panel ); } Outline Add Person’s Animated. Panel to Elevator. View add( person. Panel, 0 ); } // end method person. Created // invoked when Person has arrived at Elevator public void person. Arrived( Person. Move. Event person. Event ) { // find Panel associated with Person that issued event Animated. Panel panel = get. Person. Panel( person. Event ); if ( panel != null ) { // if Person exists // Person stops at Floor Button panel. set. Moving( false ); panel. set. Animating( false ); panel. set. Current. Frame( 1 ); stop. Walking. Sound(); double x. Position = PERSON_TO_BUTTON_DISTANCE ( panel. get. Size(). width / 2 ); double y. Position = panel. get. Position(). get. Y(); Elevator. View. ja Invoked when va Person has arrived at. Elevator. View Elevator displays the elevator simulation model. Find Animated. Panel associated with Person that Line 573 issued event Line 581 Set Person’s Animated. Panel to waiting state Line 584 (waiting for at Elevator) Lines 589 -592 panel. set. Position( x. Position, y. Position ); } } // end method person. Arrived 2003 Prentice Hall, Inc. All rights reserved.

601 602 603 604 605 606 607 608 609 610 611 612 613 614

601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 // invoked when Person has pressed Button public void person. Pressed. Button( Person. Move. Event person. Event ) { // find Panel associated with Person that issued event Animated. Panel panel = get. Person. Panel( person. Event ); if ( panel != null ) { // if Person exists // Person stops walking and presses Button panel. set. Loop( false ); panel. play. Animation( 1 ); panel. set. Velocity( 0, 0 ); panel. set. Moving( false ); panel. set. Animating( true ); stop. Walking. Sound(); } } // end method person. Pressed. Button // invoked when Person has started to enter Elevator public void person. Entered( Person. Move. Event person. Event ) { // find Panel associated with Person that issued event Animated. Panel panel = get. Person. Panel( person. Event ); Invoked when Person Outline is pressing Button Find Animated. Panel associated with Person that issued event Elevator. View. ja va Start animation of Person Elevator. View pressing Button displays the elevator simulation model. Line 603 Line 606 Lines 611 -612 Invoked when Person is entering Elevator Line 622 Find Animated. Panel 625 that associated with. Line Person issued event 2003 Prentice Hall, Inc. All rights reserved.

627 628 629 630 631 632 633 634 635 636 637 638 639 640

627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 if ( panel != null ) { Outline // determine velocity double time = TIME_TO_ELEVATOR / ANIMATION_DELAY; double distance = elevator. Panel. get. Position(). get. X() panel. get. Position(). get. X() + 2 * OFFSET; panel. set. Velocity( distance / time, -1. 5 ); // Person starts walking panel. set. Moving( true ); panel. play. Animation( 0 ); panel. set. Loop( true ); } } // end method person. Entered Determine Person’s velocity when entering Elevator, then set Person’s Animated. Elevator. View. ja Panel to moving va state Elevator. View displays the elevator simulation model. // invoked when Person has departed from Elevator public void person. Departed( Person. Move. Event person. Event) { // find Panel associated with Person that issued event Animated. Panel panel = get. Person. Panel( person. Event ); if ( panel != null ) { // if Person exists Lines 627 -642 Invoked when Person 646 has exited. Line Elevator Line 649 Find Animated. Panel associated with Person that Line 657 issued event // determine velocity (in opposite direction) double time = TIME_TO_BUTTON / ANIMATION_DELAY; double x. Velocity = - PERSON_TO_BUTTON_DISTANCE / time; panel. set. Velocity( x. Velocity, 0 ); Set Person velocity to the opposite of that when 2003 Prentice Hall, Inc. the Person was created All rights reserved.

658 659 660 661 662 663 664 665 666 667 668 669 670 671

658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 Outline // remove Person from Elevator elevator. Panel. remove( panel ); double x. Position = PERSON_TO_ELEVATOR_DISTANCE + 3 * OFFSET; double y. Position = 0; String floor. Location = person. Event. get. Location(). get. Location. Name(); Elevator. View. ja va Elevator. View Set Person’s y-coordinate to displaysonthe elevator that of the Floor which the simulation model. Elevator is located // determine Floor onto which Person exits if ( floor. Location. equals( FIRST_FLOOR_NAME ) ) y. Position = first. Floor. Position + ( first. Floor. Panel. get. Height() / 2 ); else if ( floor. Location. equals( SECOND_FLOOR_NAME ) ) y. Position = second. Floor. Position + ( second. Floor. Panel. get. Height() / 2 ); Lines 670 -677 Line 684 y. Position -= panel. get. Height(); panel. set. Position( x. Position, y. Position ); // add Person to Elevator. View add( panel, 0 ); Add Person’s Animated. Panel to Elevator. View 2003 Prentice Hall, Inc. All rights reserved.

686 687 688 689 690 691 692 693 694 695 696 697 698 699

686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 // Person starts walking panel. set. Moving( true ); panel. set. Animating( true ); panel. play. Animation( 2 ); panel. set. Loop( true ); walk. Clip. loop(); Outline Set Person’s Animated. Panel to moving state } } // end method Person. Departed // invoked when Person has exited simulation public void person. Exited( Person. Move. Event person. Event) { // find Panel associated with Person that issued move. Event Animated. Panel panel = get. Person. Panel( person. Event ); if ( panel != null ) { // if Person exists panel. set. Moving( false ); panel. set. Animating( false ); // remove Person permanently and stop walking sound synchronized( person. Animated. Panels ) { person. Animated. Panels. remove( panel ); } remove( panel ); stop. Walking. Sound(); Elevator. View. ja va Invoked when Person Elevator. View has exited simulation displays the elevator simulation model. Find Panel associated with Person that issued move. Event Lines 687 -691 Line 696 Line 699 Remove Person’s Line 709 Animated. Panel from Elevator. View } } // end method person. Exited 2003 Prentice Hall, Inc. All rights reserved.

715 716 717 718 719 720 721 722 723 724 725 726 727 728

715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 // invoked when Door has opened in model public void door. Opened( Door. Event door. Event ) { // get Door. Event Location String location = door. Event. get. Location(). get. Location. Name(); // play animation of Door opening door. Panel. play. Animation( 0 ); door. Panel. set. Animation. Rate( 2 ); door. Panel. set. Display. Last. Frame( true ); // play sound clip of Door opening if ( door. Open. Clip != null ) door. Open. Clip. play(); } // end method door. Opened // invoked when Door has closed in model public void door. Closed( Door. Event door. Event ) { // get Door. Event Location String location = door. Event. get. Location(). get. Location. Name(); // play animation of Door closing door. Panel. play. Animation( 1 ); door. Panel. set. Animation. Rate( 2 ); door. Panel. set. Display. Last. Frame( true ); Outline Invoked when Door has opened Elevator. View. ja va Play animation of Door opening Elevator. View displays the elevator simulation model. Play sound effect of Door Lineopening 717 Lines 724 -726 Lines 729 -730 Invoked when Door has closed Line 739 Lines 742 -744 Play animation of Door closing 2003 Prentice Hall, Inc. All rights reserved.

745 746 747 748 749 750 751 752 753 754 755 756 757 758

745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 // play sound clip of Door closing if ( door. Close. Clip != null ) door. Close. Clip. play(); Outline Play sound effect of Door closing } // end method door. Closed // invoked when Button has been pressed in model public void button. Pressed( Button. Event button. Event ) { // get Button. Event Location String location = button. Event. get. Location(). get. Location. Name(); // press Elevator Button if from Elevator if ( location. equals( ELEVATOR_NAME ) ) { elevator. Button. Panel. play. Animation( 0 ); elevator. Button. Panel. set. Display. Last. Frame( true ); } // press Floor Button if from Floor else if ( location. equals( FIRST_FLOOR_NAME ) ) { first. Floor. Button. Panel. play. Animation( 0 ); first. Floor. Button. Panel. set. Display. Last. Frame( true ); } Invoked when Button Elevator. View. ja has been vapressed Elevator. View displayswhere the elevator Obtain Location simulation Button was pressedmodel. If Button was pressed in Lines 747 -748 Elevator, play animation of Elevator’s Line 753 Button being pressed Lines 756 -757 Lines 760 -763 If Button was pressed on first Floor, play 768 -771 animation of Lines first Floor’s Button being pressed 2003 Prentice Hall, Inc. All rights reserved.

772 773 774 775 776 777 778 779 780 781 782 783 784 785

772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 Outline else if ( location. equals( SECOND_FLOOR_NAME ) ) { second. Floor. Button. Panel. play. Animation( 0 ); second. Floor. Button. Panel. set. Display. Last. Frame( true ); } if ( button. Clip != null ) button. Clip. play(); // play button press sound clip } // end method button. Pressed // invoked when Button has been reset in model public void button. Reset( Button. Event button. Event ) { // get Button. Event Location String location = button. Event. get. Location(). get. Location. Name(); If Button was pressed on second Floor, play animation of second Floor’s Elevator. View. ja Button being pressed va Elevator. View displays the elevator Invoked when Button simulation model. has been reset Lines 774 -777 Obtain Location where Button was reset Line 785 // reset Elevator Button if from Elevator if ( location. equals( ELEVATOR_NAME ) ) { // return to first frame if still animating if ( elevator. Button. Panel. is. Animating() ) elevator. Button. Panel. set. Display. Last. Frame( false ); else elevator. Button. Panel. set. Current. Frame( 0 ); Lines 788 -789 If Button was reset in Elevator, play Lines 792 -799 animation of Elevator’s Button being reset } 2003 Prentice Hall, Inc. All rights reserved.

800 801 802 803 804 805 806 807 808 809 810 811 812 813

800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 Outline // reset Floor Button if from Floor else if ( location. equals( FIRST_FLOOR_NAME ) ) { // return to first frame if still animating if ( first. Floor. Button. Panel. is. Animating() ) first. Floor. Button. Panel. set. Display. Last. Frame( false ); else first. Floor. Button. Panel. set. Current. Frame( 0 ); } else if ( location. equals( SECOND_FLOOR_NAME ) ) { // return to first frame if still animating if ( second. Floor. Button. Panel. is. Animating() ) second. Floor. Button. Panel. set. Display. Last. Frame( false ); else second. Floor. Button. Panel. set. Current. Frame( 0 ); If Button was reset on first Floor, play animation of first Floor’s Button being reset Elevator. View. ja va Elevator. View displays the elevator model. If Button simulation was reset on second Floor, play Lines 804 -812 animation of second Floor’s Button being reset Lines 815 -823 } } // end method button. Reset 2003 Prentice Hall, Inc. All rights reserved.

827 828 829 830 831 832 833 834 835 836 837 838 839 840

827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 // invoked when Bell has rung in model public void bell. Rang( Bell. Event bell. Event ) { bell. Panel. play. Animation( 0 ); // animate Bell if ( bell. Clip != null ) // play Bell sound clip bell. Clip. play(); } // invoked when Light turned on in model public void light. Turned. On( Light. Event light. Event ) { // turn on Light in Elevator elevator. Light. Panel. set. Current. Frame( 1 ); String location = light. Event. get. Location(). get. Location. Name(); // turn on Light on either first or second Floor if ( location. equals( FIRST_FLOOR_NAME ) ) first. Floor. Light. Panel. set. Current. Frame( 1 ); else if ( location. equals( SECOND_FLOOR_NAME ) ) second. Floor. Light. Panel. set. Current. Frame( 1 ); } // end method light. Turned. On Outline Invoked when Bell has rung Play animation and sound Elevator. View. ja effect of Bell ringing va Elevator. View Invoked when Light displays the elevator has turned on simulation model. Line 828 If Light was illuminated Lines 830 -833 on first Floor, play animation of first Floor’s Lineturned 837 on Light being Lines 846 -847 If Light was illuminated on second Floor, Lines play 851 -852 animation of second Floor’s Light being turned on 2003 Prentice Hall, Inc. All rights reserved.

856 857 858 859 860 861 862 863 864 865 866 867 868 869

856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 // invoked when Light turned off in model public void light. Turned. Off( Light. Event light. Event ) { // turn off Light in Elevator elevator. Light. Panel. set. Current. Frame( 0 ); String location = light. Event. get. Location(). get. Location. Name(); // turn off Light on either first or second Floor if ( location. equals( FIRST_FLOOR_NAME ) ) first. Floor. Light. Panel. set. Current. Frame( 0 ); else if ( location. equals( SECOND_FLOOR_NAME ) ) second. Floor. Light. Panel. set. Current. Frame( 0 ); } // end method light. Turned. Off // return preferred size of Elevator. View public Dimension get. Preferred. Size() { return new Dimension( VIEW_WIDTH, VIEW_HEIGHT ); } Outline Invoked when Light has turned off If Light was turned off first Floor, play animation of first Floor’s Light Elevator. View. ja being turned va off Elevator. View displays the elevator If Light wassimulation turned off model. on second Floor, play animation of second Floor’s Light Line 857 being turned off Lines 866 -867 Lines 871 -872 Set Elevator. View’s preferred size to VIEW_WIDTH and Lines 877 -880 VIEW_HEIGHT 2003 Prentice Hall, Inc. All rights reserved.

882 883 884 885 886 887 888 889 890 891 892 893 // return

882 883 884 885 886 887 888 889 890 891 892 893 // return minimum size of Elevator. View public Dimension get. Minimum. Size() { return get. Preferred. Size(); } // return maximum size of Elevator. View public Dimension get. Maximum. Size() { return get. Preferred. Size(); } Outline Set Elevator. View’s minimum and maximum sizes to VIEW_WIDTH and VIEW_HEIGHT } Elevator. View. ja va Elevator. View displays the elevator simulation model. Lines 883 -892 2003 Prentice Hall, Inc. All rights reserved.

35 F. 2 Class Objects • Image. Panel – Used for objects that are

35 F. 2 Class Objects • Image. Panel – Used for objects that are stationary in model • e. g. , Floor, Elevator. Shaft • Moving. Panel – Used for objects that “move” in model • e. g. , Elevator • Animated. Panel – 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. ) 2003 Prentice Hall, Inc. All rights reserved.

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

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

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

38 F. 3 Class Constants • Constants specify such information as: – – –

38 F. 3 Class Constants • Constants specify such information as: – – – Initial placement of objects in Elevator. View Rate at which Elevator. View redraws screen Names of image files used by Image. Panels Names of sound files used by Sound. Effects Distances in pixels the Image. Panels representing Elevator and Person must travel – Times needed to travel these distances 2003 Prentice Hall, Inc. All rights reserved.

39 F. 4 Class Constructor • Elevator. View constructor’s responsibilities: – – – Instantiate

39 F. 4 Class Constructor • Elevator. View constructor’s responsibilities: – – – Instantiate Image. Panels Add all Image. Panels to Elevator. View 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. ) first. Floor. Button. Panel : Animated. Panel

40 F. 4 Class Constructor (cont. ) first. Floor. Button. Panel : Animated. Panel first. Floor. Panel : Image. Panel second. Floor. Button. Panel : Animated. Panel second. Floor. Panel : Image. Panel first. Floor. Light. Panel : Animated. Panel elevator. Shaft. Panel : Image. Panel second. Floor. Light. Panel : Animated. Panel ceiling. Panel : Image. Panel wall. Panel : Image. Panel elevator. Panel : Moving. Panel : Elevator. View bell. Clip : Audio. Clip door. Open. Clip : Audio. Clip light. Panel : Animated. Panel door. Close. Clip : Audio. Clip : Sound. Effects bell. Panel : Animated. Panel door. Panel : Animated. Panel elevator. Button. Panel : Animated. Panel elevator. Clip : Audio. Clip button. Clip : Audio. Clip walk. Clip : Audio. Clip Fig F. 4 Object diagram for the Elevator. View after initialization. 2003 Prentice Hall, Inc. All rights reserved.

41 F. 5 Event Handling • Event Handling in the view – Elevator. View

41 F. 5 Event Handling • Event Handling in the view – Elevator. View implements interface Elevator. Simulation. Listener • Elevator. View implements all interfaces – Elevator. Simulation sends events to Elevator. View • Elevator. Case. Study registers Elevator. View for events from Elevator. Simulation 2003 Prentice Hall, Inc. All rights reserved.

42 F. 5. 1 Elevator. Move. Event types • Elevator. Simulation – Sends Elevator.

42 F. 5. 1 Elevator. Move. Event types • Elevator. Simulation – Sends Elevator. Move. Event when Elevator departed or arrived in the model – Invokes method elevator. Departed when Elevator departed from Floor – Invokes method elevator. Arrived when Elevator arrived at Floor 2003 Prentice Hall, Inc. All rights reserved.

43 F. 5. 2 Person. Move. Event types • Elevator. Simulation – Sends Person.

43 F. 5. 2 Person. Move. Event types • Elevator. Simulation – Sends Person. Move. Event when Person performes actions – Invokes method person. Created when model instantiates new Person – Invokes method person. Arrived when Person arrives at Elevator – Invokes method person. Pressed. Button when Person presses Button – Invokes method person. Entered when Person enters Elevator – Invokes method person. Departed when Person exits Elevator – Invokes method person. Exited when Person exits simulation 2003 Prentice Hall, Inc. All rights reserved.

44 F. 5. 3 Door. Event types • Elevator. Simulation – Sends Door. Event

44 F. 5. 3 Door. Event types • Elevator. Simulation – Sends Door. Event when Door opened or closed in the model – Invokes method door. Opened when Door opened – Invokes method door. Closed when Door closed 2003 Prentice Hall, Inc. All rights reserved.

45 F. 5. 4 Button. Event types • Elevator. Simulation – Sends Button. Event

45 F. 5. 4 Button. Event types • Elevator. Simulation – Sends Button. Event when Button pressed or reset in the model – Invokes method button. Pressed when Button pressed – Invokes method button. Reset when Button reset 2003 Prentice Hall, Inc. All rights reserved.

46 F. 5. 5 Bell. Event types • Elevator. Simulation – Sends Bell. Event

46 F. 5. 5 Bell. Event types • Elevator. Simulation – Sends Bell. Event when Bell rung • Invoking method bell. Rang 2003 Prentice Hall, Inc. All rights reserved.

47 F. 5. 6 Light. Event types • Elevator. Simulation – Sends Light. Event

47 F. 5. 6 Light. Event types • Elevator. Simulation – Sends Light. Event when Light changed state in the model – Invokes method light. Turned. On when Light turned on – Invokes method light. Turned. Off when Light turned off 2003 Prentice Hall, Inc. All rights reserved.

48 F. 6 Artifacts Revisited • Component Diagram for view – Package view contains

48 F. 6 Artifacts Revisited • Component Diagram for view – Package view contains six artifacts • Elevator. View. java – Aggregates (imports) packages images, sounds, events • Image. Panel. java • Moving. Panel. java • Animated. Panel. java • Elevator. Music. java • Sound. Effects. java 2003 Prentice Hall, Inc. All rights reserved.

49 F. 6 Component Diagrams Revisited (cont. ) view <<imports>> <<file>> <<imports>> Elevator. View.

49 F. 6 Component Diagrams Revisited (cont. ) view <<imports>> <<file>> <<imports>> Elevator. View. java <<imports>> <<file>> Image. Panel. java Moving. Panel. java <<file>> Animated. Panel. java images Sound. Effects. java sounds 1 event 1 1 Fig F. 5 Component diagram for package view. 2003 Prentice Hall, Inc. All rights reserved.

50 F. 7 Conclusion • OOD/UML case study – Implement object-oriented system designs generated

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