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]

In your loop, add the code below
try { Thread.sleep(1000); }
catch (Exception e) {}
the time is given in milliseconds
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?>
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
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.
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
>
Argh !!! \[b\] and \[/b\]
would you mind explaining what the [\b] is and what the revalidate does?i get errors from that though,maybe there are typos?
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
still does the same thingit waits half a second and then runs the for loop
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 ?
Yes of course, but the correct syntax is :
jbtOK.addActionListener(
new ActionListener() {
public void ActionPerformed(ActionEvent e) {
System.exit(0);
}
}
);
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?>
In your thread constructor, you have to call the constructor of the super class like this :super();location=l;destination=d;