help with applet needed

hello,i have to do this applet simulating an elevator for a course at uni

the way i was thinking i could simulate the movement of the elevator is by changing backgrounds of uneditable text fields in order.the only problem i have is i need to slow down the for loop that does it because it does it so fast you can't see the actual motion

is there anyway of doing this?

please email me direct at j.aliazis@gmail.com

thanks

[452 byte] By [sk3pt1ca] at [2007-10-2 4:41:27]
# 1

In your loop, add the code below

try { Thread.sleep(1000); }

catch (Exception e) {}

the time is given in milliseconds

antoine.lemoinea at 2007-7-16 0:14:30 > top of Java-index,Desktop,Core GUI APIs...
# 2

this is the actual code

//method to simulate the movement

public void moveElevator (int location , int destination) {

if (destination-location>0) {

for ( int i=location; i<destination; i++) {

elevator_is_at[i+1].setBackground(Color.BLUE);

elevator_is_at.setBackground(Color.GRAY);

try { Thread.sleep(100); }

catch (Exception e) {}

}

}

else if (destination-location><0) {

for(int i=location; i<destination; i--) {

elevator_is_at[i - 1].setBackground(Color.BLUE);

elevator_is_at.setBackground(Color.GRAY);

try { Thread.sleep(1000); }

catch (Exception e) {}

}

}

}//end of moveElevator

basically,i'm trying to set the next background as blue and the current as gray,to simulate movement

with the Thread.sleep(); that you suggested it waits and completes the whole loop fast again.did i do it wrong?>

sk3pt1ca at 2007-7-16 0:14:30 > top of Java-index,Desktop,Core GUI APIs...
# 3
Have you noticed that your two timer don't have the same value (100 and 1000 ms).I don't understand what is your actual problem
antoine.lemoinea at 2007-7-16 0:14:30 > top of Java-index,Desktop,Core GUI APIs...
# 4

the timers were my mistake,i was just experimenting

the problem is that i want the for loop to execute slowly, so that the backgrounds will change slowly to make it look like the elevator is moving.what happened before was that when i clicked a button the background next to it turned blue and all the others below it turned gray instantly and there was no way of telling that there was some actual movement.what happens with your suggestion is that the applet waits for a second and then does the same thing.basically i want the for loops iterations to occur 500miliseconds apart from each other.

sk3pt1ca at 2007-7-16 0:14:30 > top of Java-index,Desktop,Core GUI APIs...
# 5

Try this

public void moveElevator (int location , int destination) {

if (destination-location>0) {

for ( int i=location; i<destination; i++) {

elevator_is_at[i+1].setBackground(Color.BLUE);

elevator_is_at.setBackground(Color.GRAY);

[b]elevator_is_at[i+1].revalidate();

elevator_is_at.revalidate();[/b]

try { Thread.sleep(500); }

catch (Exception e) {}

}

}

else if (destination-location><0) {

for(int i=location; i<destination; i--) {

elevator_is_at[i - 1].setBackground(Color.BLUE);

elevator_is_at.setBackground(Color.GRAY);

[b]elevator_is_at[i-1].revalidate();

elevator_is_at.revalidate();[/b]

try { Thread.sleep(500); }

catch (Exception e) {}

}

}

}//end of moveElevator

>

antoine.lemoinea at 2007-7-16 0:14:30 > top of Java-index,Desktop,Core GUI APIs...
# 6
Without and ;-)
antoine.lemoinea at 2007-7-16 0:14:30 > top of Java-index,Desktop,Core GUI APIs...
# 7
Argh !!! \[b\] and \[/b\]
antoine.lemoinea at 2007-7-16 0:14:30 > top of Java-index,Desktop,Core GUI APIs...
# 8
would you mind explaining what the [\b] is and what the revalidate does?i get errors from that though,maybe there are typos?
sk3pt1ca at 2007-7-16 0:14:30 > top of Java-index,Desktop,Core GUI APIs...
# 9
I would to set both lines in bold, you mustn't add[b] and [/b]in your code,revalidate method normally makes an actualization of the new properties of the object
antoine.lemoinea at 2007-7-16 0:14:30 > top of Java-index,Desktop,Core GUI APIs...
# 10
still does the same thingit waits half a second and then runs the for loop
sk3pt1ca at 2007-7-16 0:14:30 > top of Java-index,Desktop,Core GUI APIs...
# 11

another question on this topic

can i create individual listeners, e.g.

jbtOK.addActionListener(new ActionListener()){

public void ActionPerformed(ActionEvent e)

System.exit(0);

}

for each one of my buttons rather than dealing with all of them in an ActionPerformed method ?

sk3pt1ca at 2007-7-16 0:14:30 > top of Java-index,Desktop,Core GUI APIs...
# 12

Yes of course, but the correct syntax is :

jbtOK.addActionListener(

new ActionListener() {

public void ActionPerformed(ActionEvent e) {

System.exit(0);

}

}

);

antoine.lemoinea at 2007-7-16 0:14:30 > top of Java-index,Desktop,Core GUI APIs...
# 13

ok i've fixed that

now i'm creating a new thread to run the move method

because i was told that's the only way to have the movement displayed

my thread is:

public class MoveThread extends ElevatorApplet implements Runnable{

//declaring variables

private int location;

private int destination;

public MoveThread(int l,int d) {

location=l;

destination=d;

}//end of MoveThread constructor

//overriden run method

public void run(){

while(destination>location) {

super.elevator_is_at[location+1].setBackground(Color.BLUE);

super.elevator_is_at[location].setBackground(null);

location++;

repaint();

try{Thread.sleep(500);}

catch(InterruptedException ex){}

}//end while

while(destination<location){

super.elevator_is_at[location].setBackground(null);

super.elevator_is_at[location-1].setBackground(Color.BLUE);

location--;

repaint();

try{Thread.sleep(500);}

catch(InterruptedException ex){}

}//end while

}//end of run method

}//end of class

and in my parent class i do:

Thread pick_up_passenger = new Thread(new MoveThread(passenger_array[0].getLocationOfPassenger(),

passenger_array[0].getDestinationOfPassenger()));

pick_up_passenger.start();

but when i run the program and click a button i get a null pointer exception from the thread class from where i try to change the background of elevator_is_at for the first time

any ideas?>

sk3pt1ca at 2007-7-16 0:14:30 > top of Java-index,Desktop,Core GUI APIs...
# 14
In your thread constructor, you have to call the constructor of the super class like this :super();location=l;destination=d;
antoine.lemoinea at 2007-7-16 0:14:30 > top of Java-index,Desktop,Core GUI APIs...