Bonjour,
Je débute en JAVA et un petit problème m'est apparu.
En fait l'animation fonctionne lorsque la méthode qui la gère est appelée dans le constructeur mais si je passe par un quelconque évènement, actionPerformed par exemple ; alors là l'animation fonctionne (les valeurs des variables changent) mais l'affichage à l'écran est effectué à la fin de celle-ci. En gros je ne vois que le résultat lorsque la l'animation est terminée, pendant les opérations l'affichage se bloque.
Le changement de panel est réalisé après la méthode go() et donc rien n'est visible sauf à la fin.
Fenetre.java
Ecran.java
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
76
77
78
79 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.*; public class Fenetre extends JFrame implements ActionListener { private JPanel menu ; private Ecran jeu ; private JPanel cards; private JButton btnCommencer; public Fenetre() { menu = new JPanel(); jeu = new Ecran(); cards = new JPanel(new CardLayout()); this.setTitle("Application"); this.setSize(720,540); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); //Panel menu btnCommencer = new JButton("Commencer"); menu.add(btnCommencer); btnCommencer.addActionListener(this); //CardLayout pour changement du panel cards.add(menu, "MENUPANEL"); cards.add(jeu, "JEUPANEL"); this.setContentPane(cards); this.setVisible(true); //pas de problèmes si exécuté dans le constructeur</gras> /*CardLayout cl = (CardLayout)(cards.getLayout()); cl.show(cards, "JEUPANEL"); go();*/ } public void actionPerformed(ActionEvent arg0) //problème : maj graphique à la fin { Object obj = arg0.getSource(); if (obj == btnCommencer) { CardLayout cl = (CardLayout)(cards.getLayout()); cl.show(cards, "JEUPANEL"); repaint(); go(); } } private void go() { for(int i = -50; i < jeu.getWidth() / 2; i++) { int x = jeu.getPosX(); int y = jeu.getPosY(); x++; y++; jeu.setPosX(x); jeu.setPosY(y); jeu.repaint(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } }
repaint() revalidate() n'arrangent en rien le problème.
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 import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel; public class Ecran extends JPanel { private int posX = 0; private int posY = 0; public void paintComponent(Graphics g) { g.setColor(Color.white); g.fillRect(0, 0, this.getWidth(), this.getHeight()); 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 aurait la solution, Merci d'avance.
Partager