This page was last modified 14:17, 29 June 2007.
Simple MIDP2 screen design pattern
From Forum Nokia Wiki
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).
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Universal games(very urgent) | kiranputhran | Mobile Java General | 1 | 2003-04-03 07:12 |
| New to Symbian, programming, advice needed | papillon68 | General Symbian C++ | 3 | 2006-09-18 05:26 |
| Non ui-design controls | nadav70 | Symbian User Interface | 1 | 2008-02-11 10:19 |
| Sprites on J2ME (again) | mugent | Mobile Java General | 3 | 2003-02-01 11:41 |
| at least 7 MIDP2 phones supported today in the US, Dec. 8, 2003 | zhaofei8009 | Other Programming Discussion 关于其他编程技术的讨论 | 1 | 2003-12-15 09:23 |
