Namespaces
Variants
Actions

Simple MIDP2 screen design pattern

Jump to: navigation, search
Article Metadata

Article
Created: ed_welch (29 May 2007)
Last edited: hamishwillee (18 Jul 2012)

It's a bad idea to have more than one class inherited from GameCanvas, as it's off-screen graphics buffer uses up extra memory for each class you create(it actually says this in the offical docuementation). Also some phones have flicker when you switch screens using Display.setCurrent.

It's easy to get around this by defining an abstract BaseScreen class as follows:

import javax.microedition.lcdui.Graphics;
 
public abstract class BaseScreen
{
protected MainCanvas canvas;
protected BaseScreen prevScreen;
public BaseScreen(MainCanvas canvas)
{
this.canvas = canvas;
}
public void init(BaseScreen caller)
{
prevScreen = caller;
canvas.setCurrScreen(this);
start();
}
protected abstract void start();
public abstract void keyPressed(int keyCode, int gameCode);
protected void keyReleased(int keyCode, int gameCode)
{}
 
 
public MainCanvas getCanvas()
{
return canvas;
}
 
}

Then every class that represents a "screen" just inherets from this BaseScreen class:

import javax.microedition.lcdui.Graphics;
 
public class MenuScreen extends BaseScreen
{
public MenuScreen(TestCanvas canvas)
{
super(canvas);
}
 
protected void start()
{
Graphics g = canvas.g;
// ... draw screen here
canvas.flushGraphics();
} // start
 
 
public void keyPressed(int keyCode, int gameCode)
{
if (keyCode == backButton && prevScreen != null)
{
prevScreen.init(null);
}
} // keyPressed
 
}

Then in your main canvas class you keep track of the current screen and pass the keyPressed and keyReleased events.

public class TestCanvas extends GameCanvas 
{
private testMidlet midlet;
private MenuScreen mainMenu;
private BaseScreen currScreen;
public Graphics g = null;
 
public TestCanvas(testMidlet midlet)
{
super(false);
this.midlet = midlet;
setFullScreenMode(true);
mainMenu = new MenuScreen(this);
}
 
public void start()
{
g = getGraphics();
mainMenu.init(null);
}
 
protected void keyPressed(int keyCode)
{
int gameCode = -1;
try
{
gameCode = getGameAction(keyCode);
} catch (Exception e) {}
if (currScreen != null)
{
currScreen.keyPressed(keyCode, gameCode);
}
} // keyPressed
 
protected void keyReleased(int keyCode)
{
int gameCode = -1;
try
{
gameCode = getGameAction(keyCode);
} catch (Exception e) {}
if (currScreen != null)
{
currScreen.keyReleased(keyCode, gameCode);
}
} // keyReleased
public void setCurrScreen(BaseScreen currScreen)
{
this.currScreen = currScreen;
}
 
}

Notice that we have a prevScreen reference, so the user can always go back to the previous screen by pressing the back button (usually the right softkey).

You could also go the non-object-oriented route and just put a bunch of giant switch statements in your canvas class, however this makes your code very hard to manage (especially for large projects).

This page was last modified on 18 July 2012, at 14:00.
67 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved