深入分析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
  
  }
  
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值