Listing 1
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
*
* @author mark
* @version
*/
public class MyGame
extends MIDlet
implements CommandListener {
private Display display;
private boolean initialized = false;
private Command exitCommand = new Command( "Exit", Command.EXIT,
1 );
private Displayable dummy;
/**
*
*/
public Ast() {
}
/**
* Performs any initializations necessary.
* Only runs once per MIDlet execution.
*/
private void init() {
if (!initialized) {
display = Display.getDisplay(
this );
dummy = new TextBox( "Placeholder",
"replace me with a game canvas",
30, TextField.ANY );
dummy.addCommand( exitCommand );
dummy.setCommandListener( this );
initialized = true;
}
}
public void startApp() {
init();
display.setCurrent( dummy );
}
public void pauseApp() {
}
/**
* Perform any cleanup and resource freeing necessary.
*/
public void destroyApp( boolean unconditional ) {
// we don't need to do anything here yet
}
/**
* Respond to commands, including exit
* On the exit command, cleanup and notify the execution environment
* that the MIDlet has destroyed itself.
*/
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp( false
);
notifyDestroyed();
}
}
}
Listing 2
import java.util.*;
import javax.microedition.lcdui.*;
public class Splash
extends Canvas {
private Display display;
private Displayable next;
private Timer timer = new Timer();
public Splash( Display display, Displayable next )
{
this.display = display;
this.next = next;
display.setCurrent( this );
}
protected void showNotify() {
timer.schedule( new TimerTask()
{ public void run() { displayNext(); }}
, 10000 );
}
protected void hideNotify() {
timer.cancel();
}
protected void keyPressed( int keyCode ) {
displayNext();
}
protected void pointerPressed( int x, int y
) {
displayNext();
}
private void displayNext() {
display.setCurrent( next
);
}
private void clear( Graphics gfx ) {
int r=0, g=0, b=0, gray=0;
boolean color = display.isColor();
// save out the current
color
if (color) {
r =
gfx.getRedComponent();
g =
gfx.getBlueComponent();
b =
gfx.getGreenComponent();
}
else {
gray
= gfx.getGrayScale();
}
// clear the graphics
context
gfx.setGrayScale( 255
);
gfx.fillRect( -1, -1,
getWidth() + 1, getHeight() + 1 );
// restore the current
color;
if (color) {
gfx.setColor(
r, g, b );
}
else {
gfx.setGrayScale(
gray );
}
}
protected void paint( Graphics g ) {
clear( g );
Image logo = null;
try {
logo = Image.createImage(
"/images/logo.png" );
}
catch (java.io.IOException e) {}
g.drawImage(logo, 0, 0, g.LEFT|g.TOP);
}
}
Make the following additions to MyGame.java.
/**
* Performs any initializations necessary and displays
the splas
*/
private void init() {
display = Display.getDisplay(
this );
dummy = new TextBox( "Placeholder",
"replace me with a game canvas",
30, TextField.ANY );
dummy.addCommand( exitCommand
);
dummy.setCommandListener( this
);
showSplash( display, dummy );
initialized = true;
}
public void showSplash( Display d, Displayable
next ) {
new Splash( display, next );
}
Listing 3
import javax.microedition.lcdui.*;
public class GameCanvas
extends Canvas
implements CommandListener {
/**
* Creates new GameCanvas
*/
public GameCanvas( MyGame myGame ) {
// Do initialization and set
an exit soft button
addCommand( new Command( "Exit",
Command.EXIT, 0 ) );
setCommandListener(this);
}
public void paint( Graphics g ) {
// Paint everything for the game
// First, erase the screen and then display the
background and actors
}
public void newGame() {
// Start a thread which contains a game loop
}
private void quitCanvas() {
// stop the realtime thread
}
public void quit() {
// return to the welcome screen
}
protected void keyReleased( int param ) {
// process keys when released
}
protected void keyPressed( int param ) {
// process keys when pressed
}
public void commandAction(Command p1, Displayable
p2) {
if (p1.getCommandType()
== Command.EXIT)
quitCanvas();
}
}
Listing 4
import javax.microedition.lcdui.*;
import java.util.*;
public class GameLoop
implements Runnable {
private GameCanvas gameCanvas;p> public GameLoop(
GameCanvas gameCanvas ){
}
private void init(){
}
private void processKeys(){
}
private void moveObjects(){
}
private void processCollision(){
}
// Thread entry when started
public void run() {
while(gameLoopState == STATE_RUN) {
processKeys();
moveObjects();
processCollision();
// force the game
canvas to paint the frame
gameCanvas.repaint();
gaemCanvas.serviceRepaints
();
}
}
// the function called by the game canvas every frame which
draws graphics
public void paint( Graphics g ) {
}
// quit the thread loop
public void quit() {
}
}