somehow it does not stop.

You need to debug your code to see what is happening.

hi there,

how i am suppose to clear a label in actionevent e? any feedback appreciated.

Hi there,

i did the debug. the elevator did stop but not exactly at the floor area. is there any other way to stop the movement instead of using timer?

is there any other way to stop the movement

The Timer is what changes the location of the elevator. Without some code to change the location of the elevator it would always be at the same location. You are using a Timer to change the location.

The movement will stop if there is no code (like the timer) changing its location.

hi there,

but i have put the movement of the elevator inside and i dont think i will remove it because if not the elevator wont move at all..thanks.

I thought you wanted the elevator to stop moving for a short time.
Use one timer to move the elevator
and another timer to pause it at a floor.

hi there,

tried that..but it gives me event exception error.

the codes:

if ((yco % height == 0) && (app.control.bp[current] == true)) {
            String line0 = ("Elevator is picking up passenger at floor " +
                     app.control.b[current].getActionCommand().substring(1));
            app.state.setText(line0);
            tm1.setDelay(1000);
            app.control.b[current].setBackground(Color.cyan);
        }

it gives me event exception error.

Please copy the full text of the message and paste it here.

Also there is no way to see the error in your program from a small piece of code

hi,

i tink the error caused because i have not declare a new timer yet.

Anyway, even after i declare a new timer, its still does not try to stop the elevator for a single moment.

Should i put the codes inside the event class of the elevator and whether tm1.setDelay() is a correct code?because the explanation given is to give a momentaraly delay during an event.

to stop the elevator

What in your code causes the elevator's position to change? If you don't change the variables for where the elevator is drawn, it won't move.
Look at your code to see why and how those variables are being changed. Don't change them and the elevator won't move. Change them and it will move.

hi there,

i try to create two timer but it seems the second timer does not try to stop the elevator. any feedback appreciated.

You'll have to post the code you are using. I can't make any recommendations without seeing it.

hi there,

sorry..this is the code:

if ((yco % height == 0) && (app.control.bp[current] == true)) {

        tm.stop();
        
        Timer tm1 = new Timer(100, this);
        tm1.setDelay(1000);
        tm1.start();
            String line0 = ("Elevator is picking up passenger at floor " +
            app.control.b[current].getActionCommand().substring(1));
            app.state.setText(line0);
            app.control.b[current].setBackground(Color.cyan);
            app.control.bp[current] = false;
        }

Any feedback appreciated. This timer has bothered me for one week..:) thanks.

What can I say about the short piece of code you posted?
For one thing it doesn't compile so I can't test it. The action listener code is missing. The paint method is missing. etc

hi there,

here is the full codes: (sorry about that)

import java.awt.*; //all the container and layout package
import java.awt.event.*; //all the listener and action event
import javax.swing.*; //all the panel and button
import javax.swing.event.*; //unused

//The main class
public class Elevator_Simulation extends JFrame {

    public JLabel state; // display the state of the elevator
    private JLabel id;  //your name and group
    public ButtonPanel control; //the button control panel
    private Elevator elevator; // the elevator area

    //constructor
    public Elevator_Simulation() {
        // Create GUI

        //container for all the panels
        Container container = getContentPane();

        //name and group 
        id = new JLabel("Name : Anthony Salim Kwang Group: SSP1", SwingConstants.CENTER);
        id.setForeground(Color.red);
        JPanel id_panel = new JPanel();
        id_panel.add(id);
        container.add(id, BorderLayout.NORTH);

        //all the 8 buttons panel
        control = new ButtonPanel();
        JPanel control_panel = new JPanel();
        control_panel.add(control);
        container.add(control, BorderLayout.WEST);

        //state of elevator panel
        state = new JLabel(" ", SwingConstants.CENTER);
        state.setForeground(Color.red);
        JPanel state_panel = new JPanel();
        state_panel.add(state);
        container.add(state, BorderLayout.SOUTH);

        //elevator panel
        elevator = new Elevator(this);
        elevator.setBackground(Color.yellow);
        JPanel elevator_panel = new JPanel();
        elevator_panel.add(elevator);
        container.add(elevator, BorderLayout.CENTER);

    }

    // Main method
    public static void main(String[] args) {

        // Create a frame and display it
        Elevator_Simulation frame = new Elevator_Simulation();
        frame.setTitle("lab1: Elevator Simulation");
        frame.setSize(400, 300);

        //center the frame
        frame.setLocationRelativeTo(null);

        //exit upon closing
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
} //the end of Elevator_Simulation class

//The ButtonPanel class receives and handles button pressing events
class ButtonPanel extends JPanel implements ActionListener {

    public JButton b[] = new JButton[8];  // 8 Buttons
    public boolean bp[] = new boolean[8]; // the state of each button,
                                          // pressed or not

    //constructor
    public ButtonPanel() {
        //create GUI

        setLayout(new GridLayout(8, 1));

        //create all the 8 buttons and register button to listener event
        for (int i = 1; i <= b.length; i++) {
            b[8 - i] = new JButton("F" + Integer.toString(8 - (i - 1)));
            b[8 - i].setBackground(Color.cyan);
            b[8 - i].addActionListener(this);
            add(b[8 - i]);
        }
    }

    public void actionPerformed(ActionEvent e) {
        //handle the button pressing events
        int buttonNumber =
                Integer.parseInt(String.valueOf(e.getActionCommand().charAt(1)));

        if (bp[buttonNumber - 1] = true) {
            b[buttonNumber - 1].setBackground(Color.red);
        }
        
    }
} //the end of ButtonPanel class

// The elevator class draws the elevator area and simulates elevator movement
class Elevator extends JPanel implements ActionListener {
    //Declaration of variables

    private Elevator_Simulation app; //the Elevator Simulation frame
    private boolean up; // the elevator is moving up or down
    private int width;  // Elevator width
    private int height; // Elevator height
    private int xco;	// The x coordinate of the elevator's upper left corner
    private int yco; // The y coordinate of the elevator's upper left corner
    private int dy0; // Moving interval
    private int topy; //the y coordinate of the top level
    private int bottomy; // the y coordinate of the bottom level
    private Timer tm; //the timer to drive the elevator movement
    //other variables to be used ...
    //to make sure the variables yco is not static
    private boolean firsttime;
    
    //constructor
    public Elevator(Elevator_Simulation app) {
        //necessary initialization        

        dy0 = 14;
        up = true;
        firsttime = true;
        this.app = app;

        //declare the timer
        tm = new Timer(50, this);
        
        //start the timer
        tm.start();
    }
	

    // Paint elevator area
    public void paintComponent(Graphics g) {

        width = getWidth() / 8;
        height = getHeight() / 8;
        bottomy = height * 7;
        topy = 0;
        xco = getWidth() / 2 - 45;

         if (firsttime) {
            yco = height * 7;
            firsttime = false; // turn off the flag for yco
        }
        //clear the painting canvas
        super.paintComponent(g);
        //start the Timer if not started elsewhere
        if (!tm.isRunning()) {
            tm.start();
        }

        //draw horizontal lines and the elevator
        for (int i = 0; i <= 8; i++) {
            g.setColor(Color.red);
            g.drawLine(0, height * i, getWidth(), height * i);
        }

        g.setColor(Color.GRAY);
        g.fillRect(xco, yco, width, height);
        g.setColor(Color.RED);
        g.drawLine(xco + (width / 2), yco, xco + (width / 2), (yco + height));
    }

    //Handle the timer events
    public void actionPerformed(ActionEvent e) {

        int counter = 0;
        //get current floor
        int current = 7 - (yco / height);

        //loop if the elevator needs to be stopped for a while
        if ((yco % height == 0) && (app.control.bp[current] == true)) {

        tm.stop();
        
        Timer tm1 = new Timer(100, this);
        tm1.setDelay(1000);
        tm1.start();
            String line0 = ("Elevator is picking up passenger at floor " +
            app.control.b[current].getActionCommand().substring(1));
            app.state.setText(line0);
            app.control.b[current].setBackground(Color.cyan);
            app.control.bp[current] = false;
        }
        
        //tm.start();
        //change moving direction when hits the top and bottom
        //update the state of the elevator
        if (yco == topy) {
            up = false;
            String line1 = "Elevator is going down";
            app.state.setText(line1);
        }
        if (yco == bottomy) {
            up = true;
            String line2 = "Elevator is going up";
            app.state.setText(line2);

        }
        //adjust Y coordinate to simulate elevator movement
            if(up) {
            yco --;
            }
            else {
                yco++;
            }

//            if((yco%height == 0) && (app.control.bp[current] == true)) {
//
//            String line0 = ("Elevator is picking up passenger at floor " +
//            app.control.b[current].getActionCommand().substring(1));
//            app.state.setText(line0);
//            app.control.b[current].setBackground(Color.cyan);
//            app.control.bp[current] = false;
//            }
//            if((yco%height == 0) && (app.control.bp[current] == true)) {
//                 System.out.println(app.control.b[current].getActionCommand().substring(1));
//            String line0 = ("Elevator is picking up passenger at floor " +
//            app.control.b[current].getActionCommand().substring(1));
//            app.state.setText(line0);
//            app.control.b[current].setBackground(Color.cyan);
//            app.control.bp[current] = false;
//            }

        

        //repaint the panel
        repaint();
        

    }

    
}
 //the end of Elevator class

Ignore previous content

You need some more logic to recognize when the elevator is loading.
Add another boolean that is true when the elevator is stopped and loading. Test its setting in the actionPerformed method to control whether the elevator moves or not.
Also stop and start the movement timer using this boolean's setting

Also if you add some print outs in the code you will see what is happening:

public void actionPerformed(ActionEvent e) {
        System.out.println("aP e=" + e); // show AE
        ...

Set the time to wait while loading:

tm1 = new Timer(3000, this);   // set time to wait for

hi there,

which boolean setting?

Add a new boolean called loading. Set it true when you start the timer for having the elevator wait to load and stop the other timer.
When the new Timer calls the actionPerformed method after having waited for loading to finish, if loading is true, set it false and stop the new timer and start the old timer for the elevator to continue moving.

Hi Norm. I don't mean to butt in here, but do you think maybe its time to stop and think about States and a state transition diagram? I suspect that ad-hoc booleans and timers could lead to something confusing and very hard to debug completely.
If this were my code I'd be thinking of an enum of States (stooped, going up, going down, loading) and some methods like stop(), wait(int seconds) etc to handle the state transitions.
Just ignore this if you think it's not helpful.
J

Yes, that would be a better design. I always wonder when to transition from procedural style (which I've done for decades) to OOP.
Is the OP ready for it yet?

hi there,

i tried this but does not work.

if ((yco % height == 0) && (app.control.bp[current] == true)) {

            if(loading) {
            tm.stop();
            tm1.start();
            loading = false;
            }

            String line0 = ("Elevator is picking up passenger at floor " +
            app.control.b[current].getActionCommand().substring(1));
            app.state.setText(line0);
            app.control.b[current].setBackground(Color.cyan);
            app.control.bp[current] = false;
        }

there is a boolean and new timer also.

Is the OP ready for it yet?

drogba123: sorry to talk about you in the third person like this, please don't be offended - it's intended to help!

Norm: I'm an OOP fanatic, so I believe in starting with the OO style from day 1 (although others may disagree!). Judging by the code posted so far I think the OP has plenty of capability with the essential programming constructs and the Java API way of doing things; the problem here is one of design. However, it's important to maintain the continuity of dialog that you already have. Having made my suggestion I'm going to lurk in the background, just let me know if I can help.
J

hi james,

no worry.i am beginner here so i think doing something like emu will make this more confusing.lols.

James: My OOP is weak. I just don't think in those terms and usually iterate my way into OOP code starting from procedural. I'll try to get the OPs code to work and then could you show him how to design it from an OOP point of view

drogba123; Sorry to get side tracked

i tried this but does not work.

Can you explain what the problem is in more detail? Posting "it does not work" doesn't tell me what the problem is.

Did you check if you're loading before starting the first timer thread?

One thing about the code you posted, set the boolean false BEFORE doing anything else.
It is possible that a started thread could execute before returning from the call to start().

hi,

sorry.i mean it does not stop moving.

you mean check the action event?

oh ok i will try to set it to false first.thanks.

hi norm,

i realised when i did a system.out.println to check whether the loading works or not, it does not print out anything. does this mean the loading boolean not running?thanks.

can i ask whats the meaning of this,

aP e=java.awt.event.ActionEvent[unknown type,cmd=null,when=1285247426669,modifiers=] on javax.swing.Timer@145d068

This happened when i try to press a button for second time.then loading = false will activated.

aP e=java.awt.event.ActionEvent[unknown type,cmd=null,when=1285247426669,modifiers=] on javax.swing.Timer@145d068

That is the print out showing of the event's toString() method. The end of the printout shows which timer posted the event.

it does not stop moving.

Why does it keep moving? Does the print out of the messages above show you that the timer is still running and posting events? Should it be running while you are loading?

Be a part of the DaniWeb community

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