java left logo
java middle logo
java right logo
 

Home arrow Java ME Tips arrow MIDP arrow Simple Animation using Timer
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3942
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Simple Animation using Timer E-mail
User Rating: / 31
PoorBest 

An animation is created on mobile by calling the repaint() method of Canvas class.T he image is displayed on screen at regular intervals with different positions to bring the effect of animation.

Illustration below displays animation for few seconds, then displays a static text.

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("Start",Command.BACK,1);
    exitCommand = new Command("Exit",Command.SCREEN,0);
    aboutCommand = new Command("About",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("Animation");
      form.append("Animation Complete\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();}
  }
}

 Related Tips


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.