Hi,
I want someone to explain this code ( method ), and how it works.؟؟
Especially the process of moving the elevator
// The elevator class draws the elevator area and simulates elevator movement
class Elevator extends JPanel implements ActionListener {
// Declaration of variables
private Elevator_Simulation app; // Elevator Simulation frame
private boolean up; // Elevator is moving up or down
private boolean firstRun; // Elevator initial position flag
private int width; // Elevator's width
private int height; // Elevator's height
private int xco; // X coordinate of the elevator's upper left corner
private int yco; // Y coordinate of the elevator's upper left corner
private int dy0; // Moving interval
private int topy; // Y coordinate of the top level
private int bottomy; // Y coordinate of the bottom level
private Timer timer; // Timer to drive the elevator movement
final int MoveDelay = 40; // Time between moves
final int LoadTime = 99000; // Time to wait while loading
// Constructor
public Elevator(Elevator_Simulation app) {
// Initialization of variables
setBackground(Color.white);
this.app = app;
up = true;
firstRun = true;
dy0 = 3;
timer = new Timer(MoveDelay, this);
}
// Paint elevator area
@Override
public void paintComponent(Graphics g) {
// Obtain geometric values of components for drawing the elevator area
width = app.control.floorButton[0].getWidth();
height = app.control.floorButton[0].getHeight();
// Place elevator in middle
xco = (getWidth() - width) / 2;
if (firstRun) {
yco = getHeight() - height;
firstRun = false;
}
topy = 0;
// Resize the window will make bottomy value inaccruate
bottomy = this.getHeight() - height;
// Clear the painting canvas
super.paintComponent(g);
// Start the Timer if not started elsewhere
if (!timer.isRunning()) {
timer = new Timer(MoveDelay, this);
timer.setInitialDelay(1000);
timer.start();
}
// Draw horizontal lines
for (int i = 0; i <= 9; i++) {
g.setColor(Color.black);
g.drawLine(0, height * i, getWidth(), height * i);
}
// Draw elevator
g.setColor(Color.GRAY);
g.fillRect(xco, yco, width, height);
// Draw vertical line striking through elevator
g.setColor(Color.black);
g.drawLine(xco + (width / 2), yco, xco + (width / 2), (yco + height));
}
// Handle the timer events
@Override
public void actionPerformed(ActionEvent e) {
int current = (yco / height);
if (up) {
app.state.setText("The elevator is moving up");
yco -= dy0;
if (app.control.bp[current]) {
if ((yco + height) >= (height * current) && (yco + height) <= (height * (current + 1))) {
timer.stop();
app.control.bp[current] = false;
app.control.floorButton[current].setBackground(Color.blue);
app.state.setText("The elevator is picking up passengers at floor " + (8 - current));
}
}
if (yco <= topy) {
up = false;
}
} else {
app.state.setText("The elevator is moving down");
yco += dy0;
if (app.control.bp[current]) {
if ((yco) >= (height * current) && (yco) <= (height * (current + 1))) {
timer.stop();
app.control.bp[current] = false;
app.control.floorButton[current].setBackground(Color.blue);
app.state.setText("The elevator is picking up passengers at floor " + (8 - current));
}
}
if (yco >= bottomy) {
up = true;
}
}
// Repaint the panel
repaint();
}
} // The end of Elevator class