照着书上打的 为什么还是报错 ?
import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class GameMain extends MIDlet {
private Display display;
private myGame game;
public GameMain() {
display = Display.getDisplay(this);
game = new myGame();
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
display.setCurrent(game);
}
}
class myGame extends Canvas implements Runnable
{
private Image img1,img2;
private Sprite sp1,sp2;
private Graphics gra;
private int x1 = 0, y1 = 0;
private int x2 = 150, y2 = 150;
private boolean RUN = true , DIR = true;
public myGame(){
super(true);
try{
img1 = Image.createImage("/img1.png");
img2 = Image.createImage("/img2.png");
sp1 = new Sprite(img1);
sp2 = new Sprite(img2);
}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.sleep(10);
}catch(Exception ex){}
}
}
protected void paint(Graphics arg0) {
gra = arg0.getGraphics(this);
}
}
红色字就是有报错的地方