深入分析J2ME平台MIDP低级事件处理机制

通常低级事件的处理是为了满足游戏开发的需要,因为游戏开发相关的问题非常复杂。因此不在这里进行阐述,有兴趣的朋友可以和我索要相关的游戏开发资料。MIDP定义了如下键值在Canvas类内:
  
  KEY_NUM0
  KEY_NUM1
  KEY_NUM2
  KEY_NUM3
  KEY_NUM4
  KEY_NUM5
  KEY_NUM6
  KEY_NUM7
  KEY_NUM8
  KEY_NUM9
  KEY_STAR
  KEY_POUND
  这些都是按键基本值,当然有些手机是触摸屏的。事件的传递是通过如下方法来实现的。
  
  protected void keyPressed(int keyCode)
  protected void keyReleased(int keyCode)
  protected void keyRepeated(int keyCode)
  protected void pointerPressed(int x, int y)
  protected void pointerDragged(int x, int y)
  protected void pointerReleased(int x, int y)
  protected void showNotify()
  protected void hideNotify()
  protected abstract void paint(Graphics g)
  commandAction() method of the CommandListener interface
  开发者应该继承Canvas类的同时覆盖相应的方法来实现低级事件的处理,低级事件的处理是连续的、串行的,在前面的事件返回之前后面的事件不会执行,当然也有例外,你可以参考API doc得到答案。下面是一个例子,它的功能就是在屏幕上显示出你按下键的键值,界面非常简单因此没有抓图。代码如下所示:
  
  //LowLevelMIDlet.java
  
  import javax.microedition.lcdui.Display;
  import javax.microedition.midlet.MIDlet;
  import javax.microedition.midlet.MIDletStateChangeException;
  
  /*
  * Created on 2004-6-24
  *
  * TODO To change the template for this generated file go to
  * Window - Preferences - JAVA- Code Style - Code Templates
  */
  
  /**
  * @author P2800
  *
  * TODO To change the template for this generated type comment go to Window -
  * Preferences - JAVA- Code Style - Code Templates
  */
  public class LowLevelMIDlet extends MIDlet
  {
  
  private Display display;
  private KeyCanvas keyCanvas;
  
  /*
  * (non-Javadoc)
  *
  * @see javax.microedition.midlet.MIDlet#startApp()
  */
  protected void startApp() throws MIDletStateChangeException
  {
  // TODO Auto-generated method stub
  display = Display.getDisplay(this);
  keyCanvas = new KeyCanvas();
  display.setCurrent(keyCanvas);
  
  }
  
  /*
  * (non-Javadoc)
  *
  * @see javax.microedition.midlet.MIDlet#pauseApp()
  */
  protected void pauseApp()
  {
  // TODO Auto-generated method stub
  
  }
  
  /*
  * (non-Javadoc)
  *
  * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
  */
  protected void destroyApp(boolean arg0) throws MIDletStateChangeException
  {
  // TODO Auto-generated method stub
  
  }
  
  }
  
  //KeyCanvas.java
  
  import javax.microedition.lcdui.Canvas;
  import javax.microedition.lcdui.Command;
  import javax.microedition.lcdui.CommandListener;
  import javax.microedition.lcdui.Displayable;
  import javax.microedition.lcdui.Graphics;
  
  /*
  * Created on 2004-6-25
  *
  * TODO To change the template for this generated file go to
  * Window - Preferences - JAVA- Code Style - Code Templates
  */
  
  /**
  * @author P2800
  *
  * TODO To change the template for this generated type comment go to Window -
  * Preferences - JAVA- Code Style - Code Templates
  */
  public class KeyCanvas extends Canvas implements CommandListener
  {
  private String key = "Press any key";
  
  /*
  * (non-Javadoc)
  *
  * @see javax.microedition.lcdui.Canvas#paint(javax.microedition.lcdui.Graphics)
  */
  protected void paint(Graphics g)
  {
  // TODO Auto-generated method stub
  g.setColor(255, 255, 255);
  g.fillRect(0, 0, getWidth(), getHeight());
  g.setColor(0, 0, 0);
  g.drawString(key, getWidth() / 2, getHeight() / 2, Graphics.TOP
  | Graphics.HCENTER);
  
  }
  
  public void keyPressed(int keyCode)
  {
  key = keyCode + " is pressed";
  repaint();
  }
  
  public void keyReleased(int keyCode)
  {
  key = keyCode + " is released";
  repaint();
  }
  
  /*
  * (non-Javadoc)
  *
  * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command,
  *   javax.microedition.lcdui.Displayable)
  */
  public void commandAction(Command arg0, Displayable arg1)
  {
  // TODO Auto-generated method stub
  
  }
  
  }

你可能感兴趣的:(游戏,Go)