Event Dispatching Thread Problem
Hi to all,
I have built a simple AWT Applet for educational purposes.
I draw one rectangle. The user waits 1000 ms and then I draw a second one
and so on. Then rectangles are drawn totally.
I added 2 buttons go and stop so that the user could re-execute or stop the
animation.
However these buttons are unresponsive while the animation is being executed
evenif I created a separated thread so as not to block the EDT.
I provide the code below.
Any help or suggestion would be highly appreciated.
Thanks in advance
Thanasis
++++++++++++++++++++++++++++++++++++++++++++++++
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class StopThread extends Applet implements Runnable {
Button btnGo, btnStop;
Thread calcThread = null;
public void init() {
btnGo = new Button("go");
btnStop = new Button("stop");
add(btnGo);
add(btnStop);
btnGo.addActionListener(new goButtonClass());
btnStop.addActionListener(new stopButtonClass());
}//end init
public void run() {
repaint();
}
public void paint(Graphics g) {
for (int i=0;i<=9;i++) {
int r1= (int)(Math.random()*255);
int r2= (int)(Math.random()*255);
int r3= (int)(Math.random()*255);
g.setColor(new Color(r1,r2,r3));
g.drawRect(5+3*i,15+15*i,100,50);
try {
calcThread.sleep(1000);
}
catch (InterruptedException e){
}
}//end for
}//end paint
//========inner class Listener goButtonClass=============================
class goButtonClass implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("go")) {
System.out.println("go");
if (calcThread == null) {
calcThread = new Thread(StopThread.this);
calcThread.start();
}
}
}
}
//========inner class Listener stopButtonClass=============================
class stopButtonClass implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("stop!!");
if(calcThread != null) {
calcThread.stop();
calcThread = null;
}
}
}
// end stopButtonClass
}//end APPLET