📄 keypressedcanvas.java
字号:
/**源代码由程序员联合开发网(www.pudn.com)会员"周润发"收集、整理、重新编辑
*Email: ql_chuanzhang@tom.com
*QQ号:1103798882
*欢迎大家与我联系互相交流学习
**/
package KeyPressed_package;
import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class KeyPressedCanvas extends Canvas implements Runnable{
private boolean RUN = true;
private int[] RUN_STATES = {0,1,0,0};
int xx=0,yy=0,PIC_N=10,move_x=this.getWidth()/2,move_y=this.getHeight()/2,CN=5;
private Image[] img = new Image[PIC_N];
public KeyPressedCanvas(){
super();
try {
img[1] = Image.createImage("/pic/p_down.gif");
img[2] = Image.createImage("/pic/xyj_png8x256_map001.png");
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
new Thread(this).start();
}
public void run() {
// TODO Auto-generated method stub
while(RUN){
try {
this.repaint();
Thread.currentThread();
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void keyPressed(int keyCode){
System.out.println(keyCode+"被按下");
switch (getGameAction(keyCode))
{
case UP:
move_y-=CN;yy+=CN;
switch(RUN_STATES[0])
{
case 0:
try {
img[1] = Image.createImage("/pic/p_up.gif");
RUN_STATES[0] = 1;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
case 1:
try {
img[1] = Image.createImage("/pic/p_up2.gif");
RUN_STATES[0]=2;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
case 2:
try {
img[1] = Image.createImage("/pic/p_up.gif");
RUN_STATES[0]=3;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
case 3:
try {
img[1] = Image.createImage("/pic/p_up3.gif");
RUN_STATES[0]=0;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
default:
RUN_STATES[0]=0;
break;
}
break;
case DOWN:
move_y+=CN;yy-=CN;
switch(RUN_STATES[1])
{
case 0:
try {
img[1] = Image.createImage("/pic/p_down.gif");
RUN_STATES[1] = 1;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
case 1:
try {
img[1] = Image.createImage("/pic/p_down2.gif");
RUN_STATES[1]=2;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
case 2:
try {
img[1] = Image.createImage("/pic/p_down.gif");
RUN_STATES[1]=3;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
case 3:
try {
img[1] = Image.createImage("/pic/p_down3.gif");
RUN_STATES[1]=0;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
default:
RUN_STATES[1]=0;
break;
}
break;
case RIGHT:
move_x+=CN;xx-=CN;
switch(RUN_STATES[2])
{
case 0:
try {
img[1] = Image.createImage("/pic/p_R.gif");
RUN_STATES[2] = 1;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
case 1:
try {
img[1] = Image.createImage("/pic/p_R2.gif");
RUN_STATES[2]=2;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
case 2:
try {
img[1] = Image.createImage("/pic/p_R.gif");
RUN_STATES[2]=3;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
case 3:
try {
img[1] = Image.createImage("/pic/p_R3.gif");
RUN_STATES[2]=0;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
default:
RUN_STATES[2]=0;
break;
}
break;
case LEFT:
move_x-=CN;xx+=CN;
switch(RUN_STATES[3])
{
case 0:
try {
img[1] = Image.createImage("/pic/p_L.gif");
RUN_STATES[3] = 1;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
case 1:
try {
img[1] = Image.createImage("/pic/p_L2.gif");
RUN_STATES[3]=2;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
case 2:
try {
img[1] = Image.createImage("/pic/p_L.gif");
RUN_STATES[3]=3;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
case 3:
try {
img[1] = Image.createImage("/pic/p_L3.gif");
RUN_STATES[3]=0;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
default:
RUN_STATES[3]=0;
break;
}
break;
}
}
public void keyRepeated(int keyCode){
System.out.println(keyCode+"被按下");
switch (getGameAction(keyCode))
{
case UP:
move_y-=CN;yy+=CN;
switch(RUN_STATES[0])
{
case 0:
try {
img[1] = Image.createImage("/pic/p_up.gif");
RUN_STATES[0] = 1;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
case 1:
try {
img[1] = Image.createImage("/pic/p_up2.gif");
RUN_STATES[0]=2;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
case 2:
try {
img[1] = Image.createImage("/pic/p_up.gif");
RUN_STATES[0]=3;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
case 3:
try {
img[1] = Image.createImage("/pic/p_up3.gif");
RUN_STATES[0]=0;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
default:
RUN_STATES[0]=0;
break;
}
break;
case DOWN:
move_y+=CN;yy-=CN;
switch(RUN_STATES[1])
{
case 0:
try {
img[1] = Image.createImage("/pic/p_down.gif");
RUN_STATES[1] = 1;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
case 1:
try {
img[1] = Image.createImage("/pic/p_down2.gif");
RUN_STATES[1]=2;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
case 2:
try {
img[1] = Image.createImage("/pic/p_down.gif");
RUN_STATES[1]=3;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
case 3:
try {
img[1] = Image.createImage("/pic/p_down3.gif");
RUN_STATES[1]=0;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
default:
RUN_STATES[1]=0;
break;
}
break;
case RIGHT:
move_x+=CN;xx-=CN;
switch(RUN_STATES[2])
{
case 0:
try {
img[1] = Image.createImage("/pic/p_R.gif");
RUN_STATES[2] = 1;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
case 1:
try {
img[1] = Image.createImage("/pic/p_R2.gif");
RUN_STATES[2]=2;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
case 2:
try {
img[1] = Image.createImage("/pic/p_R.gif");
RUN_STATES[2]=3;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
case 3:
try {
img[1] = Image.createImage("/pic/p_R3.gif");
RUN_STATES[2]=0;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
default:
RUN_STATES[2]=0;
break;
}
break;
case LEFT:
move_x-=CN;xx+=CN;
switch(RUN_STATES[3])
{
case 0:
try {
img[1] = Image.createImage("/pic/p_L.gif");
RUN_STATES[3] = 1;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
case 1:
try {
img[1] = Image.createImage("/pic/p_L2.gif");
RUN_STATES[3]=2;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
case 2:
try {
img[1] = Image.createImage("/pic/p_L.gif");
RUN_STATES[3]=3;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
case 3:
try {
img[1] = Image.createImage("/pic/p_L3.gif");
RUN_STATES[3]=0;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
break;
default:
RUN_STATES[3]=0;
break;
}
break;
}
}
protected void paint(Graphics gra) {
// TODO Auto-generated method stub
gra.setColor(255, 255, 255);
gra.fillRect(0, 0, this.getWidth(), this.getHeight());
//gra.drawImage(img[2], xx, yy, Graphics.TOP|Graphics.LEFT);
//gra.drawImage(img[2], 0, 0, Graphics.TOP|Graphics.LEFT);
//gra.drawImage(img[1], xx+move_x, yy+move_y, Graphics.TOP|Graphics.LEFT);
gra.drawImage(img[1], move_x, move_y, Graphics.TOP|Graphics.LEFT);
gra.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE));
gra.setColor(0,0,255);
gra.drawString(" x=" + move_x + " y=" + move_y , this.getWidth()/16, this.getHeight()/16, Graphics.LEFT | Graphics.TOP);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -