画一个"@"字符随着键盘的左右而移动

  1. /*
  2.  *   文件名: BraveMidlet.java
  3.  *  开发工具: NetBeans 6.0.1
  4.  *  设置配置: CLDL- 1.1
  5.  *  设置配置文件:MIDP-2.0   
  6. */
  7. /*
  8.  * To change this template, choose Tools | Templates
  9.  * and open the template in the editor.
  10.  */
  11. package brave;
  12. import javax.microedition.midlet.*;
  13. import javax.microedition.lcdui.*;
  14. /**
  15.  * @author Administrator
  16.  */
  17. public class BraveMidlet extends MIDlet implements CommandListener {
  18.     
  19.     private Display d;
  20.     private Command exitCommand;
  21.     private Command startCommand;
  22.     private BraveCanvas braveCanvas;
  23.     
  24.     public BraveMidlet(){
  25.         d = Display.getDisplay(this);
  26.         exitCommand = new Command("退出",Command.EXIT,1);
  27.         startCommand = new Command("开始",Command.SCREEN,1);
  28.     }
  29.     
  30.     public void startApp() {
  31.        //创建BraveCanvas 
  32.  braveCanvas = new BraveCanvas(); 
  33.          braveCanvas.addCommand(exitCommand); 
  34.          braveCanvas.addCommand(startCommand); 
  35.          braveCanvas.setCommandListener(this); 
  36.          //装载BraveCanvas 
  37.          d.setCurrent(braveCanvas);         
  38.     }
  39.     public void pauseApp() {
  40.     }
  41.     public void destroyApp(boolean unconditional) {
  42.     }
  43.     public void commandAction(Command c, Displayable dpa) {
  44.         //throw new UnsupportedOperationException("Not supported yet.");
  45.         String str_co = c.getLabel(); 
  46.          
  47.         if(str_co.equals("开始"))
  48.          { 
  49.           //运行BraveCanvas中的线程(启动游戏) 
  50.             braveCanvas.startup(); 
  51.          } 
  52.          else if(str_co.equals("退出")) 
  53.          { 
  54.             destroyApp(false); 
  55.             notifyDestroyed(); 
  56.          } 
  57.     }
  58. }
  59. /*
  60.  * BraveCanvas.java
  61. */
  62. /*
  63.  * To change this template, choose Tools | Templates
  64.  * and open the template in the editor.
  65.  */
  66. package brave;
  67. import javax.microedition.lcdui.game.GameCanvas;
  68. import javax.microedition.lcdui.Graphics;
  69. import java.io.IOException;
  70. /**
  71.  *
  72.  * @author Administrator
  73.  */
  74. public class BraveCanvas extends GameCanvas implements Runnable {
  75.     
  76.     private boolean sign;
  77.     private Graphics g;
  78.     private int x,y;
  79.     
  80.     public BraveCanvas(){
  81.         super(true);
  82.         
  83.         sign = false;
  84.         g = null;
  85.         x = this.getWidth()/2;
  86.         y = this.getHeight()/2;        
  87.     }
  88.     public void startup(){
  89.         this.sign = true;
  90.         Thread thread = new Thread(this);
  91.         //启动线程 
  92.         thread.start(); 
  93.     }        
  94.     
  95.     public void run() {
  96.         //throw new UnsupportedOperationException("Not supported yet.");
  97.         g = this.getGraphics();
  98.         
  99.         while(sign){            
  100.             try{
  101.                     //@符号的移动 
  102.                     input(g); 
  103.                     //@符号的显示 
  104.                     paint(g); 
  105.                     //这里应该有详细的计算,方便为上,置为15 
  106.                     Thread.sleep(15); 
  107.             }
  108.             catch(Exception e){
  109.                 System.out.println("2:"+ e);     
  110.             }
  111.         }
  112.     }
  113.     
  114.     public void input(Graphics g)throws IOException{
  115.         int keystates = this.getKeyStates();
  116.         switch(keystates){
  117.             case UP_PRESSED:
  118.                 y = Math.max(0,y - 1);
  119.                 break;
  120.             case DOWN_PRESSED:
  121.                 y = Math.min(this.getHeight(),y + 1);
  122.                 break;
  123.             case LEFT_PRESSED:
  124.                 x = Math.max(0, x - 1);
  125.                 break;
  126.             case RIGHT_PRESSED: 
  127.                 x = Math.min(getWidth(), x + 1); 
  128.                 break;                 
  129.             case RIGHT_PRESSED |  UP_PRESSED:
  130.                 x = Math.min(getWidth(), x + 1);
  131.                 y = Math.max(0,y - 1);
  132.                 break;               
  133.              case RIGHT_PRESSED |  DOWN_PRESSED:
  134.                 x = Math.min(getWidth(), x + 1);
  135.                 y = Math.min(this.getHeight(),y + 1);
  136.                 break;   
  137.              case LEFT_PRESSED |  UP_PRESSED:
  138.                 x = Math.max(0, x - 1);
  139.                 y = Math.max(0,y - 1);
  140.                 break;               
  141.              case LEFT_PRESSED |  DOWN_PRESSED:
  142.                 x = Math.max(0, x - 1);
  143.                 y = Math.min(this.getHeight(),y + 1);
  144.                 break;      
  145.                 
  146.                 
  147.                
  148.         }
  149.     }
  150.     public void paint(Graphics g) {
  151.             //设置画布的背景色 
  152.             g.setColor(0x000000); 
  153.             //把背景色画满画布 
  154.             g.fillRect(0, 0, getWidth(), getHeight()); 
  155.             //设置画布的前景色 
  156.             g.setColor(0xffffff); 
  157.             //在画布上写上@ 
  158.             g.drawString("@", x, y, Graphics.TOP|Graphics.LEFT); 
  159.             //刷新画布 
  160.             flushGraphics(); 
  161.     }
  162. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值