Bonjour,

Je cherche à afficher 5 souris qui arrivent dans l’espace graphique tour à tour depuis son centre et se déplacent aléatoirement vers une des cases adjacentes (nord, est, sud, ouest), toutes les (int)(Math.random()*2000) millisecondes.
Chaque souris est un thread indépendant, démarré dans le programme de test. Elles ne peuvent pas se déplacer sur une case déjà occupée, ni sortir des limites de la fenêtre. Merci infiniment.

Voici le code à ajouter ou modifier si nécessaire :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
int low = 20; int high = 390;
 
int randomX = (int)(Math.random() * (high-low)) + low;
int randomY = (int)(Math.random() * (high-low)) + low; 
Shape souris = new Ellipse2D.Double(randomX, randomY,  10, 10 );
 
int randomX1 = (int)(Math.random() * (high-low)) + low;
int randomY1 = (int)(Math.random() * (high-low)) + low;    
Shape souris1 = new Ellipse2D.Double(randomX1, randomY1, 10, 10);
 
int randomX2 = (int)(Math.random() * (high-low)) + low;
int randomY2 = (int)(Math.random() * (high-low)) + low;    
Shape souris2 = new Ellipse2D.Double(randomX2, randomY2, 10, 10);
 
    //Shape square = new Rectangle2D.Double(100, 100, 100, 100);
 
    public void paint(Graphics g) {
 
        setOvals(5);
        g.setColor( Color.green );
        g.fillOval ( 10, 20, 10, 20 ); // x, y, width, height
        g.drawOval (10,20,10,20 ) ;
 
        //g.setColor( Color.green );
       // g.fillOval ( 10, 20, 10, 20 ); // x, y, width, height
        //g.drawOval (10,30,10,20 ) ;
 
        //ga.draw(souris1);
         g.setColor(Color.green);
        ((Graphics2D) g).fill(souris1); // add cast to g
 
        // ga.draw(souris2);
        //    ga.setPaint(Color.green);
        // ga.fill(souris2);
 
         g.setColor(Color.green);
         ((Graphics2D) g).fill(souris2);
 
           //Shape souris3 = null;
          //Graphics2D g = null;
          Graphics2D graphics2d = (Graphics2D) g; //add cast to
           Shape souris3 = null;
           // graphics2d.draw(souris3);
            graphics2d.setPaint(Color.green);
            graphics2d.fill(souris3);
        // ga.setPaint(Color.red);
         //ga.draw(square);
 
            g.setColor(Color.green);
            ((Graphics2D) g).fill(souris3);
    }
 
    private void setOvals(int i) {
    }
 
    public static void main(String args[]) {
        Frame frame = new Fenetre();
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });
        frame.setSize(400, 400);
        frame.setVisible(true);
    }
   }
     /*public int getOvals() {
        return ovals;
    }
 
    public void setOvals(int ovals) {
        this.ovals = ovals;
    }
}
*/