- /*
- * 文件名: BraveMidlet.java
- * 开发工具: NetBeans 6.0.1
- * 设置配置: CLDL- 1.1
- * 设置配置文件:MIDP-2.0
- */
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package brave;
- import javax.microedition.midlet.*;
- import javax.microedition.lcdui.*;
- /**
- * @author Administrator
- */
- public class BraveMidlet extends MIDlet implements CommandListener {
- private Display d;
- private Command exitCommand;
- private Command startCommand;
- private BraveCanvas braveCanvas;
- public BraveMidlet(){
- d = Display.getDisplay(this);
- exitCommand = new Command("退出",Command.EXIT,1);
- startCommand = new Command("开始",Command.SCREEN,1);
- }
- public void startApp() {
- //创建BraveCanvas
- braveCanvas = new BraveCanvas();
- braveCanvas.addCommand(exitCommand);
- braveCanvas.addCommand(startCommand);
- braveCanvas.setCommandListener(this);
- //装载BraveCanvas
- d.setCurrent(braveCanvas);
- }
- public void pauseApp() {
- }
- public void destroyApp(boolean unconditional) {
- }
- public void commandAction(Command c, Displayable dpa) {
- //throw new UnsupportedOperationException("Not supported yet.");
- String str_co = c.getLabel();
- if(str_co.equals("开始"))
- {
- //运行BraveCanvas中的线程(启动游戏)
- braveCanvas.startup();
- }
- else if(str_co.equals("退出"))
- {
- destroyApp(false);
- notifyDestroyed();
- }
- }
- }
- /*
- * BraveCanvas.java
- */
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package brave;
- import javax.microedition.lcdui.game.GameCanvas;
- import javax.microedition.lcdui.Graphics;
- import java.io.IOException;
- /**
- *
- * @author Administrator
- */
- public class BraveCanvas extends GameCanvas implements Runnable {
- private boolean sign;
- private Graphics g;
- private int x,y;
- public BraveCanvas(){
- super(true);
- sign = false;
- g = null;
- x = this.getWidth()/2;
- y = this.getHeight()/2;
- }
- public void startup(){
- this.sign = true;
- Thread thread = new Thread(this);
- //启动线程
- thread.start();
- }
- public void run() {
- //throw new UnsupportedOperationException("Not supported yet.");
- g = this.getGraphics();
- while(sign){
- try{
- //@符号的移动
- input(g);
- //@符号的显示
- paint(g);
- //这里应该有详细的计算,方便为上,置为15
- Thread.sleep(15);
- }
- catch(Exception e){
- System.out.println("2:"+ e);
- }
- }
- }
- public void input(Graphics g)throws IOException{
- int keystates = this.getKeyStates();
- switch(keystates){
- case UP_PRESSED:
- y = Math.max(0,y - 1);
- break;
- case DOWN_PRESSED:
- y = Math.min(this.getHeight(),y + 1);
- break;
- case LEFT_PRESSED:
- x = Math.max(0, x - 1);
- break;
- case RIGHT_PRESSED:
- x = Math.min(getWidth(), x + 1);
- break;
- case RIGHT_PRESSED | UP_PRESSED:
- x = Math.min(getWidth(), x + 1);
- y = Math.max(0,y - 1);
- break;
- case RIGHT_PRESSED | DOWN_PRESSED:
- x = Math.min(getWidth(), x + 1);
- y = Math.min(this.getHeight(),y + 1);
- break;
- case LEFT_PRESSED | UP_PRESSED:
- x = Math.max(0, x - 1);
- y = Math.max(0,y - 1);
- break;
- case LEFT_PRESSED | DOWN_PRESSED:
- x = Math.max(0, x - 1);
- y = Math.min(this.getHeight(),y + 1);
- break;
- }
- }
- public void paint(Graphics g) {
- //设置画布的背景色
- g.setColor(0x000000);
- //把背景色画满画布
- g.fillRect(0, 0, getWidth(), getHeight());
- //设置画布的前景色
- g.setColor(0xffffff);
- //在画布上写上@
- g.drawString("@", x, y, Graphics.TOP|Graphics.LEFT);
- //刷新画布
- flushGraphics();
- }
- }
画一个"@"字符随着键盘的左右而移动
最新推荐文章于 2024-10-08 16:16:54 发布