// ElevatorView.java
// View for ElevatorSimulation
package com.deitel.jhtp5.elevator.view;
// Java core packages
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.applet.*;
// Java extension package
import javax.swing.*;
// Deitel packages
import com.deitel.jhtp5.elevator.event.*;
import com.deitel.jhtp5.elevator.ElevatorConstants;
public class ElevatorView extends JPanel
implements ActionListener, ElevatorSimulationListener,
ElevatorConstants {
// ElevatorView dimensions
private static final int VIEW_WIDTH = 800;
private static final int VIEW_HEIGHT = 435;
// offset for positioning Panels in ElevatorView
private static final int OFFSET = 10;
// Elevator repaints components every 50 ms
private static final int ANIMATION_DELAY = 50;
// 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 doorFrames[] = {
"images/door1.png", "images/door2.png", "images/door3.png",
"images/door4.png", "images/door5.png" };
// Person images for animation
private static final String personFrames[] = {
"images/bug1.png", "images/bug2.png", "images/bug3.png",
"images/bug4.png", "images/bug5.png", "images/bug6.png",
"images/bug7.png", "images/bug8.png" };
// Light images for animation
private static final String lightFrames[] = {
"images/lightOff.png", "images/lightOn.png" };
// Floor Light images for animation
private static final String firstFloorLightFrames[] = {
"images/firstFloorLightOff.png",
"images/firstFloorLightOn.png" };
private static final String secondFloorLightFrames[] = {
"images/secondFloorLightOff.png",
"images/secondFloorLightOn.png", };
// Floor Button images for animation
private static final String floorButtonFrames[] = {
"images/floorButtonUnpressed.png",
"images/floorButtonPressed.png",
"images/floorButtonLit.png" };
// Elevator Button images for animation
private static final String elevatorButtonFrames[] = {
"images/elevatorButtonUnpressed.png",
"images/elevatorButtonPressed.png",
"images/elevatorButtonLit.png" };
// Bell images for animation
private static final String bellFrames[] = {
"images/bell1.png", "images/bell2.png",
"images/bell3.png" };
private static final String floorImage =
"images/floor.png";
private static final String ceilingImage =
"images/ceiling.png";
private static final String elevatorImage =
"images/elevator.png";
private static final String wallImage =
"images/wall.jpg";
private static final String elevatorShaftImage =
"images/elevatorShaft.png";
// audio files
private static final String bellSound = "bell.wav";
private static final String doorOpenSound = "doorOpen.wav";
private static final String doorCloseSound = "doorClose.wav";
private static final String elevatorSound = "elevator.au";
private static final String buttonSound = "button.wav";
private static final String walkingSound = "walk.wav";
private static final String elevatorMusicSound = "liszt.mid";
// ImagePanels for Floors, ElevatorShaft, wall and ceiling
private ImagePanel firstFloorPanel;
private ImagePanel secondFloorPanel;
private ImagePanel elevatorShaftPanel;
private ImagePanel wallPanel;
private ImagePanel ceilingPanel;
// MovingPanels for Elevator
private MovingPanel elevatorPanel;
// AnimatedPanels for Buttons, Bell, Lights and Door
private AnimatedPanel firstFloorButtonPanel;
private AnimatedPanel secondFloorButtonPanel;
private AnimatedPanel elevatorButtonPanel;
private AnimatedPanel bellPanel;
private AnimatedPanel elevatorLightPanel;
private AnimatedPanel firstFloorLightPanel;
private AnimatedPanel secondFloorLightPanel;
private AnimatedPanel doorPanel;
// List containing AnimatedPanels for all Person objects
private java.util.List personAnimatedPanels;
// AudioClips for sound effects
private AudioClip bellClip;
private AudioClip doorOpenClip;
private AudioClip doorCloseClip;
private AudioClip elevatorClip;
private AudioClip buttonClip;
private AudioClip walkClip;
// ElevatorMusic to play in Elevator
private AudioClip elevatorMusicClip;
// Timer for animation controller;
private javax.swing.Timer animationTimer;
// distance from top of screen to display Floors
private int firstFloorPosition;
private int secondFloorPosition;
// Elevator's velocity
private double elevatorVelocity;
// ElevatorView constructor
public ElevatorView()
{
// specifiy null Layout
super( null );
instantiatePanels();
placePanelsOnView();
initializeAudio();
// calculate distance Elevator travels
double floorDistance =
firstFloorPosition - secondFloorPosition;
// calculate time needed for travel
double time = ELEVATOR_TRAVEL_TIME / ANIMATION_DELAY;
// determine Elevator velocity (rate = distance / time)
elevatorVelocity = ( floorDistance + OFFSET ) / time;
// start animation Thread
startAnimation();
} // end ElevatorView constructor
// instantiate all Panels (Floors, Elevator, etc.)
private void instantiatePanels()
{
// instantiate ImagePanels representing Floors
firstFloorPanel = new ImagePanel( 0, floorImage );
secondFloorPanel = new ImagePanel( 0, floorImage );
// calculate first and second Floor positions
firstFloorPosition =
VIEW_HEIGHT - firstFloorPanel.getHeight();
secondFloorPosition =
( int ) ( firstFloorPosition / 2 ) - OFFSET;
firstFloorPanel.setPosition( 0, firstFloorPosition );
secondFloorPanel.setPosition( 0, secondFloorPosition );
wallPanel = new ImagePanel( 0, wallImage );
// create and position ImagePanel for ElevatorShaft
elevatorShaftPanel =
new ImagePanel( 0, elevatorShaftImage );
double xPosition = PERSON_TO_ELEVATOR_DISTANCE + OFFSET;
double yPosition =
firstFloorPosition - elevatorShaftPanel.getHeight();
elevatorShaftPanel.setPosition( xPosition, yPosition );
// create and position ImagePanel for ceiling
ceilingPanel = new ImagePanel( 0, ceilingImage );
yPosition = elevatorShaftPanel.getPosition().getY() -
ceilingPanel.getHeight();
ceilingPanel.setPosition( xPosition, yPosition );
// create and position MovingPanel for Elevator
elevatorPanel = new MovingPanel( 0, elevatorImage );
yPosition = firstFloorPosition - elevatorPanel.getHeight();
elevatorPanel.setPosition( xPosition, yPosition );
// create and position first Floor Button
firstFloorButtonPanel =
new AnimatedPanel( 0, floorButtonFrames );
xPosition = PERSON_TO_BUTTON_DISTANCE + 2 * OFFSET;
yPosition = firstFloorPosition - 5 * OFFSET;
firstFloorButtonPanel.setPosition( xPosition, yPosition );
int floorButtonPressedFrameOrder[] = { 0, 1, 2 };
firstFloorButtonPanel.addFrameSequence(
floorButtonPressedFrameOrder );
// create and posit
没有合适的资源?快使用搜索试试~ 我知道了~
Elevator_Program.rar_java电梯_电梯
共121个文件
class:44个
java:33个
png:32个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 41 浏览量
2022-09-23
09:21:52
上传
评论
收藏 569KB RAR 举报
温馨提示
是java中的一个小程序,可以用来做课堂演示用,是一个电梯程序,许多书里都有用到的
资源推荐
资源详情
资源评论
收起资源包目录
Elevator_Program.rar_java电梯_电梯 (121个子文件)
elevator.au 13KB
ElevatorView.class 15KB
ElevatorSimulation.class 7KB
Elevator.class 6KB
ElevatorShaft.class 6KB
Person.class 4KB
AnimatedPanel.class 3KB
ImagePanel.class 3KB
Door.class 3KB
Light.class 2KB
ElevatorController.class 2KB
ElevatorController$3.class 2KB
Button.class 2KB
MovingPanel.class 2KB
ElevatorCaseStudy.class 2KB
ElevatorShaft$1.class 2KB
Floor.class 1KB
Bell.class 1KB
ElevatorShaft$3.class 1KB
Elevator$2.class 1KB
ElevatorDoor.class 1KB
ElevatorController$2.class 1KB
ElevatorController$1.class 1KB
SoundEffects.class 1KB
Elevator$1.class 1KB
ElevatorSimulationEvent.class 1KB
ElevatorShaft$2.class 1KB
ElevatorShaft$4.class 1KB
Light$1.class 1KB
Door$1.class 1KB
PersonMoveEvent.class 746B
Location.class 720B
ElevatorMoveEvent.class 551B
ButtonEvent.class 533B
LightEvent.class 530B
DoorEvent.class 527B
BellEvent.class 527B
ElevatorSimulationListener.class 494B
PersonMoveListener.class 354B
ElevatorConstants.class 328B
ElevatorMoveListener.class 263B
ButtonListener.class 238B
LightListener.class 238B
DoorListener.class 228B
BellListener.class 205B
.classpath 247B
Thumbs.db 75KB
1.jar 256KB
ElevatorView.java 30KB
Elevator.java 11KB
ElevatorShaft.java 9KB
ElevatorSimulation.java 9KB
Person.java 9KB
AnimatedPanel.java 5KB
ElevatorController.java 5KB
Door.java 5KB
ImagePanel.java 4KB
Light.java 4KB
MovingPanel.java 3KB
Floor.java 3KB
ElevatorCaseStudy.java 3KB
Button.java 2KB
ElevatorSimulationEvent.java 2KB
ElevatorDoor.java 2KB
Bell.java 2KB
PersonMoveListener.java 2KB
SoundEffects.java 2KB
Location.java 2KB
PersonMoveEvent.java 2KB
ElevatorMoveEvent.java 1KB
ElevatorMoveListener.java 1KB
LightEvent.java 1KB
ButtonEvent.java 1KB
ButtonListener.java 1KB
DoorEvent.java 1KB
ElevatorSimulationListener.java 1KB
LightListener.java 1KB
BellEvent.java 1KB
ElevatorConstants.java 1KB
DoorListener.java 1KB
BellListener.java 1KB
wall.jpg 72KB
liszt.mid 6KB
elevatorShaft.png 11KB
door2.png 7KB
door1.png 6KB
door4.png 5KB
door3.png 5KB
floor.png 5KB
elevator.png 4KB
door5.png 4KB
bug6.png 3KB
bug3.png 3KB
bug8.png 3KB
bug1.png 3KB
ceiling.png 3KB
bug5.png 3KB
bug2.png 3KB
bug4.png 3KB
bug7.png 3KB
共 121 条
- 1
- 2
资源评论
寒泊
- 粉丝: 75
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功