Probleme rafraichissement animation avec Swing
Bonjour,
Je débute sous java et j'ai un souci.
J'ai créé une fenetre animée: a l intérieur une balle rouge bouge et rebondit sans cesse sur les bords (exercice du tres bon cours sur le site des zeros)
J'ai ensuite rajouté un bouton qui est censé déclencher le mouvement de la balle. Problème lorsque je clique sur le bouton il reste enfoncé et la balle ne bouge pas. j'aimerais comprendre ou est le problème. Je crois comprendre qu'il s agit d'un nouveau thread qui est prioritaire et qui bloque l'animation. Pourtant l'instruction pour redessiner est bien localisée dans la méthode action performed.
Bref si quelqu'un peut m éclairer merci d'avance :)
voici le code:
FENETRE.JAVA:
Code:
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Fenetre extends JFrame implements ActionListener{
JPanel cont=new JPanel();
Panneau monPan=new Panneau();
public Fenetre(){
this.setSize(500,200);
this.setContentPane(cont);
cont.setLayout(new BorderLayout());
cont.add(monPan);
//cont.add(monsousPan);
JButton monbout=new JButton();
this.add(monbout, BorderLayout.SOUTH);
monbout.addActionListener(this);
this.setVisible(true);
System.out.println("bonjour");
System.out.println("Le nom du thread principal111 est " + Thread.currentThread().getName());
//go();
}
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
//monbout.setPreferredSize(new Dimension(10,10));
//Fenetre mafen=new Fenetre();
int i=1;
int j=1;
boolean booli=true;
boolean boolj=true;
int alei = (int)(Math.random() * 5);
int alej = (int)(Math.random() * 5);
//while (true){
for (int l=1;l<200;l++){
//System.out.println("test");
monPan.setPosx(i);
monPan.setPosy(j);
monPan.repaint(i);
monPan.repaint(j);
System.out.println("Le nom du thread principal est " + Thread.currentThread().getName());
if (i>=this.getWidth()-60){
booli=false;
alei=(int)(Math.random() * 2);
}
if (i==0){
alei=(int)(Math.random() * 2);
booli=true;
}
if (j>=this.getHeight()-60){
boolj=false;
alej=(int)(Math.random() * 2);
}
if (j==0){
alej=(int)(Math.random() * 2);
boolj=true;
}
if (booli){
i=i+alei;
}
else
{i--;
}
if (boolj){
j=j+alej;
}
else
{j--;
}
try{
Thread.sleep(3);//Ici une pause d'une seconde
}catch(InterruptedException f) {
f.printStackTrace();
}
}
}
} |
PANNEAU.JAVA
Code:
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
| import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Panneau extends JPanel{
private static int Posx = 0;
private static int Posy = 0;
public void paintComponent(Graphics g){
g.setColor(Color.white);
g.fillRect(0, 0, 1000, 1000);
g.setColor(Color.red);
g.fillOval(Posx,Posy,50,50);
}
public int getPosx(){
return Posx;
}
public int getPosy(){
return Posy;
}
public void setPosx(int Posx){
this.Posx=Posx;
}
public void setPosy(int Posy){
this.Posy=Posy;
}
} |