J2ME Learning Notes (vi)

Source: Internet
Author: User
Tags clear screen key
Note to handle low-level transactions or screen drawing, then we have to use canvas. Usually want to write mobile phone game also need canvas to

, which provides graphics processing functions for handling keyboards, pens, and games.
Because the canvas is inherited by displayable, it has a addcommadn () method, and can also be one and commadn other properties to fit the group

into advanced transaction processing. Screen has the same characteristics as canvas, but screen allows developers to customize the listener, but canvas does not

Allow.
Low-level transaction processing is generally divided into two kinds, one to this keyboard, one is the stylus.
First, let's take a look at the code that handles the keyboard
/**
* @author you Li
*
* CopyRight (C) 2005
*/
Import javax.microedition.midlet.*;
Import javax.microedition.lcdui.*;

public class Lleventmidlet extends MIDlet {

/**
*
*/
private display display;
Public Lleventmidlet () {
display = Display.getdisplay (this);
}

/* (non-Javadoc)
* @see Javax.microedition.midlet.midlet#startapp ()
*/
protected void startApp () throws Midletstatechangeexception {
TODO automatically generate method stubs
MyCanvas mc = new MyCanvas ();
Display.setcurrent (MC);
}

/* (non-Javadoc)
* @see Javax.microedition.midlet.midlet#pauseapp ()
*/
protected void Pauseapp () {
TODO automatically generate method stubs
}

/* (non-Javadoc)
* @see Javax.microedition.midlet.midlet#destroyapp (Boolean)
*/
protected void Destroyapp (Boolean arg0) throws Midletstatechangeexception {
TODO automatically generate method stubs
}
Class MyCanvas extends canvas{
String action = "";
public void Paint (Graphics g) {
Clear Screen
G.setcolor (255,255,255);
G.fillrect (0,0,getwidth (), getheight ());
G.setcolor (0,0,0);
Check to see if long button behavior is supported
if (hasrepeatevents ()) {
g.DrawString ("Repeat", 10,10,0);
}else{
g.DrawString ("Nonrepeat", 10,10,0);
}
g.DrawString (action,10,20,0);
}
protected void keypressed (int keycode) {
Action = "KP:" + (char) keycode;
Repaint ();
}
protected void keyreleased (int keycode) {
Action = "KR:" + (char) keycode;
Repaint ();
}
protected void keyrepeated (int keycode) {
Action = "Kre:" + (char) keycode;
Repaint ();
}
}

}
This program will respond to the keyboard behavior of the phone and then display the key values of the keyboard you clicked on. From this program, we can

To find that when the canvas subclass becomes the response screen, the keypressed () method is raised whenever the keyboard is clicked. When it is released, it will

Respond to the keyreleased () method. And the way to support the long key response is the keyrepeated () method (this method is not all machines

All supported, say, n 6110 I didn't have this response when I used it. So it has to be judged. and determine whether or not to support

The long key response method is hasrepeatevents ()). KEY_NUM0~~KEY_NUM9, and Key_star, Key_pound are all machines

Constants that are supported, and different corresponding values depending on the device. For the program to cross the platform (oh, this is Java

One of the biggest advantages OH), it is best to use only these regular keyboard responses when developing in a special situation. Of course, for the game design

Conveniently, canvas defines several game keyboard codes in MIDP: Up

, Down,left,right,fire,game_a,game_b,game_c,game_d. Getgameaction () and getKeyCode () are defined at the same time.

Two methods. To deal with related behavioral transformations.

Stylus control event Processing, because I do not have the relevant equipment also did not under the simulator, so there is no careful research, probably looked at a

Next a few more important things. The stylus hits the screen, raises the pointerpressed () method, and passes in the click Point on the screen where the x,y sits

Standard. Pointerreleased () responds when the stylus leaves the screen. and Pointerdragged () is the stylus moving on the screen

The method of dynamic-time response, at the same time will produce corresponding x,y coordinates. If we develop programs that require cross-platform support in response to stylus transactions

You must use Haspointerevents () to determine whether the device supports a stylus. Haspointermotionevents () will judge

Whether the stylus is supported to move on the screen produces the pointerdragged () method.

Of course, there are drawing processing and call handling ... I am afraid that I can not accept so many concepts. Oh, ready

Take a break. Take a look at the things you've seen a few days ago. You know the new ^_^

============================================

Finally, I would like to say, because this time to see my writing this note a lot of friends, but there is always no one to respond. Makes me feel depressed, I

Just hope to play a role in the hope that more brothers to learn together, improve together, but also to help younger brother a bit. Oh

Oh... I hope you can leave a message. ^_^



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.