Basic Java ME Game Template Part 3
Jump to part: 1 | 2 | 3

In this last part of the tutorial entitled Basic Java ME Game Template, we will modify the code of our MIDlet class create and launch the clsCanvas class the application to run. It is recommended that you start with Part 1 of this tutorial if you haven't done so.


Now open the source code of our MIDlet. We will define a few variables just under the MIDlet declaration.

public class midMain extends MIDlet {
private clsCanvas myCanvas;
public boolean isPaused = false;

...
...


The variable myCanvas will hold an instance of the clsCanvas class. The variable isPaused will tell us if the MIDlet was paused or lost focus. Next, we add some code to the startApp() for launching or starting our main loop and make our clsCanvas object visible.

    public void startApp() {
// check if MIDlet was paused
if (!isPaused){
// initialized only once
// create an instance of clsCanvas
myCanvas = new clsCanvas(this);
// get display
Display d = Display.getDisplay(this);
// make clsCanvas visible
d.setCurrent(myCanvas);
// launch the thread/start main loop
myCanvas.start();
}
//no longer paused
isPaused = false;

}


The Display object is responsible for making objects visible to the screen. In the code above, we retreived the Display object associated with the MIDlet and set the clsCanvas object as the current visible object on the device. We also called the start() method of the clsCanvas object to launch a thread and start our main loop.

Next, we need to modify the pauseApp() method so that our variable "isPaused" will be updated everytime the MIDlet looses focus or minimized into the background by the device.

    public void pauseApp() {
// MIDlet is paused or not the active window
isPaused = true;

}


The last code we will add is for making sure we release all the resources our MIDlet has used and terminate gracefully when your game has ended. Place this code in the destroyApp() method:

        public void destroyApp(boolean unconditional) {
myCanvas = null;
// close MIDlet
notifyDestroyed();

}


Here's the completed midMain.java source code:

package pkgGame;

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.*;

public class midMain extends MIDlet {
private clsCanvas myCanvas;
public boolean isPaused = false;

public void startApp() {
// check if MIDlet was paused
if (!isPaused){
// initialized only once
// create an instance of clsCanvas
myCanvas = new clsCanvas(this);
// get display
Display d = Display.getDisplay(this);
// make clsCanvas visible
d.setCurrent(myCanvas);
// launch the thread/start main loop
myCanvas.start();
}
//no longer paused
isPaused = false;
}

public void pauseApp() {
// MIDlet is paused or not the active window
isPaused = true;
}

public void destroyApp(boolean unconditional) {
myCanvas = null;
// close MIDlet
notifyDestroyed();
}
}


Here's the completed clsCanvas.java source code:

package pkgGame;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

public class clsCanvas extends GameCanvas implements Runnable {
private boolean isRunning = true;
private Graphics g;
private midMain p;

public clsCanvas(midMain parent) {
super(false);
p = parent;
}

public void start(){
// creat a new instance of the Thread class
Thread thread = new Thread(this);
// launch the thread
thread.start();
}

public void run() {
g = getGraphics();
while(isRunning){
// get key state
int iKey = getKeyStates();
// check if FIRE or 5 is pressed
if ((iKey & FIRE_PRESSED) != 0){
// signal an exit
isRunning = false;
}

//set drawing color to black
g.setColor(0x000000);
//fill the whole screen with black
g.fillRect(0, 0, getWidth(), getHeight());
// set drawing color to white
g.setColor(0xffffff);
//display a string
g.drawString("Basic Java ME Game Template", 2, 2, Graphics.TOP | Graphics.LEFT);
//display the key states in white
g.drawString(Integer.toString(iKey), 2, 22, Graphics.TOP | Graphics.LEFT);

flushGraphics();

try{
// make this thread sleep for 30 ms
Thread.sleep(30);
} catch(Exception e){

}
}
// mark as garbage
g = null;
// close the MIDlet
p.destroyApp(true);
p = null;
}

}


Yay! We're done! You can now test your MIDlet by pressing the F6 key on your keyboard or clicking on the Run Main Project icon on the toolbar.

Emulator output : Basic Java ME Game Template


You can download the source code to for this tutorial here : myGame.rar (26.4 Kb)

That ends our tutorial on creating a basic template for your game. I will also be using this template for the rest of the tutorials that will be posted here. If you have some comments, suggestions or having problem with the source code, feel free to leave a comment or use the ShoutBox. Have fun.

4 comments   |   post a comment
said...
Very thorough and informative.No
trouble following code (copy/paste),can't wait for more tutorials.
thanks, i ll try.
When I bought my computer and I didn´t know how to use java graphics, so I decided looking for information in a webside and I found an useful information that helped me a lot.. Now I am interested in to do the best investment and I found a webside very useful and interesting called costa rica investment opportunities , I think it´s a very wonderful site.
thanks a lot for sharing this.

very useful info!!


cheerS!!


dan
-------------------
‫‪‫burraco online