?? gamedemo.java
字號:
//gameDemo.java file
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.game.*;
import java.util.*;
public class gameDemo extends MIDlet implements CommandListener
{
private Command exitCommand;
private DemoCanvas canvas;
private DemoTask task ;
private Timer timer;
public gameDemo()
{
exitCommand =new Command("Exit",Command.EXIT,1);
canvas =new DemoCanvas();
canvas.addCommand(exitCommand);
canvas.setCommandListener(this);
timer = new Timer();
task = new DemoTask(canvas);
}
protected void startApp( ) throws MIDletStateChangeException
{
Display.getDisplay(this).setCurrent(canvas);
timer.schedule(task, 0,100);
}
protected void pauseApp( )
{
timer.cancel();
}
protected void destroyApp( boolean p1 )
{
}
public void commandAction(Command c,Displayable d)
{
if (c ==exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
}
public class DemoCanvas extends GameCanvas
{
private Graphics g;
private int[ ] clrs={0X00000000,0x00FF0000,0x0000FF00,0x000000FF};
private int curColor=0x00000000;
private int[ ] posXY={0,0, 20,0, 40,0, 0,20, 20,20, 40,20, 0,40, 20,40, 40,40};
private int curX=0,curY=0;
public DemoCanvas()
{
super(true);
g = getGraphics();
}
public void Action()
{
g.setColor(0x00FFFFFF);
g.fillRect(0,0,60,60);
int x,y;
int keyState = getKeyStates();
//判斷ABCD鍵是否被按下
if((keyState & GAME_A_PRESSED) !=0)
curColor=clrs[0];
else if((keyState & GAME_B_PRESSED) !=0)
curColor=clrs[1];
else if((keyState & GAME_C_PRESSED) !=0)
curColor=clrs[2];
else if((keyState & GAME_D_PRESSED) !=0)
curColor=clrs[3];
//判斷上下左右鍵是否被按下
if((keyState & UP_PRESSED) !=0)
{
curX = posXY[2];
curY = posXY[3];
}
else if((keyState & DOWN_PRESSED) !=0)
{
curX = posXY[14];
curY = posXY[15];
}
else if((keyState & LEFT_PRESSED) !=0)
{
curX = posXY[6];
curY = posXY[7];
}
else if((keyState & RIGHT_PRESSED) !=0)
{
curX = posXY[10];
curY = posXY[11];
}
else
{
curX = posXY[8];
curY = posXY[9];
}
g.setColor(curColor);
g.fillRect(curX,curY,20,20);
flushGraphics();
}
}
public class DemoTask extends TimerTask
{
private DemoCanvas canvas;
public DemoTask(DemoCanvas can)
{
canvas = can;
}
public void run()
{
canvas.Action();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -