2011-06-02 57 views
1

我想畫圈出現每一秒,我能夠這樣做,但我如何使舊形狀消失?在java中刪除一個形狀

public void paint(Graphics g) { 
    try { 
    while (true) { 
     Shape circle = new Ellipse2D.Double(500*Math.random(),500*Math.random(), 50.0f, 50.0f); 
     Graphics2D ga = (Graphics2D)g; 
     ga.draw(circle); 
     ga.setPaint(Color.green); 
     ga.fill(circle); 
     ga.setPaint(Color.red); 
     Thread.sleep(1000); 

    } 
} catch (InterruptedException e) { 
    e.printStackTrace(); 
} 

}

+1

1)爲了更快提供更好的幫助,請發佈[SSCCE](http://pscode.org/sscce.html)2)'paint(Graphics)'不要在這個千禧年編碼AWT。如果在Swing組件中自定義繪畫,則使用'paintComponent(Graphics)'3)'while(true)'不要在繪畫方法中啓動*無限循環*。 4)'Thread.sleep(1000);'不要在paint方法中等待*。 – 2011-06-03 02:32:19

回答

1

剛拿到的背景顏色,並用它來覆蓋舊的圓圈與背景顏色的圓圈。

+0

我只是說我用白色填充圈了一圈,謝謝 – 2011-06-03 00:08:45

+1

如果你的背景顏色不是白色,你可以使用'ga.getBackground()'來獲取它。 – trutheality 2011-06-03 00:10:44

0

嘗試呼叫super.paint(g);循環中的第一件事。

+0

它沒有做到這一點 – 2011-06-03 00:17:06

0

這裏最簡單的方法是簡單地將g.clearRect(WIDTH, HEIGHT);稱爲paint()方法的頂部。