hi i make the modification already.

the codes:

if ((yco % height == 0) && (app.control.bp[current] == true)) {
            
            tm.stop();
            System.out.println("a" + e);
            if(loading) {
            tm1.start();
            System.out.println("b" + e);
            loading = true;
            }

first i stop the first timer (the timer that responsible moving up and down.

then i start by saying if loading still false, start the new timer and set it to true.

but so far from println only can see action 'a' not 'b'

The posted code will only execute the print 'b' statement if loading is true.
Setting loading true inside the if condition is redundant, since loading must be true for execution to get there. That doesn't make any sense?????

Can you describe what the code you posted is supposed to do?

You need to stop writing code and look at the logic and design for the problem.
Changing this and changing that and changing the other is not the best or quickest way to write a program. Work out the logic, then write the code.

hi norm,

but the loading i set up as loading = false at the beginning. so the logic is :

when loading still false, it will try to start the new timer and then set the loading to true.

thanks.

if(loading) {

This tests if loading is true

hi norm,

okay.so i managed to get the logic right. so here is the code:

if ((yco % height == 0) && (app.control.bp[current] == true)) {
            loading = true;
            tm.stop();
            
            System.out.println("a" + e);
            if(loading) {
            tm1.start();
            System.out.println("b" + e);
            }
            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;
         }

the logic here is:

after i press the button on elevator, it will set the boolean to be true.
then it will stop the old timer. after that it will check whether the loading true or not.if true then start the new timer to stop the movement for a while.

so how should i stop the movement using new timer. can i use delay?thanks. need your advice / feedback.

how should i stop the movement using new timer

The new timer was to time how long to wait. The movement should be stopped when you create and start the loading/wait timer.
When the loading/wait timer posts its event, stop the loading timer and start the movement timer.

In looking more closely at your program, I realize now that you only need one timer.
By using the setDelay() method you can change the times between moves of the elevator.
Sorry for the confusion. The logic remains the same except instead of stopping and starting timers, just change the delay for the one timer.

hi norm,

thanks. do you mean the one timer using tm.setDelay() u mean?

Yes

hi norm,

this is the code:

public void actionPerformed(ActionEvent e) {

       //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)) {
            loading = true;
            if(loading) {
            tm.setDelay(1000);
            loading = false;
            }
            tm.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;
         }

the logic is same..i tried whenever i press any button, it will set boolean loading = true and then stop the timer and delay it for 6 seconds. after that start the timer again.

the problem here it does stop for a while but when it starts to move, the elevator move very very slow. is it something to do with the delay?thanks

problem here it does stop for a while but when it starts to move, the elevator move very very slow

What is the delay set to when it starts moving again?

A comment on your code:
Use final variables with self documentating names for the delay times vs hardcoding a literal (1000) in the method call. For example:

final int MoveDelay = 50;   // time between moves 
    final int LoadTime = 3000;  // time to wait while loading

    ....
    tm.setDelay(LoadTime);   // wait some time for loading

This code makes no sense. Step throught the logic.

loading = true;
            if(loading) {
              tm.setDelay(1000);
              loading = false;
            }

You are still just writing code without doing any design work or thinking about how the code will execute.

hi norm,

sorry but my thinking here is that, whenever the loading = true,

it will delay the timer for example tm.setDelay(loadTime) around 3 seconds (3000 miliseconds)

then no more loading time, continue the timer as usual.

any feedback appreaciated. thanks.

Can you post your code with comments showing what you are trying to do.
Did you understand what was wrong with the code I copied and posted from your code section? Play computer with it. Do it step by step and see if it makes any sense.

hi norm,

The codes:

public Elevator(Elevator_Simulation app) {

//declare a timer with its movetime delay
tm = new Timer(moveTime, this);
}
public void actionPerformed(ActionEvent e) {

//find the elevator current floor
int current = 7 - (yco / height);
       
        //loop if the elevator needs to be stopped for a while
        //if pressed button and the current floor is satisfied do following
        if ((yco % height == 0) && (app.control.bp[current] == true)) {
            
            //loading of elevator set to be true meaning it will try to hold the  elevator from moving
            loading = true;

            //if loading true do following
            if(loading) {
 
            //delay the time between elevator movement for 3 seconds.
            tm.setDelay(loadTime);

            //after that set it false so it no longer hold the elevator
            loading = false;
            }

            //a string to display pick passenger at which floor
            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;
         }
         //start the elevator again.
         tm.start();

thanks.

loading = true;
 
            //if loading true do following
            if(loading) {

Look at this!!! Think about how this will execute. When will the if ever be false?

hi norm,

ops..tats cause the loading to be true all the time and never set to false. i did clear that away and put it outside the if statement of(button == true).

after i compile, the elevator moving slower than its first time run. thanks.

apologise for the wrong logic.thanks.

the elevator moving slower

Add some debug print outs Using the Timer class's getDelay method to see why.

do you mean by printing ("e" + e); ?

print outs of the Timer class's getDelay method

hi norm,

i did not have getDelay() method..only setDelay() method. thanks.

hi norm,

ignore the previous content.it did show the delay that i set at setDelay() method.

Do you now see why the elevator is moving slowly?

since it just show the delay that i set at setDelay() method, there is no further explanation elevator moving slowly except showing the delay time.

Have you fixed the problem of the elevator moving slowly?

Its delay should be one of the values you set. Does it show the delay that you expect to see when the elevator is moving at the speed that you want it to move?

hi norm,

not yet because i still cant get the logic why it moves slowly..i suspect its because of the delay..but i suppose to delay the elevator movement.

We cross posts. Please read my last post.

hi norm,

the delay is there but the elevator does not move at the same speed after the setDelay().

i keep thinking but could not find a way to bypass that as in making it delay for a while and come back to same speed as before.

i even tried using setDelay() method again to set it to original speed but to no avail. thanks.

When it is slow, what is the value of the delay for the timer?
You need to debug you code:
Print out the timer delay every time you enter the actionPerformed method and everytime you enter the paintComponent method.

Also print out the y value every time you change the y value for the elevator's position.

hi norm,

okay.i got it.so i think because everytime i setDelay(3000) when button is pressed, it will repaint back the delay(3000) again when the action perform again. thus the new delay is no longer as 100 but as 3000.

So how do i change back to original content?thanks.

how do i change back to original content

tm.setDelay(MoveDelay); // restore delay for normal movement

You need to look at the logic of the program and decide when you can set the delay back to the shorter time.

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.