📄 elevatorview.java
字号:
panelLocation = FIRST_FLOOR_NAME;
else
panelLocation = SECOND_FLOOR_NAME;
int xPosition =
( int ) personPanel.getPosition().getX();
// if Person is inside Elevator
if ( panelLocation.equals( location )
&& xPosition > PERSON_TO_BUTTON_DISTANCE + OFFSET ) {
// remove Person AnimatedPanel from ElevatorView
remove( personPanel );
// add Person AnimatedPanel to Elevator
elevatorPanel.add( personPanel, 1 );
personPanel.setLocation( 2 * OFFSET, 9 * OFFSET );
personPanel.setMoving( false );
personPanel.setAnimating( false );
personPanel.setVelocity( 0, 0 );
personPanel.setCurrentFrame( 1 );
}
} // end while loop
// determine Elevator velocity depending on Floor
if ( location.equals( FIRST_FLOOR_NAME ) )
elevatorPanel.setVelocity( 0, -elevatorVelocity );
else
if ( location.equals( SECOND_FLOOR_NAME ) )
elevatorPanel.setVelocity( 0, elevatorVelocity );
// begin moving Elevator and play Elevator music
elevatorPanel.setMoving( true );
if ( elevatorClip != null )
elevatorClip.play();
elevatorMusicClip.play();
} // end method elevatorDeparted
// invoked when Elevator has arrived at destination Floor
public void elevatorArrived( ElevatorMoveEvent moveEvent )
{
// stop Elevator and music
elevatorPanel.setMoving( false );
elevatorMusicClip.stop();
double xPosition = elevatorPanel.getPosition().getX();
double yPosition;
// set Elevator's position to either first or second Floor
if ( elevatorPanel.getYVelocity() < 0 )
yPosition =
secondFloorPosition - elevatorPanel.getHeight();
else
yPosition =
firstFloorPosition - elevatorPanel.getHeight();
elevatorPanel.setPosition( xPosition, yPosition );
} // end method elevatorArrived
// invoked when Person has been created in model
public void personCreated( PersonMoveEvent personEvent )
{
int personID = personEvent.getID();
String floorLocation =
personEvent.getLocation().getLocationName();
// create AnimatedPanel representing Person
AnimatedPanel personPanel =
new AnimatedPanel( personID, personFrames );
// determine where Person should be drawn initially
// negative xPosition ensures Person drawn offscreen
double xPosition = - personPanel.getWidth();
double yPosition = 0;
if ( floorLocation.equals( FIRST_FLOOR_NAME ) )
yPosition = firstFloorPosition +
( firstFloorPanel.getHeight() / 2 );
else
if ( floorLocation.equals( SECOND_FLOOR_NAME ) )
yPosition = secondFloorPosition +
( secondFloorPanel.getHeight() / 2 );
yPosition -= personPanel.getHeight();
personPanel.setPosition( xPosition, yPosition );
// add some animations for each Person
int walkFrameOrder[] = { 1, 0, 1, 2 };
int pressButtonFrameOrder[] = { 1, 3, 3, 4, 4, 1 };
int walkAwayFrameOrder[] = { 6, 5, 6, 7 };
personPanel.addFrameSequence( walkFrameOrder );
personPanel.addFrameSequence( pressButtonFrameOrder );
personPanel.addFrameSequence( walkAwayFrameOrder );
// have Person begin walking to Elevator
personPanel.playAnimation( 0 );
personPanel.setLoop( true );
personPanel.setAnimating( true );
personPanel.setMoving( true );
// determine Person velocity
double time =
( double ) ( TIME_TO_BUTTON / ANIMATION_DELAY );
double xDistance = PERSON_TO_BUTTON_DISTANCE -
2 * OFFSET + personPanel.getSize().width;
double xVelocity = xDistance / time;
personPanel.setVelocity( xVelocity, 0 );
personPanel.setAnimationRate( 1 );
walkClip.loop(); // play sound clip of Person walking
// store in personAnimatedPanels
synchronized( personAnimatedPanels )
{
personAnimatedPanels.add( personPanel );
}
add( personPanel, 0 );
} // end method personCreated
// invoked when Person has arrived at Elevator
public void personArrived( PersonMoveEvent personEvent )
{
// find Panel associated with Person that issued event
AnimatedPanel panel = getPersonPanel( personEvent );
if ( panel != null ) { // if Person exists
// Person stops at Floor Button
panel.setMoving( false );
panel.setAnimating( false );
panel.setCurrentFrame( 1 );
stopWalkingSound();
double xPosition = PERSON_TO_BUTTON_DISTANCE -
( panel.getSize().width / 2 );
double yPosition = panel.getPosition().getY();
panel.setPosition( xPosition, yPosition );
}
} // end method personArrived
// invoked when Person has pressed Button
public void personPressedButton( PersonMoveEvent personEvent )
{
// find Panel associated with Person that issued event
AnimatedPanel panel = getPersonPanel( personEvent );
if ( panel != null ) { // if Person exists
// Person stops walking and presses Button
panel.setLoop( false );
panel.playAnimation( 1 );
panel.setVelocity( 0, 0 );
panel.setMoving( false );
panel.setAnimating( true );
stopWalkingSound();
}
} // end method personPressedButton
// invoked when Person has started to enter Elevator
public void personEntered( PersonMoveEvent personEvent )
{
// find Panel associated with Person that issued event
AnimatedPanel panel = getPersonPanel( personEvent );
if ( panel != null ) {
// determine velocity
double time = TIME_TO_ELEVATOR / ANIMATION_DELAY;
double distance =
elevatorPanel.getPosition().getX() -
panel.getPosition().getX() + 2 * OFFSET;
panel.setVelocity( distance / time, -1.5 );
// Person starts walking
panel.setMoving( true );
panel.playAnimation( 0 );
panel.setLoop( true );
}
} // end method personEntered
// invoked when Person has departed from Elevator
public void personDeparted( PersonMoveEvent personEvent)
{
// find Panel associated with Person that issued event
AnimatedPanel panel = getPersonPanel( personEvent );
if ( panel != null ) { // if Person exists
// determine velocity (in opposite direction)
double time = TIME_TO_BUTTON / ANIMATION_DELAY;
double xVelocity = - PERSON_TO_BUTTON_DISTANCE / time;
panel.setVelocity( xVelocity, 0 );
// remove Person from Elevator
elevatorPanel.remove( panel );
double xPosition =
PERSON_TO_ELEVATOR_DISTANCE + 3 * OFFSET;
double yPosition = 0;
String floorLocation =
personEvent.getLocation().getLocationName();
// determine Floor onto which Person exits
if ( floorLocation.equals( FIRST_FLOOR_NAME ) )
yPosition = firstFloorPosition +
( firstFloorPanel.getHeight() / 2 );
else
if ( floorLocation.equals( SECOND_FLOOR_NAME ) )
yPosition = secondFloorPosition +
( secondFloorPanel.getHeight() / 2 );
yPosition -= panel.getHeight();
panel.setPosition( xPosition, yPosition );
// add Person to ElevatorView
add( panel, 0 );
// Person starts walking
panel.setMoving( true );
panel.setAnimating( true );
panel.playAnimation( 2 );
panel.setLoop( true );
walkClip.loop();
}
} // end method PersonDeparted
// invoked when Person has exited simulation
public void personExited( PersonMoveEvent personEvent)
{
// find Panel associated with Person that issued moveEvent
AnimatedPanel panel = getPersonPanel( personEvent );
if ( panel != null ) { // if Person exists
panel.setMoving( false );
panel.setAnimating( false );
// remove Person permanently and stop walking sound
synchronized( personAnimatedPanels )
{
personAnimatedPanels.remove( panel );
}
remove( panel );
stopWalkingSound();
}
} // end method personExited
// invoked when Door has opened in model
public void doorOpened( DoorEvent doorEvent )
{
// get DoorEvent Location
String location =
doorEvent.getLocation().getLocationName();
// play animation of Door opening
doorPanel.playAnimation( 0 );
doorPanel.setAnimationRate( 2 );
doorPanel.setDisplayLastFrame( true );
// play sound clip of Door opening
if ( doorOpenClip != null )
doorOpenClip.play();
} // end method doorOpened
// invoked when Door has closed in model
public void doorClosed( DoorEvent doorEvent )
{
// get DoorEvent Location
String location =
doorEvent.getLocation().getLocationName();
// play animation of Door closing
doorPanel.playAnimation( 1 );
doorPanel.setAnimationRate( 2 );
doorPanel.setDisplayLastFrame( true );
// play sound clip of Door closing
if ( doorCloseClip != null )
doorCloseClip.play();
} // end method doorClosed
// invoked when Button has been pressed in model
public void buttonPressed( ButtonEvent buttonEvent )
{
// get ButtonEvent Location
String location =
buttonEvent.getLocation().getLocationName();
// press Elevator Button if from Elevator
if ( location.equals( ELEVATOR_NAME ) ) {
elevatorButtonPanel.playAnimation( 0 );
elevatorButtonPanel.setDisplayLastFrame( true );
}
// press Floor Button if from Floor
else
if ( location.equals( FIRST_FLOOR_NAME ) ) {
firstFloorButtonPanel.playAnimation( 0 );
firstFloorButtonPanel.setDisplayLastFrame( true );
}
else
if ( location.equals( SECOND_FLOOR_NAME ) ) {
secondFloorButtonPanel.playAnimation( 0 );
secondFloorButtonPanel.setDisplayLastFrame( true );
}
if ( buttonClip != null )
buttonClip.play(); // play button press sound clip
} // end method buttonPressed
// invoked when Button has been reset in model
public void buttonReset( ButtonEvent buttonEvent )
{
// get ButtonEvent Location
String location =
buttonEvent.getLocation().getLocationName();
// reset Elevator Button if from Elevator
if ( location.equals( ELEVATOR_NAME ) ) {
// return to first frame if still animating
if ( elevatorButtonPanel.isAnimating() )
elevatorButtonPanel.setDisplayLastFrame( false );
else
elevatorButtonPanel.setCurrentFrame( 0 );
}
// reset Floor Button if from Floor
else
if ( location.equals( FIRST_FLOOR_NAME ) ) {
// return to first frame if still animating
if ( firstFloorButtonPanel.isAnimating() )
firstFloorButtonPanel.setDisplayLastFrame(
false );
else
firstFloorButtonPanel.setCurrentFrame( 0 );
}
else
if ( location.equals( SECOND_FLOOR_NAME ) ) {
// return to first frame if still animating
if ( secondFloorButtonPanel.isAnimating() )
secondFloorButtonPanel.setDisplayLastFrame(
false );
else
secondFloorButtonPanel.setCurrentFrame( 0 );
}
} // end method buttonReset
// invoked when Bell has rung in model
public void bellRang( BellEvent bellEvent )
{
bellPanel.playAnimation( 0 ); // animate Bell
if ( bellClip != null ) // play Bell sound clip
bellClip.play();
}
// invoked when Light turned on in model
public void lightTurnedOn( LightEvent lightEvent )
{
// turn on Light in Elevator
elevatorLightPanel.setCurrentFrame( 1 );
String location =
lightEvent.getLocation().getLocationName();
// turn on Light on either first or second Floor
if ( location.equals( FIRST_FLOOR_NAME ) )
firstFloorLightPanel.setCurrentFrame( 1 );
else
if ( location.equals( SECOND_FLOOR_NAME ) )
secondFloorLightPanel.setCurrentFrame( 1 );
} // end method lightTurnedOn
// invoked when Light turned off in model
public void lightTurnedOff( LightEvent lightEvent )
{
// turn off Light in Elevator
elevatorLightPanel.setCurrentFrame( 0 );
String location =
lightEvent.getLocation().getLocationName();
// turn off Light on either first or second Floor
if ( location.equals( FIRST_FLOOR_NAME ) )
firstFloorLightPanel.setCurrentFrame( 0 );
else
if ( location.equals( SECOND_FLOOR_NAME ) )
secondFloorLightPanel.setCurrentFrame( 0 );
} // end method lightTurnedOff
// return preferred size of ElevatorView
public Dimension getPreferredSize()
{
return new Dimension( VIEW_WIDTH, VIEW_HEIGHT );
}
// return minimum size of ElevatorView
public Dimension getMinimumSize()
{
return getPreferredSize();
}
// return maximum size of ElevatorView
public Dimension getMaximumSize()
{
return getPreferredSize();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -