Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

Hot Searches
struts-config.xml  web.xml  Java date format  read/write text file using javascript  ArrayList  Insert Data into Database Using Hibernat  create text file using java  insert data to text file using java  Visual Basic Date and Time Display the t  Create table and insert data by sql quer 

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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, 0100);    
  }

  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(255255255);
      g.fillRect(00, getWidth(), getHeight());
      clean = !clean;
    }
    g.setColor(red, green, blue);                    
    g.fillArcx_loc, y_loc, radius, radius, 0360);
  }

  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() >>> 1256;
    green = (random.nextInt() >>> 1256;
    blue = (random.nextInt() >>> 1256;
  }

  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

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Tell A Friend
Your Friend Name

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.