2014-06-19 82 views
0

我想讓我的圈子移動,所以我可以繼續devolping我的小applet遊戲的學校,但由於某種原因它不動,我不明白爲什麼。圈子不移動Java小程序

public class StartingPonit extends Applet implements Runnable{ 

    int x = 0; 
    int y = 0; 
    int dx = 1; 
    int dy = 1; 
    int radis = 20; 
    private Image i; 
    private Graphics doubleG; 

    @Override 
    public void init() { 
     setSize(800,600); 

    } 
     @Override 
    public void start() { 
     Thread thread = new Thread(this); 
     thread.start(); // thread info; 
    } 
     @Override 
    public void run() { 
     x =+ dx; 
     y =+ dy; 
     //thread info 
     while (true){ 
     repaint(); 
     try { 
      Thread.sleep(17); 
     } catch (InterruptedException ex) { 
      Logger.getLogger(StartingPonit.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     } 
    } 
     @Override 
    public void stop() { 

    } 
    public void destory() { 

    } 
    @Override 
    public void update(Graphics g) { 
     if(i == null){ 
      i = createImage(this.getSize().width,getSize().height); //double bbuffering 
      doubleG = i.getGraphics(); 
     } 
     doubleG.setColor(getBackground()); 
     doubleG.fillRect(0,0, this.getSize().width,this.getSize().height); 

     doubleG.setColor(getForeground()); 
     paint(doubleG); 

     g.drawImage(i, 0, 0, this); 
    } 
    @Override 
     public void paint(Graphics g) { 
      g.setColor(Color.BLACK); 
      g.fillOval(x-radis, y-radis, radis*2, radis*2); //size of object 

    } 

} 
+2

我不知道你是什麼意圖是 - 但你移動一次循環,然後開始你的循環,所以它只會移動一次 –

+0

爲了讓循環移動,x和y需要改變。這不代表改變x和y的代碼應該位於代碼中稍微不同的位置嗎? – fvu

+1

1)爲什麼要編寫一個小程序?如果這是由於規格。由老師,請參考[爲什麼CS老師應該停止教Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/)。 2)爲什麼選擇AWT而不是Swing?看到我對[Swing extras over AWT]的回答(http://stackoverflow.com/a/6255978/418556)有很多很好的理由放棄使用AWT組件。 –

回答

0

你是不是在你的循環更新xy

//thread info 
while (true){ 
    repaint(); 
    x += dx; 
    y += dy; 
    //rest the same 
} 
+0

謝謝,好像我也有= +混了+ = – Rune

+0

哦,你是對的。我修復了我的答案以糾正它。對不起,只是複製和粘貼你的代碼,我沒有編譯它。 – dkatzel

0

您也mispelt破壞() - public void destory() - 應該是public void destroy()