Animações utilizando Timer
A classe Timer agenda tarefas para execuções futures. As tarefas podem ser agendadas para executar uma vez, ou repetidas vezes em regulares intervalos de tempo.
import java.util.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class TimerMidlet extends MIDlet implements CommandListener
{
private Display display;
private Form form;
private Command exit;
private Command stop;
private Timer timer;
private RunTimerTask tt;
private int count = 0 ;
public timerMidlet()
{
form = new Form( "Timer" );
exit = new Command( "Sair" , Command.EXIT, 1 );
stop= new Command( "Parar" , Command.STOP, 2 );
}
public void startApp ()
{
display = Display.getDisplay( this );
form.addCommand(exit);
form.addCommand(stop);
form.setCommandListener( this );
// Repetindo a cada 3 segundos
timer = new Timer();
tt = new RunTimerTask();
timer.schedule(tt, 0 , 3000 );
display.setCurrent(form);
}
public void destroyApp ( boolean unconditional){}
public void pauseApp () { }
public void commandAction(Command c, Displayable d)
{
if (c == stop)
{
timer.cancel();
}
else if (c == exit)
{
destroyApp( false );
notifyDestroyed();
}
}
/*--------------------------------------------------
* Classe RunTimerTask – Executando a tarefa
*-------------------------------------------------*/
private class RunTimerTask extends TimerTask
{
public final void run()
{
form.append( "contador: " + ++count + "\n" );
}
}
}
|