0
我想設置一個計時器類來控制球何時選擇一種新顏色。我需要在設定的時間改變球的顏色,而不是連續設置不同的顏色。這是我的球類,整個課程都是通過開課。設置一個計時器來控制何時執行隨機變量
public void go() {
if (dx >= 0) {
dx = 20;
}
}
public void update(Start sp) {
if (x + dx > sp.getWidth() - radius * 2) {
x = sp.getWidth() - radius * 2;
dx = -dx;
}
else if (x + dx < 0) {
dx = -dx;
}
else {
x += dx;
}
}
public void paint(Graphics g) {
Random set = new Random();
int num1;
num1 = set.nextInt(4);
if (num1 == 0) {
g.setColor(Color.blue);
g.fillOval(x, y, radius * 2, radius * 2);
}
if (num1 == 1) {
g.setColor(Color.green);
g.fillOval(x, y, radius * 2, radius * 2);
}
if (num1 == 2) {
g.setColor(Color.white);
g.fillOval(x, y, radius * 2, radius * 2);
}
if (num1 == 3) {
g.setColor(Color.magenta);
g.fillOval(x, y, radius * 2, radius * 2);
}
}
問題是什麼? – Compass 2014-10-02 16:59:53
我只需要有人解釋如何使用計時器類來使我的隨機變量在設定的時間段內執行。我是新來的代碼,我找不到任何其他資源來幫助我。 – Cameron 2014-10-02 17:23:44
請問[this](http://stackoverflow.com/questions/4044726/how-to-set-a-timer-in-java)有效嗎? – Compass 2014-10-02 17:28:26