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

Recommended Answers

The code is already very heavily commented so it would be hard for us to explain what it does more than the programmer who wrote it and commented their code while doing so.

Perhaps if you could give us a better example of what you're looking for? You say the …

Jump to Post

All 2 Replies

The code is already very heavily commented so it would be hard for us to explain what it does more than the programmer who wrote it and commented their code while doing so.

Perhaps if you could give us a better example of what you're looking for? You say the process of moving the elevator ... the entire thing simulates an elevator moving. Do you mean your homework assignment is to convert the existing code to pseudocode?

Yes , my assignment is about describing a step-by-step description of implementation.

Like:1. Can you explain in detail how to use the elevator movement method of the elevator up or down in the previous example.
2.Explain the method used to draw the elevator in the previous example.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.