When developing a game, probably the most important thing is to draw on screen. javax.microedition.lcdui packages gives the facilities to implement drawing on the screen. Canvas,Graphics and Image are main classes used for low level graphics.

Illustrated below is an example of basic game that create and draw image to a Canvas,set up a game loop and handle key events.

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

public class GameMidlet extends MIDlet {
    
    public GameMidlet() {
    }
    
    protected void startApp() throws MIDletStateChangeException {
        Display display = Display.getDisplay( this );
        display.setCurrent( new GameMidletScreen() );
    }
    
    protected void destroyApp( boolean unconditional ) throws 
            MIDletStateChangeException {
    }
    
    protected void pauseApp(){}
    
    class GameMidletScreen extends Canvas implements Runnable {
        
        Random generator;
        Graphics g1;
        Image img1;
        Image img2;
        int X;
        int Y;
        int Direction;
        
        public GameMidletScreen() {
            loadImages();
            X = getWidth()/2;
            Y = getHeight()/2;
            new Thread( this ).start();
        }
        
        public void loadImages() {
            try {
                if( !isDoubleBuffered() ) {
                    img1 = Image.createImage( getWidth(), getHeight() );
                    g1 = img1.getGraphics();
                }
                img2 = Image.createImage("/game.png");
            } catch( Exception e ) {
                e.printStackTrace();
            }
        }
        
        public void run() {
            while( true ) {
                int loopDelay = 1000 / 10;
                long loopStartTime = System.currentTimeMillis();
                doSomething();
                long loopEndTime  = System.currentTimeMillis();
                int loopTime = (int)(loopEndTime - loopStartTime);
                if( loopTime < loopDelay ) {
                    try {
                        Thread.sleep( loopDelay - loopTime );
                    } catch( Exception e ){}
                    
                }
            }
        }
        
        public void doSomething() {
            int ImageSpeed = 4;
            switch( Direction ) {
                case LEFT:
                    X-=ImageSpeed;
                    break;
                case RIGHT:
                    X+=ImageSpeed;
                    break;
                case UP:
                    Y-=ImageSpeed;
                    break;
                case DOWN:
                    Y+=ImageSpeed;
                    break;
            }
            repaint();
            serviceRepaints();
        }
        
        protected void paint( Graphics g ) {
            Graphics original = g;
            if( !isDoubleBuffered() ) {
                g = g1;
            }
            g.fillRect( 0, 0, this.getWidth(), this.getHeight() );
            g.drawImage( img2, X, Y, Graphics.VCENTER | Graphics.HCENTER );
            
            if( !isDoubleBuffered() ) {
                original.drawImage( img1, 0, 0, Graphics.TOP | 
                        Graphics.LEFT );
            }
        }
        
        protected void keyPressed( int keyCode ) {
            int gameAction = getGameAction( keyCode );
            switch( gameAction ) {
                case LEFT:
                    //move image left
                    Direction = LEFT;
                    break;
                case RIGHT:
                    //move image right
                    Direction = RIGHT;
                    break;
                case UP:
                    //move image up
                    Direction = UP;
                    break;
                case DOWN:
                    //move image down
                    Direction = DOWN;
                    break;
            }
        }
        
        protected void keyReleased( int keyCode ) {
            Direction = 0;
        }
    }
}

 

 Related Tips