饶荣庆 -- 您今天UCWEB了吗?--http://www.ucweb.com

3G 手机开发网

   :: 首页 :: 联系 :: 聚合  :: 管理
  99 Posts :: 1 Stories :: 219 Comments :: 0 Trackbacks
原文:http://javaeye.5d6d.com/thread-90-1-1.html

深入分析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
  
  }
  
  }





爬虫工作室 -- 专业的手机软件开发工作室
3G视线 -- 专注手机软件开发
posted on 2007-08-26 23:32 3G工作室 阅读(653) 评论(0)  编辑  收藏 所属分类: j2me

Feedback

# re: 深入分析J2ME平台MIDP低级事件处理机制 2007-08-27 16:35 姜利阳
Good  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: