V
Vanessa Berni
Hi,
I'm trying to write a program that simulate traffic. I've got a class
"Scheduler" that simulate the movement of streets, cars ...
I've created an applet which calls a thread; in his method "run" I simply
call the method run of the Scheduler (which modifies cars position ...)
(action A) and then I repaint all the streets and cars (action B) then the
thread sleeps 30 millis (action C)
The thread start and so actions A,B,C, A,B,C, A,B,C, A,B,C ... are executed.
At a certain point while executing event B (repaint) the action A starts
again....
Why???
Can you please help me??
Thank you Vanessa
public class JSim extends JPanel implements Runnable{
public Thread runner = null;
public static Schedulatore scheduler = new Schedulatore();
public SimulatoreCanvas() {
super();
}
public void run() {
startTime = System.currentTimeMillis();
while (Thread.currentThread() == runner) {
try {
//action A
this.scheduler.run();
//action B
repaint();
//action C
Thread.sleep(30);
}
catch (InterruptedException e) { ;
}
}
}
public void start() {
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}
public void paintComponent(Graphics g){
//... code that paints street objects
}
}
I'm trying to write a program that simulate traffic. I've got a class
"Scheduler" that simulate the movement of streets, cars ...
I've created an applet which calls a thread; in his method "run" I simply
call the method run of the Scheduler (which modifies cars position ...)
(action A) and then I repaint all the streets and cars (action B) then the
thread sleeps 30 millis (action C)
The thread start and so actions A,B,C, A,B,C, A,B,C, A,B,C ... are executed.
At a certain point while executing event B (repaint) the action A starts
again....
Why???
Can you please help me??
Thank you Vanessa
public class JSim extends JPanel implements Runnable{
public Thread runner = null;
public static Schedulatore scheduler = new Schedulatore();
public SimulatoreCanvas() {
super();
}
public void run() {
startTime = System.currentTimeMillis();
while (Thread.currentThread() == runner) {
try {
//action A
this.scheduler.run();
//action B
repaint();
//action C
Thread.sleep(30);
}
catch (InterruptedException e) { ;
}
}
}
public void start() {
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}
public void paintComponent(Graphics g){
//... code that paints street objects
}
}