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 Frame Animation

                         

This application shows to set the Frame Animation and implement it in the canvas class. In this example we are creating a frame using Gauge class. When the command action call the "Run" command ,which display the canvas form. In the canvas form we are drawing three blocks, when form's action will run then the blocks separated from each other take place and move on the mobile window as shown in figure:

 

 

 

 

 

 

FrameAnimation.java

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

public class FrameAnimation extends MIDlet implements CommandListener, 
         ItemStateListener
{
    private Display display;
    protected boolean started;
    private Command exit, setup, run;
    private Form form;
    private AnimationCanvas canvas;
    private Gauge block, rate;
    private static final int FRAME_RATE = 1;
    private static final int BLOCK_COUNT = 1;
    
    protected void startApp() {
        if (!started) {
            display = Display.getDisplay(this);
            form = new Form("Frame Animation");
            rate = new Gauge("Set Frame", true, 10, FRAME_RATE);
            block = new Gauge("Set Blocks", true, 4, BLOCK_COUNT);
            form.append(rate);
            form.append(block);
            form.setItemStateListener(this);            
            canvas = createAnimationCanvas();           
            
            exit = new Command("Exit", Command.EXIT, 0);
            setup = new Command("Setup", Command.SCREEN, 0);
            run = new Command("Run", Command.SCREEN, 0);
            
            canvas.addCommand(exit);
            canvas.addCommand(setup);
            form.addCommand(exit);
            form.addCommand(run);
            
            form.setCommandListener(this);
            canvas.setCommandListener(this);
            
            display.setCurrent(form);
            started = true;
        }
    }

    protected void pauseApp(){}

    protected void destroyApp(boolean unconditional){}    

    public void commandAction(Command c, Displayable d){
    String label = c.getLabel();
        if (label.equals("Exit")){
            notifyDestroyed();
        else if (label.equals("Run")){
            display.setCurrent(canvas);
        else if (label.equals("Setup")){
            display.setCurrent(form);
        }
    }
    
    public void itemStateChanged(Item item){
        if (item == block){
            int count = block.getValue();
            if(count < 1) {
                count = 1;
            }
            canvas.setBlockCount(count);
        else if (item == rate){
            int count = rate.getValue();
            if (count < 1){
                count = 1;
            }
            canvas.setFrameRate(count);
        }            
    
    
    protected AnimationCanvas createAnimationCanvas(){
        return new AnimationCanvas();
    }
        
    class AnimationCanvas extends Canvas{
    protected static final int SIZE = 4;
        protected final int[] xSpeeds = 2, -20, -};
        protected final int[] ySpeeds = 2, -22, -};
        protected int background = display.isColor() 0x6699ff 0xc0c0c0;
        protected int foreground = display.isColor() 0xff3300 0;
        protected int width = getWidth();
        protected int height = getHeight();
        protected int frameRate;
        protected Block[] blocks;
        protected Timer timer;
        protected TimerTask updateTask;

        public int getMaxBlocks(){
            return blocks.length;
        }
        AnimationCanvas(){
            setBlockCount(BLOCK_COUNT);
            setFrameRate(FRAME_RATE);
        }
        public void setBlockCount(int count){
            blocks = new Block[count];
            createBlocks();
        }        
        public int getBlockCount(){
            return blocks.length;
        }
        
        public void setFrameRate(int frameRate){
            this.frameRate = frameRate;
            if (isShown()){
                startFrameTimer();
            }
        }        
        public int getFrameRate(){
            return frameRate;
        }  
          
        protected void paint(Graphics g){
            g.setColor(background);
            g.fillRect(00, width, height);
            g.setColor(foreground);
            synchronized (this){
                for (int i = 0, count = blocks.length; i < count; i++){
                    g.fillRect(blocks[i].x, blocks[i].y, SIZE, SIZE);
                }
            }
        }
        
        protected void showNotify(){
            startFrameTimer();
        }
        protected void hideNotify(){
            stopFrameTimer();
        }
        
        private void createBlocks(){
            int startX = (width - SIZE)/2;
            int startY = (height - SIZE)/2;
            for (int i = 0, count = blocks.length; i < count; i++){
                blocks[inew Block(startX, startY, xSpeeds[i], ySpeeds[i]);
            }
        }
        protected void startFrameTimer(){
            timer = new Timer();
            
            updateTask = new TimerTask(){
                public void run() {
                    moveAllBlocks();
                }
            };
            long interval = 1000/frameRate;
            timer.schedule(updateTask, interval, interval);
        }
        
        protected void stopFrameTimer(){
            timer.cancel();            
        }
        
        public synchronized void moveAllBlocks(){
            for (int i = 0, count = blocks.length; i < count; i++){
                blocks[i].move();                
                repaint();                
            }
        }
        
        class Block{
            int x;     
            int y;      
            int xSpeed; 
            int ySpeed;
            
            Block(int x, int y, int xSpeed, int ySpeed){
                this.x = x;
                this.y = y;
                this.xSpeed = xSpeed;
                this.ySpeed = ySpeed;
            }
            
            void move(){
                x += xSpeed;
                if(x <= || x + SIZE >= width){
                    xSpeed = -xSpeed;
                }
                
                y += ySpeed;
                if(y <= || y + SIZE >= height){
                    ySpeed = -ySpeed;
                }                
            }            
        }
    }
}

 

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.