?? midlet2.java
字號:
package prj23_1;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
//兩個圖片相向運動,碰撞之后彈回
public class MIDlet2 extends MIDlet {
private MyGameCanvas mgc = new MyGameCanvas();
private Display dis;
protected void startApp() throws MIDletStateChangeException {
dis = Display.getDisplay(this);
dis.setCurrent(mgc);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
class MyGameCanvas extends GameCanvas implements Runnable{
private Image img1;
private Image img2;
private Sprite sp1;
private Sprite sp2;
private Graphics gra;
private int x1 = 0,y1 = 0;
private int x2 = 200, y2 = 200;
private boolean RUN = true;
private boolean DIR = true;
public MyGameCanvas(){
super(true);
try{
img1 = Image.createImage("/img1.png");
img2 = Image.createImage("/img2.png");
sp1 = new Sprite(img1);
sp2 = new Sprite(img2);
gra = this.getGraphics();
}catch(Exception ex){
ex.printStackTrace();
}
sp1.setPosition(x1, y1);
sp2.setPosition(x2, y2);
new Thread(this).start();
}
public void run(){
while(RUN){
if(DIR){
sp1.move(1, 1); //右下移動
sp2.move(-1, -1); //左上移動
}
else{
sp2.move(1, 1);
sp1.move(-1, -1);
}
gra.setColor(255,255,255);
gra.fillRect(0,0,this.getWidth(),this.getHeight());
sp1.paint(gra);
sp2.paint(gra);
this.flushGraphics();
if(sp1.collidesWith(sp2, true)){//碰撞檢測
//RUN = false;
DIR = false;
}
try{
Thread.currentThread().sleep(10);
}catch(Exception ex){}
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -