📄 midp2eventgame_canvas.java
字号:
/*
*MIDP2.0游戏按键测试
*MIDP2EventGame_Canvas.java
*Editer:Jinlong He
*Date:2008-01-24
*/
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class MIDP2EventGame_Canvas extends GameCanvas{
private int width; //mobile screen width
private int height; //mobile screen height
String text = "MIDP2.0游戏按键测试";
/*
*1.构造
*
*/
MIDP2EventGame_Canvas(){
super(false); //suppressKeyEvents取值为true时不响应键盘事件,所以在此为false
width = this.getWidth();
height = this.getHeight();
if(this.hasPointerEvents()){
text ="特性:设备支持手写笔";
}else{
text ="特性:设备不支持手写笔";
}
if(this.hasPointerMotionEvents()){ //是否支持触控笔拖曳pointerDragged(),和上面的方法是2.0里有的
text +="、支持触控笔拖曳";
}else{
text +="、不支持触控笔拖曳";
}
if(this.hasRepeatEvents()){
text +="、支持持继按键";
}else{
text += "、不支持持继按键";
}
flushGraphics();
//repaint();
}
/*
*2.实现paint()方法
*
*/
public void paint(Graphics g){
g.setColor(0,0,255);
g.fillRect(0,0,width,height);
g.setColor(255,255,255);
g.drawString(text,0,height/2,Graphics.LEFT | Graphics.TOP); //写字符四个参数:字符串、始宽、始高、格式
}
/*
*3.响应按键事件
*keyReleased为keyPressed的相反事件,松开按钮时触发
*/
protected void keyPressed(int keyCode){
//↓↑←→↖↗↙↘
text="游戏按键测试";
int keyState = this.getKeyStates();
if ((UP_PRESSED & keyState) != 0){
text += "游戏上"; //"↑" or "2"
}
if ((DOWN_PRESSED & keyState) != 0){
text += "游戏下"; //"↓" or "8"
}
if ((LEFT_PRESSED & keyState) != 0){
text += "游戏左"; //"→" or "6"
}
if ((RIGHT_PRESSED & keyState) != 0){
text += "游戏右"; //"←" or "4"
}
if ((FIRE_PRESSED & keyState) != 0){
text += "游戏开火"; //"OK" or "5"
}
if ((GAME_A_PRESSED & keyState) != 0){
text += "游戏A↖"; //"1"
}
if ((GAME_B_PRESSED & keyState) != 0){
text += "游戏B↗"; //"3"
}
if ((GAME_C_PRESSED & keyState) != 0){
text += "游戏C↙"; //"7"
}
if ((GAME_D_PRESSED & keyState) != 0){
text += "游戏D↘"; //"9"
}
repaint();
}
/*
*4.响应按键事件
*keyReleased为keyPressed的相反事件,松开按钮时触发
*/
protected void keyReleased(int keyCode){
//↓↑←→↖↗↙↘
text = "松开";
int keyState = this.getKeyStates();
if ((UP_PRESSED & keyState) != 0){
text += "游戏上"; //"↑" or "2"
}
if ((DOWN_PRESSED & keyState) != 0){
text += "游戏下"; //"↓" or "8"
}
if ((LEFT_PRESSED & keyState) != 0){
text += "游戏左"; //"→" or "6"
}
if ((RIGHT_PRESSED & keyState) != 0){
text += "游戏右"; //"←" or "4"
}
if ((FIRE_PRESSED & keyState) != 0){
text += "游戏开火"; //"OK" or "5"
}
if ((GAME_A_PRESSED & keyState) != 0){
text += "游戏A↖"; //"1"
}
if ((GAME_B_PRESSED & keyState) != 0){
text += "游戏B↗"; //"3"
}
if ((GAME_C_PRESSED & keyState) != 0){
text += "游戏C↙"; //"7"
}
if ((GAME_D_PRESSED & keyState) != 0){
text += "游戏D↘"; //"9"
}
repaint();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -