i m having a frame of image..i jus want to show it the whole frames in a single click..
pls give me any suggestion or sample code
frames of image -sprite
DevelopmentTeam wrote:why don't you use sprite class, do you have any problem using it? You can use a sprite class and run it in a thread and initiate that thread when the click happens.
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
public class clsCanvas extends GameCanvas implements Runnable
{
/************************************/
private boolean isRunning = true;
private Graphics g;
private midMain fParent;
/***********************************/
private Image clwalk;
private int clwalkXstart;
private int clnX=0;
//private boolean h=false;
/**************************************/
public clsCanvas(midMain m) //constructor to initilization
{
super(true);
fParent=m;
try
{
clwalk = Image.createImage("/cl_walk_240x320.png");
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
/*************************************/
public void start() //start game thread here
{
Thread runner = new Thread(this);
runner.start();
}
/************************************/
public void run()
{ //run method
int iKey = 0;
g = getGraphics();
while(isRunning)
{
drawPlayer(g);
iKey = getKeyStates();
if ((iKey & GameCanvas.RIGHT_PRESSED) != 0) //keyPress_RIGHT
{
System.out.println("right_key_pressed");
moveStrip();
}
flushGraphics();
try
{
Thread.sleep(30);
}
catch (Exception ex)
{ }
}//while ends here
g = null;
fParent.destroyApp(false);
fParent = null;
}//run ends here
/*****************************************/
private void drawPlayer(Graphics g) //drawPlayer() Method [called by run()]
{
g.setColor(0x000000);
g.fillRect(0,0,getWidth(),getHeight());
g.drawRegion(clwalk,clwalkXstart, 0, 26, 41, 0, clnX+32,180, Graphics.TOP|Graphics.LEFT);
try
{
Thread.sleep(40);
} catch (InterruptedException ex)
{
ex.printStackTrace();
}
}//DrawPlayer ends here
/****************************************/
private void moveStrip() //moveStrip() method [called by run()]
{
clwalkXstart +=26; //clipX updating to 26
if(clwalkXstart==390)
clwalkXstart=0;
clnX +=2; //increasing the destinationX to 2
}
}
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
public class clsCanvas extends GameCanvas implements Runnable
{
private boolean isRunning = true;
private Graphics g;
private midMain fParent;
private Image clwalk;
private int clwalkXstart;
private int clnX=0;
private boolean playerRunning = false;
public clsCanvas(midMain m) //constructor to initilization
{
super(true);
fParent=m;
try {
clwalk = Image.createImage("/cl_walk_240x320.png");
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void start() //start game thread here
{
Thread runner = new Thread(this);
runner.start();
}
public void run()
{ //run method
int iKey = 0;
g = getGraphics();
while(isRunning)
{
drawPlayer(g);
iKey = getKeyStates();
if ((iKey & GameCanvas.RIGHT_PRESSED) != 0) //keyPress_RIGHT
{
playerRunning = true;
System.out.println("right_key_pressed");
}
if(playerRunning) {
moveStrip();
}
flushGraphics();
try {
Thread.sleep(30);
} catch (Exception ex) { }
}//while ends here
g = null;
fParent.destroyApp(false);
fParent = null;
}//run ends here
private void drawPlayer(Graphics g) //drawPlayer() Method [called by run()]
{
g.setColor(0x000000);
g.fillRect(0,0,getWidth(),getHeight());
g.drawRegion(clwalk,clwalkXstart, 0, 26, 41, 0, clnX+32,180, Graphics.TOP|Graphics.LEFT);
try {
Thread.sleep(40);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}//DrawPlayer ends here
private void moveStrip() //moveStrip() method [called by run()]
{
clwalkXstart +=26; //clipX updating to 26
if(clwalkXstart==390) {
clwalkXstart=0;
playerRunning = false;
}
clnX +=2; //increasing the destinationX to 2
}
}DevelopmentTeam wrote:
- Code: Select all
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
public class clsCanvas extends GameCanvas implements Runnable
{
private boolean isRunning = true;
private Graphics g;
private midMain fParent;
private Image clwalk;
private int clwalkXstart;
private int clnX=0;
private boolean playerRunning = false;
public clsCanvas(midMain m) //constructor to initilization
{
super(true);
fParent=m;
try {
clwalk = Image.createImage("/cl_walk_240x320.png");
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void start() //start game thread here
{
Thread runner = new Thread(this);
runner.start();
}
public void run()
{ //run method
int iKey = 0;
g = getGraphics();
while(isRunning)
{
drawPlayer(g);
iKey = getKeyStates();
if ((iKey & GameCanvas.RIGHT_PRESSED) != 0) //keyPress_RIGHT
{
playerRunning = true;
System.out.println("right_key_pressed");
}
if(playerRunning) {
moveStrip();
}
flushGraphics();
try {
Thread.sleep(30);
} catch (Exception ex) { }
}//while ends here
g = null;
fParent.destroyApp(false);
fParent = null;
}//run ends here
private void drawPlayer(Graphics g) //drawPlayer() Method [called by run()]
{
g.setColor(0x000000);
g.fillRect(0,0,getWidth(),getHeight());
g.drawRegion(clwalk,clwalkXstart, 0, 26, 41, 0, clnX+32,180, Graphics.TOP|Graphics.LEFT);
try {
Thread.sleep(40);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}//DrawPlayer ends here
private void moveStrip() //moveStrip() method [called by run()]
{
clwalkXstart +=26; //clipX updating to 26
if(clwalkXstart==390) {
clwalkXstart=0;
playerRunning = false;
}
clnX +=2; //increasing the destinationX to 2
}
}
Check out this code. I didnt test it, but read the code for the concept.
Users browsing this forum: No registered users and 1 guest