import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class TestElevator
{
public static void main(String[] args)
{
SimpleFrame frame = new SimpleFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //退出模式,退出应用程序后的默认窗口关闭操作
frame.setLocation(40, 40); //窗口位置坐标
frame.show();
}
}
class SimpleFrame extends JFrame
{
public SimpleFrame()
{
setSize(690, 510);
setTitle("电梯模拟");
SimplePanel panel = new SimplePanel();
this.add(panel);
}
}
class SimplePanel extends JPanel
{
private CanvasOuter myCanvasOuter;//显示外部按钮的画布类,位于左侧
private CanvasInner myCanvasInner;//显示内部按钮的画布类,位于右侧
private Elevator[] s;//电梯数组
private Found foundThread = new Found();//任务分配线程
public final static int NUM = 2; //电梯数
public final static int FLOOR = 6; //楼层
final int UP = 0; //电梯状态初始化
final int DOWN = 1;
final int PAUSE = 2;
final int OPEN = 3;
final int CLOSE = 4;
public SimplePanel()
{
setLayout(null);
myCanvasOuter = new CanvasOuter();
myCanvasInner = new CanvasInner();
add(myCanvasOuter);
add(myCanvasInner);
s = new Elevator[NUM];
for (int i = 0; i < s.length; i++)
{
s[i] = new Elevator(i);
}
for (int i = 0; i < s.length; i++)
{
s[i].start();//
}
foundThread.start();
}
class Found extends Thread //任务分配线程
{
private ArrayList tasks;
private Task task;
public Found()
{
tasks = new ArrayList();
}
public void addTask(int i, boolean flag)
{
tasks.add(new Task(i, flag));
}
public void run()
{
while (true)
{
if (!tasks.isEmpty())
{
task = (Task) (tasks.get(0));
int i = task.floor;
int minFloor = FLOOR;
int id = 0;
boolean f = task.isUp;
boolean isFound = false;
if (f)//要上楼
{
for (int j = 0; j < NUM; j++)//电梯数
{
if ((s[j].state == PAUSE) || (s[j].current < i && s[j].state == UP))//停或者电梯正在从下往上
{
int distance = Math.abs(i - s[j].current);
if (distance < minFloor)
{
id = j;
minFloor = distance;
isFound = true;
}
}
}
}
else//下楼
{
for (int j = 0; j < NUM; j++)
{
if ((s[j].state == PAUSE) || (s[j].current > i && s[j].state == DOWN))//停或正在从上往下
{
int distance = Math.abs(i - s[j].current);
if (distance < minFloor)
{
id = j;
minFloor = distance;
isFound = true;
}
}
}
}
if (isFound)
{
s[id].addFloor(i);
tasks.remove(0);
}
}
try
{
sleep(100);
}
catch (InterruptedException e)
{
System.out.println("Interrupted");
}
}
}
class Task//任务:要到几楼,上去还是下去
{
private int floor;
private boolean isUp;
public Task(int i, boolean flag)
{
floor = i;
isUp = flag;
}
}
}
public void resetOuterB(int floor) //设置外部的按钮
{
myCanvasOuter.bUp[floor].setEnabled(true);
myCanvasOuter.bDown[floor].setEnabled(true);
myCanvasOuter.bUp[floor].setForeground(null);
myCanvasOuter.bDown[floor].setForeground(null);
}
public void drawButtons(int id)//设置内部按钮
{
for (int i = 0; i < FLOOR; i++)
{
if (s[id].destinations.contains(new Integer(i)))
{
myCanvasInner.interPanels[id].interB[i].setBackground(Color.YELLOW);
}
else
{
myCanvasInner.interPanels[id].interB[i].setBackground(null);
}
}
}
class Elevator extends Thread //电梯主类
{
private int id; //电梯标识号
private int floor = FLOOR; //总层数
private JPanel myPanel = new JPanel();
private JPanel myPanel1 = new JPanel();
private JButton numB;
private JButton stateB = new JButton("---");
private JButton[] buttons; //电梯组成
public int current = 1; //所在楼层
public int state = PAUSE; //上下行标志
private JButton floorB = new JButton("1");
private ArrayList destinations; //目的地链表
public Elevator(int x)
{
id = x;
myPanel1.setLayout(new GridLayout());
numB = new JButton("第"+(x+1)+"号电梯");
myPanel1.add(numB);
myPanel.setLayout(new GridLayout(floor + 2, 1));
floorB.setBackground(Color.WHITE);
myPanel.add(floorB);
myPanel.add(stateB);
buttons = new JButton[FLOOR];
for (int i = buttons.length - 1; i >= 0; i--)
{
buttons[i] = new JButton();
buttons[i].setBackground(Color.LIGHT_GRAY);
myPanel.add(buttons[i]);
}
buttons[0].setBackground(Color.RED);
myPanel1.setBounds(100 * id + 250, 10, 100, 50);
myPanel.setBounds(100 * id + 250, 60, 100, 400);
add(myPanel1);
add(myPanel);
destinations = new ArrayList();
}
public void addFloor(int i) //响应外部按钮
{
if (destinations.contains(new Integer(i)))
return;
if (state == PAUSE)
{
destinations.add(new Integer(i));
buttons[i].setBackground(Color.YELLOW);
if (current > i)
{
state = DOWN;
}
else
{
state = UP;
}
return;
}
if (state == UP)
{
for (int j = 0; j < destinations.size(); j++)
{
if (i < ((Integer) (destinations.get(j))).intValue())
{
destinations.add(j, new Integer(i));
buttons[i].setBackground(Color.YELLOW);
}
}
}
if (state == DOWN)
{
for (int j = 0; j < destinations.size(); j++)
{
if (i > ((Integer) (destinations.get(j))).intValue())
{
destinations.add(j, new Integer(i));
buttons[i].setBackground(Color.YELLOW);
}
}
}
destinations.add(new Integer(i));
buttons[i].setBackground(Color.YELLOW);
}
public void wantToFloor(int i) //响应内部按钮
{
if (destinations.contains(new Integer(i)))
return;
if (state == PAUSE)
{
destinations.add(new Integer(i));
buttons[i].setBackground(Color.YELLOW);
if (current > i + 1)
{
state = DOWN;
}
else
{
state = UP;
}
return;
}
if (state == UP)
{
if (current > i + 1)
return;
for (int j = 0; j < destinations.size(); j++)
{
if (i < ((Integer) (destinations.get(j))).intValue())
{
destinations.add(j, new Integer(i));
buttons[i].setBackground(Color.YELLOW);
return;
}
}
}
if (state == DOWN)
{
if (current < i)
return;
for (int j = 0; j < destinations.size(); j++)
{
if (i > ((Integer) (destinations.get(j))).intValue())
{
destinations.add(j, new Integer(i));
buttons[i].setBackground(Color.YELLOW);
return;
}
}
}
destinations.add(new Integer(i));
buttons[i].setBackground(Color.YELLOW);
}
public void setStateB() //设置运行状态按钮
{
if (state == PAUSE)
{
stateB.setText("---");
stateB.setForeground(null);
myCanvasInner.interPanels[id].stateB.setText("---");
myCanvasInner.interPanels[id].stateB.setForeground(null);
myCanvasOuter.stateB1.setText("---");
myCanvasOuter.stateB1.setForeground(null);
myCanvasOuter.stateB2.setText("---");
myCanvasOuter.stateB2.setForeground(null);
}
else if (state == UP)
{
stateB.setText("上");
stateB.setForeground(Color.RED);
myCanvasInner.interPanels[id].stateB.setText("上");
myCanvasInner.interPanels[id].stateB.setForeground(Color.RED);
if(id==0)
{
myCanvasOuter.stateB1.setText("上");
myCanvasOuter.stateB1.setForeground(Color.RED);
}
if(id==1)
{
myCanvasOuter.stateB2.setText("上");
myCanvasOuter.stateB2.setForeground(Color.RED);
}
}
else
{
stateB.setT
java项目实践之电梯模拟调度算法
4星 · 超过85%的资源 需积分: 16 134 浏览量
2013-06-03
21:44:49
上传
评论 1
收藏 4KB ZIP 举报
pzb295
- 粉丝: 51
- 资源: 1
最新资源
- C语言基础-C语言编程基础之Leetcode编程题解之第30题串联所有单词的子串.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第29题两数相除.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第28题找出字符串中第一个匹配项的下标.zip
- 实验报告模板(1).docx
- C语言基础-C语言编程基础之Leetcode编程题解之第26题删除有序数组中的重复项.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第25题K个一组翻转链表.zip
- hnu计算机系统作业-计算机系统基础课程大作业.zip
- 树莓派app.apk
- C++的基于同态加密技术的匿名电子投票系统源码.zip
- SW建模格式图.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈