Bonjour,
Je suis un cours de Java sur un site Internet. Dans une partie du cours, on doit animer une boule rouge en appelant la méthode repaint. Cependant l'animation ne se déroule pas et la méthode repaint ne semble pas fonctionner
voir le code ci-dessous
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 import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel; public class Panneau extends JPanel { private int posX = -50; private int posY = -50; public void paintComponent(Graphics g){ g.setColor(Color.red); g.fillOval(posX, posY, 50, 50); } public int getPosX() { return posX; } public void setPosX(int posX) { this.posX = posX; } public int getPosY() { return posY; } public void setPosY(int posY) { this.posY = posY; } }si quelqu'un a une explication
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 import java.awt.Dimension; import javax.swing.JFrame; public class Fenetre extends JFrame{ private Panneau pan = new Panneau(); public Fenetre(){ this.setTitle("Animation"); this.setSize(300, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setContentPane(pan); this.setVisible(true); go(); } private void go(){ for(int i = -50; i < pan.getWidth(); i++){ int x = pan.getPosX(), y = pan.getPosY(); x++; y++; pan.setPosX(x); pan.setPosY(y); pan.repaint(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } }
merci
Partager