aspose file tools*
The moose likes Java Micro Edition and the fly likes Help Needed on animation in j2me Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Help Needed on animation in j2me" Watch "Help Needed on animation in j2me" New topic
Author

Help Needed on animation in j2me

Rishi Tyagi
Ranch Hand

Joined: Feb 14, 2002
Posts: 100
Hi everybody,
Is there any good link available on the net which can help in developing animation in a canvas in j2me.
I want to develop some games in j2me where i have to show some animations , I will be highly obliged If someone can give some sample code for the same.
Hope i will get help here..
Rishi
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Here are some links that you might find useful:
MIDP User Interface Libraries
MIDP GUI Programming
Using the MIDP Low-Level User Interface API
Developing Custom Components for the Mobile Information Device Profile
Drawing Flicker-Free Graphics with the MIDP


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Rishi Tyagi
Ranch Hand

Joined: Feb 14, 2002
Posts: 100
Thanks valentin for the quick response,
I think these links are realy usefull for me
Rishi
Rishi Tyagi
Ranch Hand

Joined: Feb 14, 2002
Posts: 100
Hi Valentin ,
Your links are really helpfull for me, thanks again.
Sorry But still have some problems , I am describing it here below:-
I have created a simple animation program which contains 3 classes
1- TestDogy
This is a simple midlet class which is being invoked at the starting of the application and makes the object of "MainCanvas" class.
2- MainCanvas
This is the subclass of the java.microedition.lcdui.Canvas class .
in the constructor it creates a boundry and shows an image which is the first frame for the animation.
when user clicks the start command then start method calls
Start method makes an object of Anim Class and shedules it for keep running repeatedly after a specified interval.
3- Anim Class
This is the subclass of TimerTask class chnages the image name to be shown each timewhenever it is invoked.

Now Problem is:
whenever user starts the animation start method of the MainCanvas class is invoked and it starts the Timer. But image is not being refreshed. Means after clicking on start command i am getting blank screen while Timer is working fine as the run method of Anim class is being invoked regularly after the specified time interval defined in the interval variable.
I want to ask the reason why the image at canvas is not being refreshed for each iteration.
I am placing the code for all 3 classes here
Code started:---------------------------
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class TestDogy extends MIDlet
{
Display display;
public void startApp()
{
display=Display.getDisplay(this);
MainCanvas mcan=new MainCanvas(display,this);
}
public void pauseApp(){}
public void destroyApp(boolean b){}
}
//*********MainCanvas Class Started**************
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class MainCanvas extends Canvas implements CommandListener
{
MIDlet midlet;
Display display;
int wd=0,ht=0;
Image img,img1,img2,img3,img4;
long interval=500;
private Command cmdExit,cmdStart,cmdStop;
Anim anim;
public MainCanvas(){}
public MainCanvas(Display d,MIDlet m)
{
display=d;
midlet=m;
wd=getWidth();
ht=getHeight();
cmdExit=new Command("Exit",Command.SCREEN,0);
cmdStart=new Command("Start",Command.SCREEN,0);
cmdStop=new Command("Stop",Command.SCREEN,0);
addCommand(cmdExit);
addCommand(cmdStart);
//addCommand(cmdStop);
setCommandListener(this);
System.out.println("width :"+wd);
System.out.println("height :"+ht);
img=Image.createImage(wd,ht);
Graphics g=img.getGraphics();
g.drawRect(0,5,wd,ht-10);
try{
Image img_p=Image.createImage("/puppy_frame1.png");
g.drawImage(img_p,5,55,Graphics.TOP|Graphics.LEFT);
}catch(Exception e)
{}
d.setCurrent(this);
}
public void start()
{
try{
removeCommand(cmdStart);
}catch(Exception e)
{
}
try{
addCommand(cmdStop);
}catch(Exception e)
{
}
anim=new Anim(this);
java.util.Timer t=new java.util.Timer();
t.schedule(anim,new java.util.Date(),interval);
//t.schedule(anim,interval);
}
public void stop()
{
anim.cancel();
try{
removeCommand(cmdStop);
}catch(Exception e)
{
}
try{
addCommand(cmdStart);
}catch(Exception e)
{
}
}
public void drawnew(String img_nm)
{
img=Image.createImage(wd,ht);
Graphics g=img.getGraphics();
g.drawRect(0,5,wd,ht-10);
try{
Image img_p=Image.createImage(img_nm);
g.drawImage(img_p,5,55,Graphics.TOP|Graphics.LEFT);
}catch(Exception e)
{}
repaint();
System.out.println("after repaint for image "+img_nm);
}
public void paint(Graphics g)
{
if(img!=null)
g.drawImage(img,0,0,Graphics.TOP|Graphics.LEFT);
}
public void commandAction(Command c,Displayable d)
{
if(c==cmdExit)
{
anim.cancel();
midlet.notifyDestroyed();
}
else if(c==cmdStart)
{
start();
}
else if(c==cmdStop)
{
stop();
}
}
}

//***********Anim Class started******************
import java.util.*;

public class Anim extends TimerTask
{
String[] img_nm={"/puppy_frame2.png","/puppy_frame3.png","/puppy_frame4.png","/puppy_frame5.png","/puppy_frame1.png"};
int current=0;
MainCanvas mcan;
public Anim()
{
}
public Anim(MainCanvas m)
{super();
mcan=m;
}
public void run()
{
mcan.drawnew(img_nm[current]);
if(current<4)
current++;
else
current=0;
}

}
Rishi Tyagi
Ranch Hand

Joined: Feb 14, 2002
Posts: 100
Dear friends I have made some modifications in the above Anim and MainCanvas class . It works but with too much flickering.
Now i would like to ask some more questions:-
Q 1:- Is this the right way to show Animations in j2me?
if answer is Yes then How can we remove the flickering, I mean to say how can we get the smoth animation here as this.
If No then what are the other right ways to show the same?
New Code for MainCanvas and Anim Classes
//--------------Code for new MainCanvas Class----
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class MainCanvas extends Canvas implements CommandListener
{
MIDlet midlet;
Display display;
int wd=0,ht=0;
Image img,img1,img2,img3,img4;
long interval=100;
private Command cmdExit,cmdStart,cmdStop;
Anim anim;
int current_ctr=0;
public MainCanvas(){}
public MainCanvas(Display d,MIDlet m)
{
display=d;
midlet=m;
wd=getWidth();
ht=getHeight();
cmdExit=new Command("Exit",Command.SCREEN,0);
cmdStart=new Command("Start",Command.SCREEN,0);
cmdStop=new Command("Stop",Command.SCREEN,0);
addCommand(cmdExit);
addCommand(cmdStart);
//addCommand(cmdStop);
setCommandListener(this);
System.out.println("width :"+wd);
System.out.println("height :"+ht);
img=Image.createImage(wd,ht);
Graphics g=img.getGraphics();
g.drawRect(0,5,wd,ht-10);
try{
Image img_p=Image.createImage("/puppy_frame1.png");
g.drawImage(img_p,5,55,Graphics.TOP|Graphics.LEFT);
}catch(Exception e)
{}
img1=Image.createImage(wd,ht);
g=img1.getGraphics();
g.drawRect(0,5,wd,ht-10);
try{
Image img_p=Image.createImage("/puppy_frame2.png");
g.drawImage(img_p,5,55,Graphics.TOP|Graphics.LEFT);
}catch(Exception e)
{}
img2=Image.createImage(wd,ht);
g=img2.getGraphics();
g.drawRect(0,5,wd,ht-10);
try{
Image img_p=Image.createImage("/puppy_frame3.png");
g.drawImage(img_p,5,55,Graphics.TOP|Graphics.LEFT);
}catch(Exception e)
{}
img3=Image.createImage(wd,ht);
g=img3.getGraphics();
g.drawRect(0,5,wd,ht-10);
try{
Image img_p=Image.createImage("/puppy_frame4.png");
g.drawImage(img_p,5,55,Graphics.TOP|Graphics.LEFT);
}catch(Exception e)
{}
img4=Image.createImage(wd,ht);
g=img4.getGraphics();
g.drawRect(0,5,wd,ht-10);
try{
Image img_p=Image.createImage("/puppy_frame5.png");
g.drawImage(img_p,5,55,Graphics.TOP|Graphics.LEFT);
}catch(Exception e)
{}
d.setCurrent(this);
}
public void start()
{
try{
removeCommand(cmdStart);
}catch(Exception e)
{
}
try{
addCommand(cmdStop);
}catch(Exception e)
{
}
anim=new Anim(this);
java.util.Timer t=new java.util.Timer();
t.schedule(anim,new java.util.Date(),interval);
//t.schedule(anim,interval);
}
public void stop()
{
anim.cancel();
try{
removeCommand(cmdStop);
}catch(Exception e)
{
}
try{
addCommand(cmdStart);
}catch(Exception e)
{
}
}
public void drawnew(int ctr)
{
current_ctr=ctr;
/*img=Image.createImage(wd,ht);
Graphics g=img.getGraphics();
g.drawRect(0,5,wd,ht-10);
try{
Image img_p=Image.createImage(img_nm);
g.drawImage(img_p,5,55,Graphics.TOP|Graphics.LEFT);
}catch(Exception e)
{}
*/
repaint();
System.out.println("after repaint for image "+ctr);
}
public void paint(Graphics g)
{
if(current_ctr==0)
g.drawImage(img,0,0,Graphics.TOP|Graphics.LEFT);
else if(current_ctr==1)
g.drawImage(img1,0,0,Graphics.TOP|Graphics.LEFT);
else if(current_ctr==2)
g.drawImage(img2,0,0,Graphics.TOP|Graphics.LEFT);
else if(current_ctr==3)
g.drawImage(img3,0,0,Graphics.TOP|Graphics.LEFT);
else if(current_ctr==4)
g.drawImage(img4,0,0,Graphics.TOP|Graphics.LEFT);
}
public void commandAction(Command c,Displayable d)
{
if(c==cmdExit)
{
anim.cancel();
midlet.notifyDestroyed();
}
else if(c==cmdStart)
{
start();
}
else if(c==cmdStop)
{
stop();
}
}
}
//--------------Code for new Anim Class-------
import java.util.*;
public class Anim extends TimerTask
{
String[] img_nm={"/puppy_frame2.png","/puppy_frame3.png","/puppy_frame4.png","/puppy_frame5.png","/puppy_frame1.png"};
int current=0;
MainCanvas mcan;
public Anim()
{
}
public Anim(MainCanvas m)
{super();
mcan=m;
}
public void run()
{
//modified here
mcan.drawnew(current);
if(current<4)
current++;
else
current=0;
}

}
a sanjuan
Ranch Hand

Joined: Jun 12, 2002
Posts: 164
the link above about creating flicker free graphics in MIDP should help:
http://wireless.java.sun.com/midp/ttips/flickgraphics/
Lance Titchkosky
Ranch Hand

Joined: Apr 30, 2002
Posts: 36
For my J2ME project I used threading instead of the timer to create animation. This way was more intuitive to me since I'm familar with threads.. Anyways either approach should work.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Help Needed on animation in j2me