📄 elevatorsimulation.java
字号:
package adminClient;
/**************************************************************************************
* ************************************************************************************
* 此类刻画电梯运行界面*****************************************************************
* ***************************************by darkalex.eos******************************
*/
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class ElevatorSimulation extends JPanel {
int floor1;// 描述电梯当前所在的楼层
int floor2;
int floor3;
int floor4;
String direction1;// 描述电梯运行方向
String direction2;
String direction3;
String direction4;
double openper1;// 电梯的开门参数,用于模拟开门关门
double openper2;
double openper3;
double openper4;
Color defaultcolor = new Color(100, 100, 100);
Color color = new Color(110, 110, 110);
Color color2 = new Color(180, 180, 180);
public ElevatorSimulation() {
floor1 = 1;
floor2 = 1;
floor3 = 1;
floor4 = 1;
direction1 = "up";
direction2 = "up";
direction3 = "up";
direction4 = "up";
openper1 = 0;
openper2 = 0;
openper3 = 0;
openper4 = 0;
}
public ElevatorSimulation(int floor1, int floor2, int floor3, int floor4) {
this.floor1 = floor1;
this.floor2 = floor2;
this.floor3 = floor3;
this.floor4 = floor4;
}
public int getFloor1() {
return floor1;
}
public void setFloor1(int floor1, String direction1, double openper1) {
this.floor1 = floor1;
this.direction1 = direction1;
this.openper1 = openper1;
repaint();
}
public int getFloor2() {
return floor2;
}
public void setFloor2(int floor2, String direction2, double openper2) {
this.floor2 = floor2;
this.direction2 = direction2;
this.openper2 = openper2;
repaint();
}
public int getFloor3() {
return floor3;
}
public void setFloor3(int floor3, String direction3, double openper3) {
this.floor3 = floor3;
this.direction3 = direction3;
this.openper3 = openper3;
repaint();
}
public int getFloor4() {
return floor4;
}
public void setFloor4(int floor4, String direction4, double openper4) {
this.floor4 = floor4;
this.direction4 = direction4;
this.openper4 = openper4;
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
@SuppressWarnings("unused")
FontMetrics floor01 = g.getFontMetrics();
Font font = new Font("", Font.BOLD, 20);
String s1 = "";
String s2 = "";
String s3 = "";
String s4 = "";
if (floor1 == 10) {
s1 = "" + floor1 + " " + direction1;
} else {
s1 = "" + floor1 + " " + direction1;
}
if (floor2 == 10) {
s2 = "" + floor2 + " " + direction2;
} else {
s2 = "" + floor2 + " " + direction2;
}
if (floor3 == 10) {
s3 = "" + floor3 + " " + direction3;
} else {
s3 = "" + floor3 + " " + direction3;
}
if (floor4 == 10) {
s4 = "" + floor4 + " " + direction4;
} else {
s4 = "" + floor4 + " " + direction4;
}
g.setColor(Color.RED);
g.setFont(font);
g.drawString(s1, getWidth() / 8 - 30, 40);
g.drawString(s2, getWidth() / 8 * 3 - 30, 40);
g.drawString(s3, getWidth() / 8 * 5 - 30, 40);
g.drawString(s4, getWidth() / 8 * 7 - 30, 40);
g.setColor(color);
g.drawLine(getWidth() / 8 - 70, 470, 7 * getWidth() / 8 + 70, 470);//下框
g.drawLine(getWidth() / 8 - 70, 50, 7 * getWidth() / 8 + 70, 50);//上框
g.drawLine(getWidth() / 8 - 70, 50, getWidth() / 8 - 70, 470);//左框
g.drawLine(7 * getWidth() / 8 + 70, 50, 7 * getWidth() / 8 + 70, 470);//右框
for (int i = 0; i < 10; i++) {
//电梯1构图
g.setColor(color);
if ((i + 1) == floor1) {
g.setColor(color2);
}
g.fill3DRect(getWidth() / 8 - 60, (420 - 40 * i), 120, 40, true);
if ((i + 1) == floor1) {
g.setColor(Color.WHITE);
g.fill3DRect(getWidth() / 8 - 60 + (int) (120 - 120 * openper1) / 2,
(420 - 40 * i), (int) (120 * openper1), 40, true);
// 根据openedpercent参数画矩形模拟开门关门
}
//电梯2构图
g.setColor(color);
if ((i + 1) == floor2) {
g.setColor(color2);
}
g.fill3DRect(getWidth() / 8 * 3 - 60, (420 - 40 * i), 120, 40, true);
if ((i + 1) == floor2) {
g.setColor(Color.WHITE);
g.fill3DRect(getWidth() / 8 * 3 - 60 + (int) (120 - 120 * openper2)/ 2,
(420 - 40 * i), (int) (120 * openper2), 40, true);
// 根据openedpercent参数画矩形模拟开门关门
}
//电梯3构图
g.setColor(color);
if ((i + 1) == floor3) {
g.setColor(color2);
}
g.fill3DRect(getWidth() / 8 * 5 - 60, (420 - 40 * i), 120, 40, true);
if ((i + 1) == floor3) {
g.setColor(Color.WHITE);
g.fill3DRect(getWidth() / 8 * 5 - 60+ (int) (120 - 120 * openper3) / 2,
(420 - 40 * i), (int) (120 * openper3), 40, true);
// 根据openedpercent参数画矩形模拟开门关门
}
//电梯4构图
g.setColor(color);
if ((i + 1) == floor4) {
g.setColor(color2);
}
g.fill3DRect(getWidth() / 8 * 7 - 60, (420 - 40 * i), 120, 40, true);
if ((i + 1) == floor4) {
g.setColor(Color.WHITE);
g.fill3DRect(getWidth() / 8 * 7 - 60+ (int) (120 - 120 * openper4) / 2,
(420 - 40 * i), (int) (120 * openper4), 40, true);
// 根据openedpercent参数画矩形模拟开门关门
}
}
}
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -