26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1458 Eleva<strong>to</strong>r View (on CD) Appendix I<br />

<strong>to</strong>nPanel or secondFloorBut<strong>to</strong>nPanel and the initial on-screen location of an<br />

AnimatedPanel associated with Person. This AnimatedPanel uses this constant<br />

<strong>to</strong> calculate the travel distance <strong>to</strong> the firstFloorBut<strong>to</strong>nPanel or secondFloor-<br />

But<strong>to</strong>nPanel. The firstFloorBut<strong>to</strong>nPanel and secondFloorBut<strong>to</strong>n-<br />

Panel use the constant <strong>to</strong> position themselves on screen. Line 34 declares the int<br />

constant BUTTON_TO_ELEVATOR_DISTANCE, describing the horizontal distance<br />

between the firstFloorBut<strong>to</strong>nPanel or secondFloorBut<strong>to</strong>nPanel and the<br />

eleva<strong>to</strong>rPanel. The AnimatedPanel associated with a Person uses this constant<br />

<strong>to</strong> determine the travel distance when entering the eleva<strong>to</strong>rPanel.<br />

Line 39 declares int constant TIME_TO_BUTTON, which represents this AnimatedPanel’s<br />

travel time <strong>to</strong> the firstFloorBut<strong>to</strong>nPanel or secondFloorBut<strong>to</strong>nPanel.<br />

Line 40 declares int constant TIME_TO_ELEVATOR, which represents the<br />

time the AnimatedPanel associated with a Person needs <strong>to</strong> enter the eleva<strong>to</strong>r-<br />

Panel from the firstFloorBut<strong>to</strong>nPanel or secondFloorBut<strong>to</strong>nPanel.<br />

Using the equation rate = distance / time, the AnimatedPanel associated with the<br />

Person can determine the velocity needed <strong>to</strong> travel. Similarly, line 43 declares int constant<br />

ELEVATOR_TRAVEL_TIME, which represents the eleva<strong>to</strong>rPanel’s travel time<br />

between the firstFloorPanel and secondFloorPanel—lines 166–169 use this<br />

constant <strong>to</strong> determine double attribute eleva<strong>to</strong>rVelocity (line 149).<br />

I.4 Class construc<strong>to</strong>r<br />

The responsibilities of the Eleva<strong>to</strong>rView construc<strong>to</strong>r (lines 152–174) are<br />

• To instantiate all ImagePanels<br />

• To add all ImagePanels <strong>to</strong> the Eleva<strong>to</strong>rView<br />

• To initialize the audio objects<br />

• To compute the eleva<strong>to</strong>rPanel’s initial velocity and distance traveled<br />

• To start the animation Timer<br />

Lines 157 calls private method instantiatePanels (lines 177–299), which<br />

instantiates all ImagePanels in the Eleva<strong>to</strong>rView. Lines 180–181 instantiate the<br />

firstFloorPanel and secondFloorPanel, and lines 184–190 set these objects’<br />

positions—the Eleva<strong>to</strong>rView positions the firstFloorPanel on the bot<strong>to</strong>m of<br />

the screen and positions the secondFloorPanel in the vertical center of the screen.<br />

Line 192 instantiates the wallPanel ImagePanel. The Eleva<strong>to</strong>rView does not<br />

need <strong>to</strong> calculate the position for the wallPanel, because the wallPanel’s default<br />

screen position (i.e., xPosition = 0, yPosition = 0) is correct. Lines 195–202 and<br />

lines 205–210 instantiate and position the eleva<strong>to</strong>rShaftPanel and ceiling-<br />

Panel ImagePanels, respectively. The Eleva<strong>to</strong>rView positions the eleva<strong>to</strong>r-<br />

ShaftPanel in the right of the screen and positions the ceilingPanel above the<br />

eleva<strong>to</strong>rShaftPanel. Lines 213–217 instantiate the eleva<strong>to</strong>rPanel and position<br />

it over the eleva<strong>to</strong>rShaftPanel above the firstFloorPanel. Lines 220–<br />

229 instantiate the firstFloorBut<strong>to</strong>nPanel AnimatedPanel, place it next <strong>to</strong> the<br />

eleva<strong>to</strong>rShaftPanel, then create a frame sequence But<strong>to</strong>n pressed animation.<br />

Lines 232–240 perform the same actions on the secondFloorBut<strong>to</strong>nPanel. Lines<br />

243–249 instantiate the firstFloorLightPanel AnimatedPanel and place it <strong>to</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!