import javax.microedition.lcdui.*; import javax.microedition.lcdui.game.*; public class MCanvas extends Canvas implements Runnable{ private Graphics gBuffer;//Buffer private Image screenImage;//Buffer private static int scw=176;//screenWidth private static int sch=220;//screenHeight private boolean runFlag=true; private Thread thread1=new Thread(this); private Font f=Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_PLAIN,Font.SIZE_SMALL); private Image fonMenu; private int PRESS_UP=1; private int PRESS_DOWN=2; private int PRESS_LEFT=4; private int PRESS_RIGHT=8; private int PRESS_FIRE=0x10; private int PRESS_KEY_1=0x20; private int PRESS_KEY_2=0x40; private int PRESS_KEY_3=0x80; private int PRESS_KEY_4=0x100; private int PRESS_KEY_5=0x200; private int PRESS_KEY_6=0x400; private int PRESS_KEY_7=0x800; private int PRESS_KEY_8=0x1000; private int PRESS_KEY_9=0x2000; private int PRESS_KEY_0=0x4000; private int PRESS_POUND=0x8000; private int PRESS_STAR=0x10000; private int keyNumPress; public MCanvas() { super(); setFullScreenMode(true); screenImage=Image.createImage(scw,sch); gBuffer=screenImage.getGraphics(); } //Draw public void draw(Graphics g){ } /** * paint */ public void paint(Graphics g) { g.drawImage(screenImage,0,0,Graphics.TOP|Graphics.LEFT); } /** * Cikl */ public void run(){ while (runFlag) { draw(gBuffer); repaint(); try { Thread.sleep(20); } catch (Exception exc) { } } } /** * Called when a key is pressed. */ public void keyPressed(int keyCode) { switch (keyCode) { case KEY_NUM1: keyNumPress|=PRESS_KEY_1; break; case KEY_NUM2: keyNumPress|=PRESS_KEY_2; break; case KEY_NUM3: keyNumPress|=PRESS_KEY_3; break; case KEY_NUM4: keyNumPress|=PRESS_KEY_4; break; case KEY_NUM5: keyNumPress|=PRESS_KEY_5; break; case KEY_NUM6: keyNumPress|=PRESS_KEY_6; break; case KEY_NUM7: keyNumPress|=PRESS_KEY_7; break; case KEY_NUM8: keyNumPress|=PRESS_KEY_8; break; case KEY_NUM9: keyNumPress|=PRESS_KEY_9; break; case KEY_NUM0: keyNumPress|=PRESS_KEY_0; break; case KEY_STAR: keyNumPress|=PRESS_STAR; break; case KEY_POUND: keyNumPress|=PRESS_POUND; break; default: break; } switch (getGameAction(keyCode)) { case UP: if (keyCode!=KEY_NUM2)keyNumPress|=PRESS_UP; break; case DOWN: if (keyCode!=KEY_NUM8)keyNumPress|=PRESS_DOWN; break; case LEFT: if (keyCode!=KEY_NUM4)keyNumPress|=PRESS_LEFT; break; case RIGHT: if (keyCode!=KEY_NUM6)keyNumPress|=PRESS_RIGHT; break; case FIRE: if (keyCode!=KEY_NUM5)keyNumPress|=PRESS_FIRE; break; } } /** * Called when a key is released. */ public void keyReleased(int keyCode){ switch (keyCode) { case KEY_NUM1: keyNumPress=(keyNumPress^PRESS_KEY_1)&(~PRESS_KEY_1); break; case KEY_NUM2: keyNumPress=(keyNumPress^PRESS_KEY_2)&(~PRESS_KEY_2); break; case KEY_NUM3: keyNumPress=(keyNumPress^PRESS_KEY_3)&(~PRESS_KEY_3); break; case KEY_NUM4: keyNumPress=(keyNumPress^PRESS_KEY_4)&(~PRESS_KEY_4); break; case KEY_NUM5: keyNumPress=(keyNumPress^PRESS_KEY_5)&(~PRESS_KEY_5); break; case KEY_NUM6: keyNumPress=(keyNumPress^PRESS_KEY_6)&(~PRESS_KEY_6); break; case KEY_NUM7: keyNumPress=(keyNumPress^PRESS_KEY_7)&(~PRESS_KEY_7); break; case KEY_NUM8: keyNumPress=(keyNumPress^PRESS_KEY_8)&(~PRESS_KEY_8); break; case KEY_NUM9: keyNumPress=(keyNumPress^PRESS_KEY_9)&(~PRESS_KEY_9); break; case KEY_NUM0: keyNumPress=(keyNumPress^PRESS_KEY_0)&(~PRESS_KEY_0); break; case KEY_STAR: keyNumPress=(keyNumPress^PRESS_STAR)&(~PRESS_STAR); break; case KEY_POUND: keyNumPress=(keyNumPress^PRESS_POUND)&(~PRESS_POUND); break; default: break; } switch (getGameAction(keyCode)) { case UP: if (keyCode!=KEY_NUM2)keyNumPress=(keyNumPress^PRESS_UP)&(~PRESS_UP); break; case DOWN: if (keyCode!=KEY_NUM8)keyNumPress=(keyNumPress^PRESS_DOWN)&(~PRESS_DOWN); break; case LEFT: if (keyCode!=KEY_NUM4)keyNumPress=(keyNumPress^PRESS_LEFT)&(~PRESS_LEFT); break; case RIGHT: if (keyCode!=KEY_NUM6)keyNumPress=(keyNumPress^PRESS_RIGHT)&(~PRESS_RIGHT); break; case FIRE: if (keyCode!=KEY_NUM5)keyNumPress=(keyNumPress^PRESS_FIRE)&(~PRESS_FIRE); break; } } /** * begin Game */ public void startGame(){ thread1.start(); } /** * end Game */ public void stopGame(){ runFlag=false; } }