So instead of...
while(rad < 200){
repaint();
rad++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
You simply need to turn the logic around a little...
Timer timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
rad++;
if (rad < 200) {
repaint();
} else {
((Timer)evt.getSource()).stop();
}
}
});
timer.start();
Basically, the Timer
will act as the Thread.sleep()
, but in a nice way that doesn't break the UI, but will allow you to inject a delay between execution. Each time it executes, you need to increment your value, test for the "stop" condition and update otherwise...
Take a look at How to Use Swing Timers and the other 3, 800 questions on the subject on SO...