import java.applet.Applet;
import java.awt.*;
import javax.swing.*;import java.awt.Graphics;
import java.util.*;public class Ball extends Applet implements Runnable{
//private int i=0;
public void init(){
setBackground(Color.white);
}
   public void paint(Graphics g){
   for(int i=0;i<=10;i++){

int x = (int)(Math.random()*100);
int y = (int)(Math.random()*100);
int c = (int)(Math.random()*100);
Random random = new Random();
int R  = random.nextInt(256);
int G = random.nextInt(256);
int B = random.nextInt(256);
            

       g.setColor(new Color(R,G,B));
       g.fillOval(x, y, c, c);

   }

   }   

public void start(){
Thread t = new Thread(this);
t.start();
}

public void run(){
while(true){

try{  


Thread.sleep(2000);
 
}catch(Exception e){
e.printStackTrace();
}

repaint();


}
}

}

解决方案 »

  1.   


    import java.applet.Applet;
    import java.awt.*;
    import javax.swing.*;import java.awt.Graphics;
    import java.util.*;public class Ball extends Applet implements Runnable {
    // private int i=0;
    public void init() {
    setBackground(Color.white);
    } public void paint(Graphics g) {
    for (int i = 0; i <= 10; i++) {
    try {
    Thread.currentThread().sleep(100);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    int x = (int) (Math.random() * 100);
    int y = (int) (Math.random() * 100);
    int c = (int) (Math.random() * 100);
    Random random = new Random();
    int R = random.nextInt(256);
    int G = random.nextInt(256);
    int B = random.nextInt(256); g.setColor(new Color(R, G, B));
    g.fillOval(x, y, c, c); } } public void start() {
    Thread t = new Thread(this);
    t.start();
    } public void run() {
    while (true) { try { Thread.sleep(2000); } catch (Exception e) {
    e.printStackTrace();
    } repaint(); }
    }}
      

  2.   

    加上這句 Thread.currentThread().sleep(100);