import java.awt.*;
import java.awt.Graphics;
import java.util.Random;
class Ladder extends Canvas implements Runnable
{
Thread myLadder; //电梯线程
ladderButton button; //电梯按钮
int people=0; //电梯里最多有20个人
int floor=1; //楼层数
boolean flag=false; //是否到达目的地(电梯是否开门)true开门
boolean flag2=true; //绘制向上或向下箭头,true向上
boolean flag3=false; //控制paint()
Ring ring; //响铃
Random rand; //定义随机数生成器
int x_up[]={73,78,83}; //向上箭头的坐标
int y_up[][]={{640,630,640},{520,510,520},{400,390,400},{280,270,280},{160,150,160},{40,30,40}};
int x_down[]={85,90,95}; //向下箭头的坐标
int y_down[][]={{630,640,630},{510,520,510},{390,400,390},{270,280,270},{150,160,150},{30,40,30}};
Ladder(Frame f)
{
ring=new Ring();
rand=new Random();
myLadder=new Thread(this);
button=new ladderButton(f);
repaint();
}
public void paint(Graphics g)
{
g.drawLine(5,25,5,745);
g.drawLine(105,25,105,745);
for(int i=0;i<6;i++)
{
g.setColor(Color.GRAY);
g.drawRect(5,625-i*120,100,20);
g.fillRect(5,625-i*120,100,20);
g.setColor(Color.DARK_GRAY);
g.drawRect(7,647-i*120,46,96);
g.drawRect(57,647-i*120,46,96);
g.fillRect(7,647-i*120,46,96);
g.fillRect(57,647-i*120,46,96);
}
g.drawLine(5,745,105,745);
g.setFont(new Font("宋体",Font.BOLD,16)); //设置字体
g.setColor(Color.RED);
for(int i=0;i<6;i++)
{
if(people==20) g.drawString("FULL "+floor,15,640-i*120); ////画文字,电梯里最多有20个人
else g.drawString(""+floor,15,640-i*120);
}
if(flag2) //画箭头
{
for(int i=0;i<6;i++) {g.drawPolygon(x_up,y_up[i],3); g.fillPolygon(x_up,y_up[i],3);}
g.setColor(Color.GRAY);
for(int i=0;i<6;i++) {g.drawPolygon(x_down,y_down[i],3);g.fillPolygon(x_down,y_down[i],3); }
}
else if(!flag2)
{
for(int i=0;i<6;i++) {g.drawPolygon(x_down,y_down[i],3);g.fillPolygon(x_down,y_down[i],3); }
g.setColor(Color.GRAY);
for(int i=0;i<6;i++) {g.drawPolygon(x_up,y_up[i],3); g.fillPolygon(x_up,y_up[i],3);}
}
if(flag) arrived(g); //到达目的地
}
public void arrived(Graphics g) //电梯到达
{
int left=46,right=57;
ring.sound(); //响铃
while(left>6) //电梯门开
{
g.setColor(Color.WHITE);
g.drawRect(7,647-(floor-1)*120,left,96);
g.drawRect(right,647-(floor-1)*120,left,96);
g.fillRect(7,647-(floor-1)*120,left,96);
g.fillRect(right,647-(floor-1)*120,left,96);
left--; right++;
g.setColor(Color.DARK_GRAY);
g.drawRect(7,647-(floor-1)*120,left,96);
g.drawRect(right,647-(floor-1)*120,left,96);
g.fillRect(7,647-(floor-1)*120,left,96);
g.fillRect(right,647-(floor-1)*120,left,96);
try{ Thread.sleep(100); }
catch (Exception e) {}
}
people=people-rand.nextInt(10); //随机下p个人,p在0到5之间
if(people<0) people=0;
people=people+rand.nextInt(5); //随机上p个人,p在0到5之间
if(people>20) people=20;
try{ Thread.sleep(2000); } //3秒后关电梯门
catch (Exception e) {}
while(left<47) //电梯门关
{
g.setColor(Color.WHITE);
g.drawRect(7,647-(floor-1)*120,left,96);
g.drawRect(right,647-(floor-1)*120,left,96);
g.fillRect(7,647-(floor-1)*120,left,96);
g.fillRect(right,647-(floor-1)*120,left,96);
left++; right--;
g.setColor(Color.DARK_GRAY);
g.drawRect(7,647-(floor-1)*120,left,96);
g.drawRect(right,647-(floor-1)*120,left,96);
g.fillRect(7,647-(floor-1)*120,left,96);
g.fillRect(right,647-(floor-1)*120,left,96);
try{ Thread.sleep(100); }
catch (Exception e) {}
}
flag3=true;
}
public void run()
{
while(true)
{
int n=0;
for(n=0;n<6;n++) //检测是否有需要处理的信息
{
if(button.getStatusUp(n)!=0||button.getStatusDown(n)!=0||button.getFloorUp(n)!=0||button.getFloorDown(n)!=0) break;
}
if(n<6) //有需要处理的信息
{
while(floor<7) //电梯向上
{
flag2=true; //标记绘制向上箭头
boolean mark=false; //标记同一层楼即按了外面的按钮,有按了里面的按钮
if(button.getStatusUp(floor-1)==1) //处理外面的按钮
{
button.setUpOrDown(1); //电梯当前状态为向上
flag=true;
repaint();
while(true)
{
if(flag3) {flag3=false;break;}
}
button.setStatusUp(floor-1);
button.setButtonUpBackground(floor-1);
flag=false;
if(button.getFloorUp(floor-1)!=0) mark=true; //同一层楼即按了外面的按钮,又按了里面的按钮
}
if(button.getFloorUp(floor-1)!=0) //处理里面的按钮
{
button.setUpOrDown(1); //电梯当前状态为向上
flag=true;
button.setFloorUp(floor-1);
button.setButtonNumBackground(floor-1);
if(!mark)
{
repaint();
while(true)
{
if(flag3) {flag3=false;break;}
}
}
flag=false;
}
try{ Thread.sleep(2*1000); }
catch (Exception e) {}
int m;
for(m=floor;m<7;m++)
{
if(button.getStatusUp(m-1)!=0||button.getFloorUp(m-1)!=0||button.statusDown[m-1]!=0) break;
}
if(m<7)
{
floor++;
if(floor>6) {floor=6;button.setFloor(floor);break;}
else {button.setFloor(floor);repaint();}
}
else break;
}
while(floor>0) //电梯向下
{
flag2=false;
boolean mark=false;
if(button.getStatusDown(floor-1)==-1)
{
button.setUpOrDown(-1);
flag=true;
repaint();
while(true)
{
if(flag3) {flag3=false;break;}
}
button.setStatusDown(floor-1);
button.setButtonDownBackground(floor-1);
flag=false;
if(button.getFloorDown(floor-1)!=0) mark=true; //同一层楼即按了外面的按钮,有按了里面的按钮
}
if(button.getFloorDown(floor-1)!=0)
{
button.setUpOrDown(-1);
flag=true;
button.setFloorDown(floor-1);
button.setButtonNumBackground(floor-1);
if(!mark)
{
repaint();
while(true)
{
if(flag3) {flag3=false;break;}
}
}
flag=false;
}
try{ Thread.sleep(2*1000); }
catch (Exception e) {}
int m;
for(m=floor;m>0;m--)
{
if(button.getStatusDown(m-1)!=0||button.getFloorDown(m-1)!=0||button.statusUp[m-1]!=0) break;
}
if(m>0)
{
floor--;
if(floor<1) {floor=1;button.setFloor(floor);break;}
else {button.setFloor(floor);repaint();}
}
else break;
}
}
else
{
try{ Thread.sleep(1000); }
catch(Exception e) {}
}
}
}
}