JPanel MousePressed et Freeze

Résolu
cs_xboubix Messages postés 3 Date d'inscription jeudi 9 février 2006 Statut Membre Dernière intervention 6 mai 2006 - 6 mai 2006 à 14:11
cs_xboubix Messages postés 3 Date d'inscription jeudi 9 février 2006 Statut Membre Dernière intervention 6 mai 2006 - 6 mai 2006 à 15:03
Bonjour,
j'essaye de faire un petit programme tout simple, dans lequel un Jpanel possède une image de fond, et un gif transparent se balade dedans, lorsque je clique sur la souris (n'importe ou pour l'instant) j'aimerai qu'un second sprite s'ajoute à l'écran. Le sprite s'ajoute effectivement MAIS aprés avoir RE-cliquer pour "défreezer" l'animation qui ne se raffraichie pas mais continue à tourner en arrière plan!... Il doit y avoir un problème au niveau du Thread, mais je vois pas du tout lequel.

POuvez vous me dire d'ou ce freeze provient? Merci!
Les sources et .class :
http://perso.club-internet.fr/galays/BidouBounce.zip

le code du JPanel:

import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;


public class BouncePanel extends JPanel implements Runnable {

Image bidou, fond, bisou;
float current = 0F;
Thread runner;
int xPosition = 10;
int xMove = 5;
int yPosition = 1;
int bidouHeight = 158;
int bidouWidth = 110;
int height;
ArrayList BisousTab = new ArrayList();


public BouncePanel() {
super();
Toolkit kit = Toolkit.getDefaultToolkit();
bidou = kit.getImage("bidou.gif");
fond = kit.getImage("fond.jpg" );
bisou = kit.getImage("bisou.gif");
this.addMouseListener(new AnimMouseAction() );
runner = new Thread( this );
runner.start();
}


public void paintComponent(Graphics comp) {
Graphics2D comp2D = (Graphics2D) comp;
height = getSize().height - bidouHeight-20;
if (yPosition == -1)
yPosition = height - 20;
if ((fond != null) && (bidou != null) ) {
comp2D.drawImage(fond,0,0,this);
Iterator it = BisousTab.iterator ( ) ;
while ( it . hasNext ( ) ) {
comp2D.drawImage(bisou,(int) ( (( Bisou ) (it.next())).x), (int) ( (( Bisou ) (it.next())).y), this);
}
comp2D.drawImage(bidou,(int) xPosition, (int) yPosition, this);



}
}


public void run() {


Thread thisThread = Thread.currentThread();
while (runner == thisThread) {
current += (float) 0.1;
if ( current > 3)
current = (float) 0;
xPosition += xMove;
if (xPosition > (getSize().width - bidouWidth))
xMove *= -1;
if (xPosition < 1)
xMove *= -1;
double bounce = Math.sin(current) * height;
yPosition = (int) (height - bounce);
repaint();
try {
Thread.sleep(30);
} catch ( InterruptedException e) {}
}
}

class AnimMouseAction extends MouseAdapter {




public void mousePressed(MouseEvent e) { //if ( runner !null) runner null;
BisousTab.add( new Bisou ( xPosition, yPosition));



}


}


class Bisou {

public int x;
public int y;

public Bisou(int x, int y) {
this.x = x;
this.y = y;

}
}


}


PS: Oui, c'est un programme niais

2 réponses

scaryman Messages postés 492 Date d'inscription vendredi 30 janvier 2004 Statut Membre Dernière intervention 16 mai 2007 12
6 mai 2006 à 14:34
Salut
Change dans ton run :
while ( it . hasNext ( ) ) {
Object o = it.next();
comp2D.drawImage(bisou,(int) ( (( Bisou ) o).x), (int) ( (( Bisou ) o).y), this);
}

et aussi je crois que ce serait mieux de mettre un mouseReleased au lieu d'un mousePressed

Voila
A++
3
cs_xboubix Messages postés 3 Date d'inscription jeudi 9 février 2006 Statut Membre Dernière intervention 6 mai 2006
6 mai 2006 à 15:03
Merci, c'est parfait :-)
0
Rejoignez-nous