java left logo
java middle logo
java right logo
 

Home arrow Java ME Tips arrow MIDP arrow How to write your First MIDP game
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4094
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to write your First MIDP game E-mail
User Rating: / 16
PoorBest 

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.getDisplaythis );
        display.setCurrentnew GameMidletScreen() );
    }
    
    protected void destroyAppboolean 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 Threadthis ).start();
        }
        
        public void loadImages() {
            try {
                if!isDoubleBuffered() ) {
                    img1 = Image.createImagegetWidth(), getHeight() );
                    g1 = img1.getGraphics();
                }
                img2 = Image.createImage("/game.png");
            catchException e ) {
                e.printStackTrace();
            }
        }
        
        public void run() {
            whiletrue ) {
                int loopDelay = 1000 10;
                long loopStartTime = System.currentTimeMillis();
                doSomething();
                long loopEndTime  = System.currentTimeMillis();
                int loopTime = (int)(loopEndTime - loopStartTime);
                ifloopTime < loopDelay ) {
                    try {
                        Thread.sleeploopDelay - loopTime );
                    catchException e ){}
                    
                }
            }
        }
        
        public void doSomething() {
            int ImageSpeed = 4;
            switchDirection ) {
                case LEFT:
                    X-=ImageSpeed;
                    break;
                case RIGHT:
                    X+=ImageSpeed;
                    break;
                case UP:
                    Y-=ImageSpeed;
                    break;
                case DOWN:
                    Y+=ImageSpeed;
                    break;
            }
            repaint();
            serviceRepaints();
        }
        
        protected void paintGraphics g ) {
            Graphics original = g;
            if!isDoubleBuffered() ) {
                g = g1;
            }
            g.fillRect00this.getWidth()this.getHeight() );
            g.drawImageimg2, X, Y, Graphics.VCENTER | Graphics.HCENTER );
            
            if!isDoubleBuffered() ) {
                original.drawImageimg1, 00, Graphics.TOP | 
                        Graphics.LEFT );
            }
        }
        
        protected void keyPressedint keyCode ) {
            int gameAction = getGameActionkeyCode );
            switchgameAction ) {
                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 keyReleasedint keyCode ) {
            Direction = 0;
        }
    }
}

 Related Tips


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.