// Create a Canvas for processing game actions import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class MapGameActions extends MIDlet { private Display display; // A display private GameActionCanvas canvas; // canvas public MapGameActions() { display = Display.getDisplay(this); canvas = new GameActionCanvas(this); } protected void startApp(){ display.setCurrent(canvas); } protected void pauseApp(){ } protected void destroyApp(boolean unconditional){ } public void exitMIDlet(){ destroyApp(true); notifyDestroyed(); } } /* Game action event handling */ class GameActionCanvas extends Canvas implements CommandListener{ private Command cmExit; // exit command private String keyText = null; // key code text private MapGameActions midlet; // constructor public GameActionCanvas(MapGameActions midlet) { this.midlet = midlet; // create exit command & listen for events cmExit = new Command("Exit", Command.EXIT, 1); addCommand(cmExit); setCommandListener(this); } // paint the text representing the key code protected void paint(Graphics g) { g.setColor(255,255,255); // clear the background to white g.fillRect(0,0, getWidth(), getHeight()); // set colour and draw text if(keyText != null) { // draw with red pen g.setColor(255, 0, 0); // centre the text g.drawString(keyText, getWidth()/2, getHeight()/2, Graphics.TOP | Graphics.HCENTER); } } // event handling public void commandAction(Command c, Displayable d){ if (c == cmExit) midlet.exitMIDlet(); } // game action event handling and converted into key code protected void keyPressed(int keyCode) { switch(getGameAction(keyCode)) { // place action inside the case case UP: case DOWN: case LEFT: case FIRE: case GAME_A: case GAME_B: case GAME_C: case GAME_D: default: // The default value to be the text of the agme action keyText = getKeyName(keyCode); } repaint(); } }