In-depth analysis of J2ME platform MIDP low-level event processing mechanism

zhaozj2021-02-16  68

In the previous article, we told how to implement the CommandListener and the ItemStateListener interface to complete the advanced event processing. But the low-level events from the keyboard input cannot be done by Command. You must inherit the Canvas class and override the method to complete the processing of low-level events.

Usually the processing of low-level events is to meet the needs of game development, because the problem related to game development is very complicated. So don't explain here, interested friends can develop information with me to be related. MIDP defines the following key value in the Canvas class

Key_Num2 Key_num1 key_num2 key_num3 key_num4 key_num5 key_num6 key_num7 key_num8 key_num9 key_star key_pound These are all basic values, of course, some mobile phones are touch screens. The transmission of an event is achieved by the following method.

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 developer should inherit the Canvas class while covering the corresponding methods to achieve the processing low-level events, handling low-level events It is continuous, serial, and after the previous event returns, the incident will not be executed, of course, there is also an exception, you can refer to the API DOC to get the answer. Here is an example, its function is to display the key value you press down on the screen, and the interface is very simple and therefore no graphic. The code is as follows: //lowlevelmidlet.javaimport 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, DISPLAY;

/ * * (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.javaimport 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 stubg. 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 "ispessed"; 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

}

}

转载请注明原文地址:https://www.9cbs.com/read-20138.html

New Post(0)