?? elevatormgr.java
字號:
/*文件名: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 + -