当前位置: 代码迷 >> Java相关 >> 照着书上打的 为什么还是报错 ?
  详细解决方案

照着书上打的 为什么还是报错 ?

热度:363   发布时间:2011-05-07 08:45:22.0
照着书上打的 为什么还是报错 ?
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);            
        }
   
}

红色字就是有报错的地方
搜索更多相关的解决方案: private  

----------------解决方案--------------------------------------------------------
呵呵,有些包你可能没有啊,对于java出问题解决最好的办法是将出错信息复制百度上搜一下,大多数解决方案都会搜到的。
----------------解决方案--------------------------------------------------------
super(true);
里面不能是boolean型只能是""字符串吧

----------------解决方案--------------------------------------------------------
  相关解决方案