"Not to multithread in Java" doesn't sound right. The advice was maybe geared towards Swing and to run your app/do your painting on the Event Dispatch Thread. In which case you would want to do the animation with a java.swing.Timer
. The basic construct is
Timer( int deleyInMillis, ActionListener listener )
where the delayInMillis
is the milliseconds delayed between ActionEvents
fired by the timer. The ActionListener
will be the listener listing for the ActionEvents
. So every delayInMillis
the actionPerformed
method will be called.
So you could do something like
Timer timer = new Timer(10000, new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if (someStoppingCondition) {
((Timer)e.getSource()).stop();
} else {
// do something every ten seconds.
}
}
});
timer.start();
You can see more at How to Use Swing Timers. You can also see a bunch of examples here and here and here and here and here and here.