?? repeateventtest.java
字號:
import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import java.util.*;public class RepeatEventTest extends MIDlet implements CommandListener, ItemStateListener{ Display display; MyCanvas myCanvas; Form form; ChoiceGroup setRepeat; Gauge timeInterval; Command exitCmd; Command okCmd; Command backCmd; Timer repeatTimer; RepeatTask task; boolean simRepeat, hasRepeat; public RepeatEventTest(){ display = Display.getDisplay(this); exitCmd = new Command("退出", Command.EXIT, 1); okCmd = new Command("確定", Command.OK, 1); backCmd = new Command("返回", Command.BACK, 1); setRepeat = new ChoiceGroup("模擬repeat事件?",Choice.EXCLUSIVE); setRepeat.append("否", null); setRepeat.append("是", null); timeInterval = new Gauge("時間間隔:200ms", true, 10, 0); myCanvas = new MyCanvas(this); repeatTimer = new Timer(); task = new RepeatTask(); form = new Form("Repeat事件選擇"); form.append(setRepeat); form.append(timeInterval); form.addCommand(exitCmd); form.addCommand(okCmd); form.setCommandListener(this); form.setItemStateListener(this); } public void startApp(){ display.setCurrent(form); } public void pauseApp(){ } public void destroyApp(boolean unconditional){ } public void commandAction(Command c, Displayable d){ if(c == exitCmd){ destroyApp(true); notifyDestroyed(); } else if(c == okCmd){ if(setRepeat.getSelectedIndex() == 0) simRepeat = false; else simRepeat = true; display.setCurrent(myCanvas); } else if(c == backCmd){ display.setCurrent(form); } } public void itemStateChanged(Item item){ if(item == timeInterval){ int value = timeInterval.getValue(); timeInterval.setLabel("時間間隔:"+(200+100*value)+"ms"); } } class MyCanvas extends Canvas{ int gameAction; int x, y; public MyCanvas(RepeatEventTest parent){ x = getWidth()/2; y = getHeight()/2; addCommand(backCmd); setCommandListener(parent); } public void paint(Graphics g){ g.setColor(0xFFFFFF); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(0); g.fillRect(x, y, 20, 20); } public void showNotify(){ repeatTimer.schedule(task, 0); } public void hideNotify(){ repeatTimer.cancel(); } public void keyPressed(int keyCode){ if(simRepeat){ gameAction = getGameAction(keyCode); hasRepeat = true; } else{ gameAction = getGameAction(keyCode); performGameAction(); } } public void keyReleased(int keyCode){ if(simRepeat){ hasRepeat = false; } } public void keyRepeated(int keyCode){ if(hasRepeatEvents()){ keyPressed(keyCode); } } void performGameAction(){ switch(gameAction){ case Canvas.UP: if(y < getHeight()) y-=2; break; case Canvas.DOWN: if(y > 0) y+=2; break; case Canvas.LEFT: if(x > 0) x-=2; break; case Canvas.RIGHT: if(y < getWidth()) x+=2; break; case Canvas.FIRE: if(y < getWidth()) x+=2; break; } repaint(); } } class RepeatTask extends TimerTask{ public void run(){ while(true){ while(hasRepeat){ int interval = 200+100*timeInterval.getValue(); try{ myCanvas.performGameAction(); Thread.sleep(interval); } catch(Exception ex){} } } } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -