Tuesday, August 30, 2011

Simple Mobile Game in J2ME(Canvas Continued):-

In previous tutorial we learned about a simple canvas class.
In this tutoial we will try to create a simple gaming interface using Canvas.
In this code we have created a canvas class and created its object in our Midlet.
Inside the constructor SampleCanvas() , we read a image and then create a sprite ,which represent the moving item inside the game.
You can download sample sprite image from here .You have to paste it inside source(src) folder of your project.
Then whenever a key is pressed ,the canvas function keyPressed() is called automatically.
we define this function to detect the key pressed and then call the check function for the first time(when thread is not started ).
And thereafter it changes the direction of movement by changing keycode.
Check function is created to start the thread and hence the movement of sprite.
Thread ,when started calls the handleaction function which decides the direction  of movement of sprite according to the key pressed.
And finally repaint function changes the position of sprite accordingly.

We conclude this tutorial here and  will discuss in depth about various other provisions that can be made in subsequent tutorial.


import java.io.IOException;
import java.lang.Thread;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.Sprite;

public class GameMidlet extends MIDlet {

    private SampleCanvas gameCanvas;
    private Thread t=null;
    private Display d;

    public void startApp() {
        try {
            this.gameCanvas = new SampleCanvas();
        } catch (IOException ex) {
        d = Display.getDisplay(this);

    public void pauseApp() {

    public void destroyApp(boolean unconditional) {

class SampleCanvas extends Canvas implements Runnable {
    int     x, y;           // Location of cross hairs
    String  event = "";     // Last key event type
    int     keyCode;        // Last keyCode pressed
    int     w, h;           // width and height of the canvas
    Image img;
    Sprite sprite;
    boolean flag;
    Thread t1;
    SampleCanvas() throws IOException {
       sprite=new Sprite(img);
        w = getWidth();
        h = getHeight();


    protected void keyPressed(int key) {
        keyCode = key;
        event = "Pressed";

   protected void Check()
        t1=new Thread(this);

        void handleActions(int keyCode) {
        int action = getGameAction(keyCode);
        switch (action) {
            case LEFT:
            x -= 1;
            case RIGHT:
            x += 1;
            case UP:
            y -= 1;
            case DOWN:
            y += 1;

    protected void paint(Graphics g) {
        g.setColor(255,255, 255);
        g.fillRect(0, 0, w, h);
        sprite.setRefPixelPosition(0,0) ;
        sprite.setPosition(x, y);
        sprite.paint(g) ;

    public void run() {
            try {
            } catch (InterruptedException ex) {

No comments:

Post a Comment

customised by Vaibhav