📄 lift.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.net.*;
/**
* @author baihongfei
* SSE OF TONGJI UNIVERSITY
* THE DETAILED CLASS OF LIFT
*/
//任务类型:0 只需响应电梯内人 1 只需响应电梯外的人 2 两者都要响应
class Task
{
private int floor=0;
private int type=0;
public Task(int i){
floor = i;
}
public Task(int i,int t){
floor = i;
type = t;
}
public int getTask(){
return floor;
}
public int getType(){
return type;
}
public void setTyep(int t){
type = t;
}
}
public class Lift extends JFrame implements Runnable{
Lift t;
Thread self = null;
JLabel label = new JLabel("begin");
final static int NUM_FLOOR = 20;
JLabel info = new JLabel();
JLabel outside = null;
JPanel content = new JPanel();
JPanel choice = new JPanel();
JPanel down = new JPanel();
JButton openbt = new JButton();
JButton closebt= new JButton();
JButton alarmbt= new JButton();
final JButton[] num = new JButton[NUM_FLOOR];
int floorID=1;
LiftDemo controlPn;
int liftID=0;
int estate = 0;
int target=0;
boolean isopen=false;
boolean isstop=false;
LinkedList upTasks=new LinkedList();
LinkedList downTasks=new LinkedList();
LinkedList run=upTasks;
LinkedList unrun=downTasks;
LinkedList outup=new LinkedList();
LinkedList outdown=new LinkedList();
LinkedList whole=new LinkedList();
final static ImageIcon[] fg=new ImageIcon[NUM_FLOOR];
final static ImageIcon[] bg=new ImageIcon[NUM_FLOOR];
final static ImageIcon[] state=new ImageIcon[NUM_FLOOR];
public Lift(String s,JLabel out,LiftDemo control,int liftid){
super(s);
outside = out;
t=this;
liftID=liftid;
this.controlPn = control;
JPanel p=new JPanel();
for(int i=0;i<NUM_FLOOR;i++)
{
String imageName = "LiftImage/" + (i+1) + ".gif";
URL iconURL = ClassLoader.getSystemResource(imageName);
ImageIcon icon = new ImageIcon(iconURL);
fg[i] = icon;
}
for(int i=0;i<NUM_FLOOR;i++)
{
String imageName = "LiftImage/b" + (i+1) + ".gif";
URL iconURL = ClassLoader.getSystemResource(imageName);
ImageIcon icon = new ImageIcon(iconURL);
bg[i] = icon;
}
for(int i=0;i<NUM_FLOOR;i++)
{
String imageName = "LiftImage/s" + (i+1) + ".gif";
URL iconURL = ClassLoader.getSystemResource(imageName);
ImageIcon icon = new ImageIcon(iconURL);
state[i] = icon;
}
String imageName = "LiftImage/open.gif";
URL iconURL = ClassLoader.getSystemResource(imageName);
ImageIcon open = new ImageIcon(iconURL);
imageName = "LiftImage/close.gif";
iconURL = ClassLoader.getSystemResource(imageName);
ImageIcon close = new ImageIcon(iconURL);
imageName = "LiftImage/alarm.gif";
iconURL = ClassLoader.getSystemResource(imageName);
ImageIcon alarm = new ImageIcon(iconURL);
for(int i=0;i<NUM_FLOOR;i++)
{
final int index=i;
num[i]=new JButton(fg[i]);
num[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
if(floorID==index+1)return;
num[index].setIcon(bg[index]);
int temp = index+1;
int up=-1;
if(temp>floorID){
for(int i=0;i<upTasks.size();i++){
int tt=((Task)upTasks.get(i)).getTask();
if(temp == tt)return;
if(tt>temp){up=i;break;}
}
if(up!=-1){
Task t=new Task(temp);
upTasks.add(up,t);
return;
}
else{
Task t=new Task(temp);
upTasks.addLast(t);
return;
}
}
else{
for(int i=0;i<downTasks.size();i++){
int tt=((Task)downTasks.get(i)).getTask();
if(temp == tt)return;
if(tt<temp){up=i;break;}
}
if(up!=-1){
Task t=new Task(temp);
downTasks.add(up,t);
return;
}
else{
Task t=new Task(temp);
downTasks.addLast(t);
return;
}
}
}
});
choice.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
choice.setLayout(new GridLayout(5,4,5,5));
choice.add(num[i]);
}
JPanel right=new JPanel();
right.setLayout(new BorderLayout());
right.add(new JLabel("Now On:"),BorderLayout.PAGE_START);
info.setIcon(state[0]);
right.add(info,BorderLayout.PAGE_END);
JPanel left=new JPanel();
openbt.setIcon(open);
openbt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(isstop){
isopen=true;
openbt.setEnabled(false);
}
}
});
closebt.setIcon(close);
closebt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
isopen=false;
openbt.setEnabled(true);
}
});
alarmbt.setIcon(alarm);
alarmbt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
controlPn.help(liftID,floorID);
}
});
left.add(openbt);
left.add(closebt);
left.add(alarmbt);
down.setLayout(new BorderLayout());
down.setBorder(BorderFactory.createEmptyBorder(6,6,10,10));
down.add(left,BorderLayout.LINE_START);
down.add(right,BorderLayout.LINE_END);
content.setLayout(new BorderLayout());
content.add(choice,BorderLayout.PAGE_START);
content.add(down,BorderLayout.PAGE_END);
this.getContentPane().add(content);
}
//根据任务评估电梯响应的大概时间(假设停在某层耗费3秒 经过某层耗费1秒)
public int judge(int id,int dir){
if(id == floorID && dir == estate)return 0;
if(estate ==0 )return Math.abs(floorID - id);
else{
int end=0;
boolean isup=true;
whole.addAll(0,run);
int a=whole.size();
whole.addAll(whole.size(),unrun);
int b=whole.size();
if(run==upTasks){
whole.addAll(whole.size(),outup);
isup=true;
}
if(run==downTasks)
{
whole.addAll(whole.size(),outdown);
isup=false;
}
int c=whole.size();
if(estate == dir){
if(estate == 2 && id>floorID){
int up= -1;
for(int i=0; i<a; i++){
int tt=((Task)whole.get(i)).getTask();
if(tt>id){up=i;break;}
}
if(up!=-1){
Task t=new Task(id);
whole.add(up,t);
end = up;
}
else{
Task t=new Task(id);
whole.add(a,t);
end = a;
}
}
if(estate ==1 && id<floorID){
int up= -1;
for(int i=0; i<a; i++){
int tt=((Task)whole.get(i)).getTask();
if(tt<id){up=i;break;}
}
if(up!=-1){
Task t=new Task(id);
whole.add(up,t);
end = up;
}
else{
Task t=new Task(id);
whole.add(a,t);
end = a;
}
}
if(estate ==2 && id<floorID){
int up= -1;
for(int i=b; i<c; i++){
int tt=((Task)whole.get(i)).getTask();
if(tt>id){up=i;break;}
}
if(up!=-1){
Task t=new Task(id);
whole.add(up,t);
end = up;
}
else{
Task t=new Task(id);
whole.add(c,t);
end = c;
}
}
if(estate ==1 && id>floorID ){
int up= -1;
for(int i=b; i<c; i++){
int tt=((Task)whole.get(i)).getTask();
if(tt<id){up=i;break;}
}
if(up!=-1){
Task t=new Task(id);
whole.add(up,t);
end = up;
}
else{
Task t=new Task(id);
whole.add(c,t);
end = c;
}
}
}
if(estate != dir){//反向
int up= -1;
for(int i=a; i<b; i++){
int tt=((Task)whole.get(i)).getTask();
if(isup){
if(tt<id){up=i;break;}
}
else{
if(tt>id){up=i;break;}
}
}
if(up!=-1){
Task t=new Task(id);
whole.add(up,t);
end = up;
}
else{
Task t=new Task(id);
whole.add(b,t);
end = b;
}
}
int x=0,y=0;
int sum = 0;
for(int j=0;j<=end;j++,x=y){
if(j==0)x=floorID;
y=((Task)whole.get(j)).getTask();
sum=sum+Math.abs(y-x);
}
for(int i=0;i<whole.size();i++){
int tt=((Task)whole.get(i)).getTask();
System.out.println("$$$$$Lift "+liftID+"wholequeue:"+tt);
}
System.out.println("@@@@@outside task(floor"+id+",dir"+dir+"):lift "+liftID+" time "+(sum+end*3));
whole.clear();
return (sum + end*3);
}
}
public boolean rmTask(int id,int dir){
if(dir==2){
for(int i=0;i<upTasks.size();i++){
Task temp = (Task)upTasks.get(i);
int t = temp.getType();
int l = temp.getTask();
if(id == l){
if(t==1){
upTasks.remove(i);
}
if(t==2){
temp.setTyep(0);
}
return true;
}
}
for(int i=0;i<outup.size();i++){
Task temp = (Task)outup.get(i);
int t = temp.getType();
int l = temp.getTask();
if(id == l){
if(t==1){
outup.remove(i);
}
if(t==2){
temp.setTyep(0);
}
return true;
}
}
return false;
}
if(dir==1){
for(int i=0;i<downTasks.size();i++){
Task temp = (Task)downTasks.get(i);
int t = temp.getType();
int l = temp.getTask();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -