how to show a strip of image in a single keypress?

Forum for J2ME mobile games related topics including programming doubts, books and other resources for J2ME game development

how to show a strip of image in a single keypress?

Postby itskrishnaraj » Fri Sep 05, 2008 10:59 am

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
itskrishnaraj
 
Posts: 39
Joined: Thu Aug 07, 2008 1:28 pm

Re: how to show a strip of image in a single keypress?

Postby DevelopmentTeam » Fri Sep 05, 2008 11:31 am

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.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: how to show a strip of image in a single keypress?

Postby itskrishnaraj » Fri Sep 05, 2008 11:49 am

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.



my problem is i am having a strip of image which contain 16 images like a walking man...i jus want to complete the enyire strip in a single key press like i have to complete a walk in a single keypress

here is sample midlet for walking

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 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
       
    }
   
   
}


in my code when i press a key it showing the next frame only not the entire frame

i guess you got my problem
itskrishnaraj
 
Posts: 39
Joined: Thu Aug 07, 2008 1:28 pm

Re: how to show a strip of image in a single keypress?

Postby DevelopmentTeam » Sat Sep 06, 2008 4:46 pm

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.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: how to show a strip of image in a single keypress?

Postby itskrishnaraj » Mon Sep 08, 2008 5:27 am

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.



thanks a lot an..its solved my porblem...
itskrishnaraj
 
Posts: 39
Joined: Thu Aug 07, 2008 1:28 pm

Re: how to show a strip of image in a single keypress?

Postby DevelopmentTeam » Mon Sep 08, 2008 8:18 am

Yes, thanks
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India


Return to J2ME Games

Who is online

Users browsing this forum: No registered users and 3 guests

cron