public class Cercle implements Runnable {
    static ThreadGroup tg = new ThreadGroup("Cercles");
    
    int x, y;  // position du centre
    int rayon; // le rayon du cercle
    static int width, height; // taille de la zone ou les cercle a le droit de se promener
    
    // Constructeurs
    public Cercle(int x, int y, int rayon, int width, int height) {
        this.x = x;
        this.y = y;
        this.rayon = rayon;
        this.width = width;
        this.height = height;
        
        new Thread(tg, this).start();
    }
    

    // Méthodes statiques
    static public void suspend() {
        tg.suspend();
    }

    static public void resume() {
        tg.resume();
    }

    // Méthodes publiques
    public int getX() {
        return x;
    }
 
     public int getY() {
        return y;
    }
 
     public int getRayon() {
        return rayon;
    }
 
    public void setX(int x) {
        this.x = x;
    }
    
    public void setY(int y) {
        this.y = y;
    }
    
    public void setRayon(int r) {
        rayon = r;
    }
    
    public void move(int x, int y) {
        this.x = x;
        this.y = y;
    }
    
    public String toString() {
        return((String) ("x = " + x + "\n" +
                         "y = " + y + "\n" +
                         "rayon = " + rayon + "\n" +
                         "width = " + width + "\n" +
                         "height = " + height + "\n"));
    }
    
    public void run() {
        int incX=1, incY=1;

        while(true) {

            int x = this.x, y = this.y;
            
            // On calcule la nouvelle position du cercle
            
            x += Math.rint(Math.random() * incX);
            y += Math.rint(Math.random() * incY);
        
            
            // Pour ne pas que le cercle sorte de l'ecran
            if ((incX == 1) && (x > (width - rayon)))
                incX = -1;
            
            if ((incX == -1) && ((x - rayon) < 0))
                incX = 1;
            
            if ((incY == 1) && (y > (height - rayon)))
                incY = -1;
            
            if ((incY == -1) && ((y - rayon) < 0))
                incY = 1;
  
            
            // On déplace le cercle
            move(x, y);
            
            // On "dort" entre 0 et 1/100 de seconde, aléatoirement
            try {
                Thread.currentThread().sleep((long) (10 * Math.random()));
            } catch(InterruptedException e) {
                System.out.println("Erreur dans le sleep(1000 * Math.random());");
                e.printStackTrace();
            }
        }
    }
}
    
