⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 elevatormgr.java

📁 这是基于java实现的三层电梯模拟,正确简单
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/*文件名:ElevatorMgr.java
   类名:ElevatorMgr.class
   功能:生成整个程序的布局,以及接受鼠标事件,处理鼠标事件
*/
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.*;
import java.applet.Applet;
import java.applet.*;
//ElevatorMgr类
public class ElevatorMgr extends Applet
{//声明变量

    public Floor firstFloor;//第一楼层
    public Floor secondFloor;//第二楼层
    public Floor thirdFloor;//第三楼层
    public Elevator elevator;//位于楼层的电梯
    public ControlBoard controlBoard;//控制面板
    public Clock time;//时钟
    public Button floor1Btn;//第1层的按钮
    public Button floor2Btn;//第2层的按钮
    public Button floor3Btn;//第3层的按钮
    public int floorHeight;//楼层的高度
    public int floorWidth;//楼层的宽度
    public int elevatorHeight;//电梯的高度
    public int elevatorWidth;//电梯的宽度
    public int clockHeight;//钟的高度
    public int clockWidth;//钟的宽度
    public int controlBHeight;//控制板的高度
    public int controlBWidth;//控制板的宽度
    public AudioClip clickbutton;//点击按钮的声音
    public AudioClip sayhi;//进入电梯的声音
    public AudioClip error;//操作出错的声音
    public int f1;//楼层一变量
    public int f2;//
    public int f3;//
//程序的初始化,主要是初始化声音文件,安排各个控件的位置,布局
    public void init()
    {
        clickbutton = getAudioClip(getDocumentBase(), "sound/drip.au");//点击按钮的声音
        error = getAudioClip(getDocumentBase(), "sound/yahoo1.au");//出错时声音
        sayhi = getAudioClip(getDocumentBase(), "sound/hi.au");//进入电梯声音
        time = new Clock(1, 1, clockWidth, clockHeight);
        time.resize(clockWidth, 200);//时钟大小调整
        controlBoard = new ControlBoard(this);//创建控制面板
        controlBoard.resize(controlBWidth, controlBHeight);//控制面板的初始化
        elevator = new Elevator(this, time, this);//创建电梯
        elevator.resize(elevatorWidth, elevatorHeight);//电梯大小调整
        //创建楼层以及楼层大小
        firstFloor = new Floor(this, elevator);
        firstFloor.resize(floorWidth, floorHeight);
        secondFloor = new Floor(this, elevator);
        secondFloor.resize(floorWidth, floorHeight);
        thirdFloor = new Floor(this, elevator);
        thirdFloor.resize(floorWidth, floorHeight);
        //创建各楼层按钮的控制面板以及创建各楼层的按钮以及大小的初始化
        controlBoard = new ControlBoard(this);
        controlBoard.resize(controlBWidth, controlBHeight);
        floor1Btn = new Button("Floor1");
        floor1Btn.resize(70, 30);
        floor2Btn = new Button("Floor2");
        floor2Btn.resize(70, 30);
        floor3Btn = new Button("Floor3");
        floor3Btn.resize(70, 30);
        add(time);
        add(firstFloor);
        add(secondFloor);
        add(thirdFloor);
        add(elevator);
        add(controlBoard);
        add(floor1Btn);
        add(floor2Btn);
        add(floor3Btn);
        setLayout(null);
        //setLayout(new FlowLayout);
        firstFloor.reshape(clockWidth + 1, 2*(floorHeight + 1), floorWidth, floorHeight);
        secondFloor.reshape(clockWidth + 1, floorHeight+1, floorWidth, floorHeight);
        thirdFloor.reshape(clockWidth + 1, 1, floorWidth, floorHeight);
        elevator.reshape(clockWidth + floorWidth + 2, 1, elevatorWidth, floorHeight * 3);
        controlBoard.reshape(clockWidth + floorWidth + elevatorWidth + 4, 1, controlBWidth, controlBHeight);
        time.reshape(1, 1, clockWidth, 300);
        floor1Btn.reshape(115, floorHeight * 3 + 5, 70, 30);
        floor2Btn.reshape(215, floorHeight * 3 + 5, 70, 30);
        floor3Btn.reshape(315, floorHeight * 3 + 5, 70, 30);
        f1=1;
        f2=1;
        f3=1;
        //接受各楼层鼠标事件和处理各楼层的鼠标事件
        floor1Btn.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent mouseevent)
            {
                floor1Btn_mouseClicked(mouseevent);
            }

        });
        floor2Btn.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent mouseevent)
            {
                floor2Btn_mouseClicked(mouseevent);
            }

        });
        floor3Btn.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent mouseevent)
            {
                floor3Btn_mouseClicked(mouseevent);
            }

        });
        setBackground(Color.red);
    }

    public boolean mouseUp(Event event, int i, int j)
    {
        if(i >= clockWidth + floorWidth + elevatorWidth + 4 + 20 && i <= clockWidth + floorWidth + elevatorWidth + 4 + 20 + 20 && j >= 20 && j <= 40)
        {
            controlBoard.setFloor1ButtonUnpressed();
        } else
        if(i >= clockWidth + floorWidth + elevatorWidth + 4 + 20 && i <= clockWidth + floorWidth + elevatorWidth + 4 + 20 + 20 && j >= 153 && j <= 173)
        {
            controlBoard.setFloor2ButtonUnpressed();
        }
        if(i >= clockWidth + floorWidth + elevatorWidth + 4 + 20 && i <= clockWidth + floorWidth + elevatorWidth + 4 + 20 + 20 && j >= 240 && j <= 260)
        {
            controlBoard.setFloor3ButtonUnpressed();
        }
        return true;
    }
//鼠标按下事件
    public boolean mouseDown(Event event, int i, int j)
    {//第三层的电梯按钮,按此按钮使得电梯上三楼
        if(i >= clockWidth + floorWidth + elevatorWidth + 4 + 20 && i <= clockWidth + floorWidth + elevatorWidth + 4 + 20 + 20 && j >= 20 && j <= 40)
        {//电梯在运动
            if(elevator.moving)
            {
                showStatus("wait while moving!!!");//显示状态:电梯运动时要等待
            } else
            if(elevator.location != 3)//电梯的位置不在三层
            {
                controlBoard.setFloor1ButtonPressed();//电梯位于一层
                clickbutton.play();//电梯为向上运动,控制面板设置为向上
                elevator.setDestination(3);//目的地为三层
                 controlBoard.setFloor2ButtonPressed();//电梯位于2层
                clickbutton.play();//电梯为向上运动,控制面板设置为向上
                elevator.setDestination(3);//目的地为三层
            }
        } else  //第二层的电梯按钮,按此按钮使得电梯上二层
        if(i >= clockWidth + floorWidth + elevatorWidth + 4 + 20 && i <= clockWidth + floorWidth + elevatorWidth + 4 + 20 + 20 && j >= 153 && j <= 173)
        {//电梯在运动
            if(elevator.moving)
            {
                showStatus("wait while moving!!!");//显示状态:电梯运动时要等待
            } else
            if(elevator.location != 2)//电梯的位置不在2层
            {
                controlBoard.setFloor1ButtonPressed();//电梯位于一层
                clickbutton.play();//电梯为向上运动,控制面板设置为向上
                elevator.setDestination(2);//目的地为2层
                 controlBoard.setFloor3ButtonPressed();//电梯位于3层
                clickbutton.play();//电梯为向下运动,控制面板设置为向下
                 elevator.setDestination(2);  //目的地为2层
            }
        } else  //第一层的电梯按钮,按此按钮使得电梯上一楼
        
        
        if(i >= clockWidth + floorWidth + elevatorWidth + 4 + 20 && i <= clockWidth + floorWidth + elevatorWidth + 4 + 20 + 20 && j >= 240 && j <= 260)
        {//电梯在运动
            if(elevator.moving)
            {
                showStatus("wait while moving!!!");//显示状态:电梯运动时要等待
            } else
            if(elevator.location != 1)//电梯的位置不在1层
            {
                controlBoard.setFloor3ButtonPressed();//电梯位于3层
                clickbutton.play();//电梯为向下运动,控制面板设置为向下
                elevator.setDestination(1);//目的地为1层
               controlBoard.setFloor2ButtonPressed();//电梯位于2层
                clickbutton.play();//电梯为向下运动,控制面板设置为向下
                elevator.setDestination(1);//目的地为1层 
            }
        } else   
        
        //使得一层乘客进入电梯
        
        if(i >= clockWidth + 190 && i <= clockWidth + 200 && j >= 215 && j <= 290)
        {//乘客1在楼层里
            if(firstFloor.occupied1)
            {//电梯不在一层
                if(elevator.location != 1)
                {
                    showStatus("Elevator is not on the First Floor! Call it first.");//显示电梯不在一层的状态,要将其调到一层
                    error.play();//调用错误操作的方法
                } else //电梯在运动时
                
                if(elevator.moving)
                {
                    showStatus("Person can't jump onto a moving elevator!!!");//显示乘客不能跳进运动电梯的状态
                } else  //乘客1已在电梯里
                if(elevator.occupied1)
                {
                    showStatus("There is already someone on the elevator!");//显示已在状态
                } else
                {
                    showStatus("Person on the First Floor is boarding the Elevator");//显示进入电梯状态
                    firstFloor.unoccupy1();//乘客1不在楼层里
                    elevator.occupy1();//乘客1在电梯里
                    //elevator.setDestination(2);
                }
            } else
            {
                showStatus("First Floor is unoccupied1!");
            }
        } else  
        if(i >= clockWidth + 180 && i <= clockWidth + 190 && j >= 215 && j <= 290)
        {
            if(firstFloor.occupied2)
            {
                if(elevator.location != 1)
                {
                    showStatus("Elevator is not on the First Floor! Call it first.");
                    error.play();
                } else
                if(elevator.moving)
                {
                    showStatus("Person  can't jump onto a moving elevator!!!");
                } else
                if(elevator.occupied2)
                {
                    showStatus("There is already someone on the elevator!");
                } else
                {
                    showStatus("Person on the First Floor is boarding the Elevator");
                    firstFloor.unOccupy2();
                    elevator.occupy2();
                    //elevator.setDestination(2);
                }
            } else
            {
                showStatus("First Floor is unoccupied1!");
            }
        }else
        if(i >= clockWidth + 170 && i <= clockWidth + 180 && j >= 215 && j <= 290)
        {
            if(firstFloor.occupied3)
            {
                if(elevator.location != 1)
                {
                    showStatus("Elevator is not on the First Floor! Call it first.");
                    error.play();
                } else
                if(elevator.moving)
                {
                    showStatus("Person can't jump onto a moving elevator!!!");
                } else
                if(elevator.occupied3)
                {
                    showStatus("There is already someone on the elevator!");
                } else
                {
                    showStatus("Person on the First Floor is boarding the Elevator");
                    firstFloor.unOccupy3();
                    elevator.occupy3();
                    //elevator.setDestination(2);
                }
            } else
            {
                showStatus("First Floor is unoccupied1!");
            }
        }else
        if(i >= clockWidth + 160 && i <= clockWidth + 170 && j >= 215 && j <= 290)
        {
            if(firstFloor.occupied4)
            {
                if(elevator.location != 1)
                {
                    showStatus("Elevator is not on the First Floor! Call it first.");
                    error.play();
                } else
                if(elevator.moving)
                {
                    showStatus("Person  can't jump onto a moving elevator!!!");
                } else
                if(elevator.occupied4)
                {
                    showStatus("There is already someone on the elevator!");
                } else
                {
                    showStatus("Person on the First Floor is boarding the Elevator");
                    firstFloor.unOccupy4();
                    elevator.occupy4();
                    //elevator.setDestination(2);
                }
            } else
            {
                showStatus("First Floor is unoccupied1!");
            }
        }else
        if(i >= clockWidth + 150 && i <= clockWidth + 160 && j >= 215 && j <= 290)
        {
            if(firstFloor.occupied5)
            {
                if(elevator.location != 1)
                {
                    showStatus("Elevator is not on the First Floor! Call it first.");
                    error.play();
                } else
                if(elevator.moving)
                {
                    showStatus("Person can't jump onto a moving elevator!!!");
                } else
                if(elevator.occupied5)
                {
                    showStatus("There is already someone on the elevator!");
                } else
                {
                    showStatus("Person on the First Floor is boarding the Elevator");
                    firstFloor.unOccupy5();
                    elevator.occupy5();
                    //elevator.setDestination(2);
                }
            } else
            {
                showStatus("First Floor is unoccupied1!");
            }
        }else
        if(i >= clockWidth + 140 && i <= clockWidth + 150 && j >= 215 && j <= 290)
        {
            if(firstFloor.occupied6)
            {
                if(elevator.location != 1)
                {
                    showStatus("Elevator is not on the First Floor! Call it first.");
                    error.play();
                } else
                if(elevator.moving)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -