GameActionDemo.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:3k
源码类别:

J2ME

开发平台:

Java

  1. import javax.microedition.midlet.*;
  2. import javax.microedition.lcdui.*;
  3. public class GameActionDemo extends MIDlet {
  4.     private Display display;
  5.     public GameActionDemo() {
  6.         display=Display.getDisplay(this);
  7.     }
  8.     
  9.     public void startApp() throws MIDletStateChangeException {
  10.         display.setCurrent(new GameActionDemoCanvas());
  11.     }
  12.     /**
  13.      * Pause the MIDlet
  14.      */
  15.     public void pauseApp() {
  16.     }
  17.     /**
  18.      * Called by the framework before the application is unloaded
  19.      */
  20.     public void destroyApp(boolean unconditional) {
  21.     }
  22.     class GameActionDemoCanvas extends Canvas {
  23.         int width, height;
  24.         int deltaX, deltaY;
  25.         int x,y;
  26.         
  27.         public GameActionDemoCanvas() {      
  28.             //draw a 8x10 net
  29.             deltaX=this.getWidth()/8;
  30.             deltaY=this.getHeight()/10;
  31.             width=deltaX*8;
  32.             height=deltaY*10;
  33.             x=0;
  34.             y=0;
  35.         }
  36.         public void paint(Graphics g) {         
  37.             //set background to white
  38.             g.setColor(0xFFFFFF);
  39.             g.fillRect(0,0,width,height);
  40.             
  41.             //set foreground color to black
  42.             g.setColor(0x000000);
  43.             g.fillRect(x,y,deltaX,deltaY);
  44.         }
  45.         
  46.         public void keyPressed(int keycode) {
  47.             switch(getGameAction(keycode)) {
  48.             case Canvas.DOWN:
  49.                 y+=deltaY;
  50.                 if(y>=height) {
  51.                     y-=height;
  52.                     repaint(x,0,deltaX,height);
  53.                 }
  54.                 else {
  55.                     repaint(x,y-deltaY,deltaX,2*deltaY);
  56.                 }
  57.                 break;
  58.             case Canvas.UP:
  59.                 y-=deltaY;
  60.                 if(y<0) {
  61.                     y+=height;
  62.                     repaint(x,0,deltaX,height);
  63.                 }
  64.                 else {
  65.                     repaint(x,y,deltaX,2*deltaY);
  66.                 }
  67.                 break;
  68.             case Canvas.LEFT:
  69.                 x-=deltaX;
  70.                 if(x<0) {
  71.                     x+=width;
  72.                     repaint(0,y,width,deltaY);
  73.                 }
  74.                 else {
  75.                     repaint(x,y,deltaX*2,deltaY);
  76.                 }
  77.                 break;
  78.             case Canvas.RIGHT:
  79.                 x+=deltaX;
  80.                 if(x>=width) {
  81.                     x-=width;
  82.                     repaint(0,y,width,deltaY);
  83.                 }
  84.                 else {
  85.                     repaint(x-deltaX,y,deltaX*2,deltaY);
  86.                 }
  87.                 break;
  88.             default:
  89.             }
  90.         }
  91.     }
  92. }