📄 mygamewithinputcanvas.java
字号:
import javax.microedition.lcdui.* ;
import javax.microedition.lcdui.game.* ;
public class MyGameWithInputCanvas extends GameCanvas
implements Runnable
{
public MyGameWithInputCanvas()
{
super(true) ;
}
boolean conti = true ;
int rate = 50 ;
public void run()
{
long st = 0 ;
long et = 0 ;
Graphics g = getGraphics() ;
while(conti)
{
st = System.currentTimeMillis() ;
input() ;
render(g) ;
et = System.currentTimeMillis() ;
if((et-st)<rate)
{
try
{
Thread.sleep(rate-(et-st));
}catch(Exception exp){}
}
}
}
int x = 50 ;
int y = 10 ;
public void input()
{
int keystate = getKeyStates() ;
if((keystate & UP_PRESSED)!=0)
{
y = y - 2 ;
}else if((keystate & DOWN_PRESSED)!=0)
{
y = y + 2 ;
}
}
public void render(Graphics g)
{
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);
g.drawString("Test",x,y,0);
x++ ;
if(x > getWidth())
x = 0 ;
flushGraphics() ;
}
public void start()
{
Thread t = new Thread(this) ;
t.start();
}
public void exit()
{
conti = false ;
}
public void keyPressed(int keycode)
{
System.out.println("按键事件");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -