Elevator.java
public class Elevator{
TextField tf1;
TextField tf2;
//显示界面的TextField队列
private ArrayList<TextField> textFields = new ArrayList<TextField>();
public void showUI(){
//窗口初始化
javax.swing.JFrame window = new javax.swing.JFrame();
window.setSize(800, 1000);
window.setTitle("电梯算法");
window.setDefaultCloseOperation(3);
window.setLocationRelativeTo(null);
JPanel jPanel = new JPanel();
jPanel.setPreferredSize(new Dimension(0, 100));
jPanel.setBackground(Color.white);
Label l1 = new Label("Previews:");
Label l2 = new Label("Now:");
tf1 = new TextField("143");
tf2 = new TextField("125");
jPanel.add(l1);
jPanel.add(tf1);
jPanel.add(l2);
jPanel.add(tf2);
Button button1 = new Button("START");
button1.setPreferredSize(new Dimension(100, 60));
jPanel.add(button1);
Button button = new Button("NEXT");
button.setPreferredSize(new Dimension(100, 60));
jPanel.add(button);
JPanel jPanel2 = new JPanel();
//设置网格布局
GridLayout gridLayout = new GridLayout(10,20,0,3);
jPanel2.setLayout(gridLayout);
//添加TextView作为柱面
for(int i=0;i<200;++i){
TextField textField = new TextField(""+i);
jPanel2.add(textField, gridLayout);
textFields.add(textField);
}
jPanel2.setBackground(Color.blue);
window.add(jPanel,BorderLayout.NORTH);
window.add(jPanel2, BorderLayout.CENTER);
window.setVisible(true);
button.addActionListener(this);
button1.addActionListener(this);
}
//存储柱面号的数组
private int a[] = new int[]{86,147,91,165,177,94,150,102,175,130};
//这个后面会用到
int b[] = new int[]{86,147,91,177,94,150,102,165,175,130};
//需要访问柱面的队列
private ArrayList<Cylinder> cylinderlist = new ArrayList<Cylinder>();
//初始化访问的柱面
public void init(){
cylinderlist = new ArrayList<Cylinder>();
for(int i=0;i<a.length;++i){
Cylinder cylinder = new Cylinder(a[i]);
cylinderlist.add(cylinder);
}
//使用匿名类排序
Collections.sort(cylinderlist, new Comparator<Cylinder>() {
public int compare(Cylinder h1, Cylinder h2) {
return h1.number-h2.number;
}
});
//将要访问的柱面设为红色
for(int i=0;i<b.length;++i){
textFields.get(b[i]).setBackground(Color.RED);
}
//当前柱面号设为灰色
textFields.get(now).setBackground(Color.gray);
}
private int now = 125;//当前柱面号
private int previews =143;//之前完成的柱面号服务请求
public void findMin(){
//赋值给完成的柱面号
previews = now;
//之前已经用匿名类排序后,只要选择队列中该位置的后一个元素就可以了
int no = 0;
for(int i=cylinderlist.size()-1;i>=0;--i){
Cylinder cylinder = cylinderlist.get(i);
if(cylinder.number>=now)no = i;
}
Cylinder c = cylinderlist.get(no);
//访问到后将选中的柱面移除访问序列
cylinderlist.remove(no);
//将选中的柱面号赋值给当前移动臂在的位置
now = c.number;
}
public void findMax(){
previews = now;
int no = 0;
for(int i=0;i<cylinderlist.size();++i){
Cylinder cylinder = cylinderlist.get(i);
if(cylinder.number<=now)no = i;
}
Cylinder c = cylinderlist.get(no);
cylinderlist.remove(no);
now = c.number;
}
public void display(){
//判断如果是向里移动
if(previews-now<0){
//循环为之前完成的柱面号至当前柱面号
for(int k=previews;k<now;++k){
//将当前TextField的颜色置为灰色
TextField textField2 = textFields.get(k+1);
textField2.setBackground(Color.GRAY);
//将刚才访问过的TextField颜色置为白色
TextField textField = textFields.get(k);
textField.setBackground(Color.WHITE);
//休眠时间,可以调整移动的速度
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//与上面类似
if(previews-now>0){
for(int k=previews;k>now;--k){
TextField textField2 = textFields.get(k-1);
textField2.setBackground(Color.GRAY);
TextField textField = textFields.get(k);
textField.setBackground(Color.WHITE);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void actionPerformed(ActionEvent e) {
//获取点击事件
String event = e.getActionCommand();
//判断点击了哪个按钮
switch (event) {
case "NEXT":
if(cylinderlist.size()>0){
//向外移动调用findMin()方法
if ((now-previews)>0&&now!=199){
findMin();
//输出看一下结果
System.out.println("previews:"+previews+" now:"+now);
//显示效果
display();
}else {
//向里移动调用findMax()方法
findMax();
System.out.println("previews:"+previews+" now:"+now);
display();
}
}else{
System.out.println("已完成");
}
break;
case "START":
//获取文本框中的数字
previews = Integer.parseInt(tf1.getText());
now = Integer.parseInt(tf2.getText());
//初始化
init();
System.out.println(now+" "+previews);
break;
default:
break;
}
}
public static void main(String[] args) {
Elevator elevator = new Elevator();
elevator.showUI();
}
}