Pouriez-vous me dire commant faire bouger un objet (cercle ou rectangle) en mode aleatoir?
Pouriez-vous me dire commant faire bouger un objet (cercle ou rectangle) en mode aleatoir?
Tiens, tu peux adapter ca à ton objet :
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 import javax.swing.JWindow; /* * Created on 21 janv. 2006 * */ /** * @author Julien<br> * Project : Wizz<br> * Package : <br> * File : Wizz.java<br> * Creation : 21 janv. 2006<br> * Last modified : 21 janv. 2006<br> * Comment :Wizz like MSN...<br> **/ public class Wizz implements Runnable { private JWindow f; private Thread t; public Wizz(JWindow f) { super(); this.f = f; } public void start() { if (t == null) { t = new Thread(this); } t.start(); } /* * overrided method */ public void run() { int x = f.getX(); int y = f.getY(); for (int i = 0; i < 40; i++) { if (i % 2 == 0) f.setLocation(f.getX() + 5, f.getY() - 5); else f.setLocation(f.getX() - 5, f.getY() + 5); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } f.setLocation(x, y); } }
Mèrci, pour avoir repondu à mon problèm, mais je n'arrive pas à le faire marcher le programme avec mon objet graphique(cercle ou rectangle...). Peut-tu l'implanter l' objet toi même, pour que je vois comment tu fais? Et explique un peut ce que c'est un JWindow? Mèrci encore!
En fait c'était juste un exemple. Mais j'avais oublié que toi tu manipules des objets graphiques (cercles,...)... Pour ton problème, il faut donc que tu te bases sur la gestion d'un thread ainsi que sur la méthode paint.. Tu mets le tout dans un JPanel (ou autre) et ca te donne un truc comme ca (pas testé)
A toi de voir ce que tu peux faire avec ca...
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 public class Test implements Runnable { int x_pos = 10; int y_pos = 100; int radius = 20; public void start (){ Thread th = new Thread (this); th.start (); } public void run (){ Thread.currentThread().setPriority(Thread.MIN_PRIORITY); while (true){ x_pos ++; repaint(); try{ Thread.sleep (20); } catch (InterruptedException ex){ // do nothing } Thread.currentThread().setPriority(Thread.MAX_PRIORITY); } } public void paint (Graphics g) { g.setColor (Color.red); g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius); } }
+++
Ju
Partager