import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class KeyCodes extends MIDlet { private Display display; private KeyCodeCanvas canvas; public KeyCodes() { display = Display.getDisplay(this); canvas = new KeyCodeCanvas(this); } protected void startApp() { display.setCurrent(canvas); } protected void pauseApp() { } protected void destroyApp(boolean unconditional) { } public void exitMIDlet() { destroyApp(true); notifyDestroyed(); } } class KeyCodeCanvas extends Canvas implements CommandListener { private Command cmExit; private String keyText = "hello let@#s go!"; private KeyCodes midlet; public KeyCodeCanvas(KeyCodes midlet) { this.midlet = midlet; cmExit = new Command("Exit", Command.EXIT, 1); addCommand(cmExit); setCommandListener(this); } protected void paint(Graphics g) { System.out.println("I am invoked!"); g.setColor(0, 255, 0); g.fillRect(0, 0, getWidth(), getHeight()); if (keyText != null) { g.setColor(0, 0, 0); g.drawString(keyText, getWidth() / 2, getHeight() / 2, Graphics.TOP | Graphics.HCENTER); } } public void showNotify() { repaint(); try { Thread.sleep(2000); } catch(InterruptedException e) { } } public void commandAction(Command c, Displayable d) { if (c == cmExit) midlet.exitMIDlet(); } protected void keyPressed(int keyCode) { keyText = getKeyName(keyCode); repaint(); try { Thread.sleep(2000); } catch(InterruptedException e) { } } } |
|