⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 planegameactionexample.java

📁 Java ME手机应用开发大全一书的配套光盘上的源码
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class PlaneGameActionExample extends MIDlet
{
  private Display  display;       
  private MyCanvas canvas;   
  public PlaneGameActionExample()
  {
	//获取MIDlet的Display对象实例
    display = Display.getDisplay(this);
	//声明Canvas屏幕对象
    canvas  = new MyCanvas (this);
  }
  protected void startApp()
  {
	//显示屏幕对象
    display.setCurrent(canvas);
  }
  protected void pauseApp()
  { 
  }
  protected void destroyApp( boolean unconditional )
  { 
  }
  public void exitMIDlet()
  {
    destroyApp(true);
    notifyDestroyed();
  }
}
class MyCanvas extends Canvas implements CommandListener
{
  private Command exit;
  //声明图片对象
  private Image plane;
  private PlaneGameActionExample gameActionExample;
  private int x, y;
  public MyCanvas (PlaneGameActionExample gameActionExample)
  {
	//设置飞机图片显示的起始位置
    x = getWidth()/2;
    y = getHeight()/2;
	try
	{
		 //获得图像文件
		 plane = Image.createImage("/plane.png");    
	}
	catch (Exception e)
	{
	}
   
    this.gameActionExample = gameActionExample;
    exit = new Command("Exit", Command.EXIT, 1);
    addCommand(exit);
    setCommandListener(this);
  } 
  protected void paint(Graphics graphics)
  {
	 //填充屏幕
    graphics.setColor(255,255,255);
    graphics.fillRect(0, 0, getWidth(), getHeight());
    graphics.setColor(255, 0, 0);
	//绘制飞机图像
	graphics.drawImage(plane,x,y,graphics.HCENTER|graphics.VCENTER);
  }
  public void commandAction(Command command, Displayable displayable)
  {
    if (command == exit)
    {
      gameActionExample.exitMIDlet();
     }
  }
  //响应游戏动作
  protected void keyPressed(int key)
  {
	//根据按键代码匹配对应的游戏动作,对应不同的动作,改变飞机的位置
    switch ( getGameAction(key) ){
     case Canvas.UP:
       
       y=y-10;
       break;
     case Canvas.DOWN:
       
       y=y+10;
       break;
     case Canvas.LEFT:
       
       x=x-10;
       break;
     case Canvas.RIGHT:
      
       x=x+10;
       break;
     case Canvas.FIRE:
       
       break;
    }
    repaint();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -