Criando uma animação usando Timer - J2ME

Uma animação é criada em dispositivos móveis através do método repaint() da classe Canvas. A imagem é apresentada na tela em intervalos regulares de tempo em posições diferentes para mostrar o efeito da animação, neste caso, o movimento.

O código abaixo faz uma animação de alguns segundos e depois deixa a imagem parada na tela.

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class Animation extends MIDlet implements CommandListener
{
  public Display display;
  public Animation() {}

  public void startApp() 
  {
    AnimationCanvas animation=new AnimationCanvas(this);
    animation.startAnimation();
    Display.getDisplay(this).setCurrent(animation);
  }

  public void pauseApp() {}

  public void destroyAppboolean unconditional ) {}

  public void exit()
  {
    destroyApp(true);
    notifyDestroyed();
  }

  public void commandAction(Command c, Displayable s){}
}


import javax.microedition.lcdui.*;

public class AnimationCanvas extends Canvas implements Runnable, 
 CommandListener
{
  private Animation midlet;
  int height;
  int width;
  int pad = 3;
  boolean finishedAnimation = false;
  int deltaY = 0;

  private Command  startCommand;
  private Command exitCommand;
  private Command aboutCommand;


  public AnimationCanvas(Animation midlet)
  {
    this.midlet=midlet;
    height = getHeight();
    width = getWidth();

    startCommand = new Command("Iniciar",Command.BACK,1);
    exitCommand = new Command("Sair",Command.SCREEN,0);
    aboutCommand = new Command("Sobre",Command.SCREEN,3);
  }

  public void paint(Graphics g)
  {
    g.setColor(0xFFFFFF);
    g.fillRect(0,0,width,height);
    g.setColor(0,255,0);
    g.drawRoundRect(0,0,width-1,height-1,10,10);
  
    if (finishedAnimation)
    {
      Form form = new Form("Animação");
      form.append("Animação Completa\n");
      form.addCommand(exitCommand);
      form.setCommandListener(this);
      Display.getDisplay(midlet).setCurrent(form);

    }
    else
    {
      Image img=null;
      try 
      {
        img = Image.createImage("/logo.png");
      }
      catch(Exception e){}
      int ih = img.getHeight();
      int iw = img.getWidth();
      int imgX = (width-2)/2;
      int imgY = (height-2)/2;
      g.drawImage(img,imgX,imgY+deltaY,Graphics.VCENTER|Graphics.HCENTER);
        
      removeCommand(exitCommand);
    }
  }

  public void startAnimation()
  {
    try
    {
      finishedAnimation = false;
      Thread t = new Thread(this);
      t.start();
    }
    catch(Exception e){}
  }

  public void run()
  {
    try
    {
      int count = 0;
      int sleep_time = 200;
      while (true)
      {
        Thread.sleep(sleep_time);
        count++;
        if (count > 20
        {
          finishedAnimation = true;
          Thread.sleep(sleep_time);
          repaint();
          break;
        }
        if (count <=
          deltaY += 5;
        else if (count >&& count <=15)
          deltaY -= 5;
        else 
          deltaY +=5;
        repaint();
      }
    }
    catch(Exception e){}
  }

  public void commandAction(Command c,Displayable d)
  {
    if (c == exitCommand){
      midlet.exit();}
  }
}