Home J2me J2ME Timer Animation
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

J2ME Timer Animation

Advertisement
This application illustrates how to use the Timer class and implement it in the canvas class.

J2ME Timer Animation

     

This application illustrates how to use the Timer class and implement it in the canvas class. In this Tutorial we have given you a good example, which helps you to understand using of timer class for drawing the canvas. For the different task of canvas we are using the AnimatTimerTask class and schedule to this class as below:

timer.schedule(timertask, 0, 100);

To the use of this application, you can draw different type and style of canvas. You can download this source code and implement it in your project from the given link below. 

The Application is as follows:

 

 

 

TimerAnimation.java

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

public class TimerAnimation extends MIDlet{
  private Display  display;  
  private AnimationCanvas canvas;
  private Timer timer;  
  private AnimateTimerTask timertask;  

  public TimerAnimation(){
  display = Display.getDisplay(this);
  timer = new Timer();
  canvas  = new AnimationCanvas(this);
  timertask = new AnimateTimerTask(canvas);
  timer.schedule(timertask, 0, 100);  
  }

  protected void startApp(){
  display.setCurrent(canvas);
  }

  protected void pauseApp(){}

  protected void destroyApp(boolean unconditional){
  notifyDestroyed();
  }

  public void exitMIDlet(){
  destroyApp(true);
  }
}

class AnimateTimerTask extends TimerTask{
  private AnimationCanvas canvas;

  public AnimateTimerTask(AnimationCanvas canvas){
  this.canvas = canvas;
  }

  public final void run(){
  if ((canvas.x_loc + canvas.radius +  canvas.x_dir > canvas.getWidth()) ||
  (canvas.x_loc - canvas.radius +  canvas.x_dir < 0)){  
  canvas.x_dir = -canvas.x_dir;
  canvas.changeColor();
  canvas.change_way++;
  }
  if ((canvas.y_loc + canvas.radius +  canvas.y_dir > canvas.getHeight()) ||
  (canvas.y_loc - canvas.radius + canvas.y_dir < 0)){
  canvas.y_dir = -canvas.y_dir;
  canvas.changeColor();
  canvas.change_way++;  
  }
  canvas.x_loc += canvas.x_dir;
  canvas.y_loc += canvas.y_dir;
  canvas.repaint();  
  }
}

class AnimationCanvas extends Canvas implements CommandListener{
  private TimerAnimation a_midlet;  
  private Command exit; 
  private int fire, right, left;  
  private boolean clean = false; 
  private Random random;  
  int x_loc, y_loc, radius, red, green, blue, x_dir, y_dir, 
  start_x, start_y, change_way = 0;
  private static final int MAX_CHANGES = 50;

  public AnimationCanvas(TimerAnimation a_midlet){
  this.a_midlet = a_midlet;
  random = new java.util.Random();
  init();
  radius = 7;
  exit = new Command("Exit", Command.EXIT, 1);
  fire =  getKeyCode(FIRE);
  right = getKeyCode(RIGHT);
  left = getKeyCode(LEFT);
  addCommand(exit);
  setCommandListener(this);
  } 

  protected void paint(Graphics g){
  if (change_way > MAX_CHANGES){
  init();
  }
  if (clean){
  g.setColor(255, 255, 255);
  g.fillRect(0, 0, getWidth(), getHeight());
  clean = !clean;
  }
  g.setColor(red, green, blue);  
  g.fillArc( x_loc, y_loc, radius, radius, 0, 360);
  }

  private void init(){
  x_loc = getWidth() / 2;
  y_loc = getHeight() / 2; 
  x_dir = (random.nextInt() % 10);
  if (x_dir == 0)  x_dir = 1;
  y_dir = (random.nextInt() % 10);
  if (y_dir == 0)  y_dir = 1;
  change_way = 0;  
  clean = true;  
  changeColor();  
  }

  protected void changeColor(){
  red = (random.nextInt() >>> 1) % 256;
  green = (random.nextInt() >>> 1) % 256;
  blue = (random.nextInt() >>> 1) % 256;
  }

  public void commandAction(Command c, Displayable d){
  String label = c.getLabel();
  if (label.equals("Exit"))
  a_midlet.exitMIDlet();
  }

  protected void keyPressed(int keyCode){
  if (keyCode == fire)
  init();
  else if (keyCode == left)
  radius = Math.max(1, --radius);
  else if (keyCode == right)
  radius = Math.min(getWidth() / 4, ++radius);
  }
}

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 2, 2008

Related Tutorials

Ask Questions?    Discuss: J2ME Timer Animation  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Shamol kumar(joy)
June 5, 2012
Feed back

How can I compile this source code?How can I make class file in my project?How can I run your example code in my j2me supported device?Help me and reply me?Thanks.
DMCA.com