我在這裏與球動畫的問題是,球正在直線移動留下的蹤跡。我預期的結果是,不應該有球的蹤跡。Java多線程球動畫
該代碼確定球沿着X軸的一個方向上的運動。
public class App extends JFrame implements Runnable{
int x=0,y=250;
public void run() {
for(;;) {
try {
repaint();
x++;
Thread.sleep(10);
} catch(Exception e){}
}
}
public void paint(Graphics g) {
g.drawOval(x,y,30,30);
}
public static void main(String[] args) {
App frame= new App();
frame.setTitle("Bounce");
frame.setSize(400, 450);
frame.setVisible(true);
Thread t1 = new Thread(frame);
t1.start();
}
}
[繪製一個矩形,不會在下一個繪畫中消失](http://stackoverflow.com/questions/12683533/drawing-a-rectangle-that-wont-disappear-in-next-paint)應該結束你的問題是什麼以及如何解決它。你需要使用'paintComponent(Graphics g)'並且調用'super.paintComponent(g)'來擺脫那個「蹤跡」。 – Obicere
哎。一般而言,這是非常糟糕的做法。整個GUI在EDT之外運行。 –