Bonjour à tous !
J'ai un petit problème dans mon code et je galère à trouver une réponse claire (que ce soit sur le fofo ou google).

Donc c'est simple :
j'ai un jeu en JFrame qui contient un plateau en JPanel. Dans ce JPanel se trouve longueur*largeur cases. Le problème est tout bête : lorsque je clique sur mes cases, elles changent la valeur de la première case au lieu d'elle-même.

Bref je ne comprends pas pourquoi mon MouseListener pourtant défini dans l'objet case ne marche pas sur la case en question.

Exemple :




Et le code (case) :

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
80
81
82
83
84
85
86
87
88
89
 
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
 
import javax.imageio.ImageIO;
import javax.swing.JPanel;
 
enum Etat {LIBRE, NOIR, BLANC, PRIS};
 
class CasePlateau extends JPanel implements MouseListener {
 
	Etat etatCase = Etat.LIBRE;
	int x, y;
	String imageCaseNom = "libre.jpg";
 
	public CasePlateau(Etat pEtatCase, int pX, int pY){
		etatCase = pEtatCase;
		x = pX*Plateau.tailleCase ;
		y = pY*Plateau.tailleCase ;
		this.addMouseListener(this);
	}
 
	public Etat getEtatCase() {
		return etatCase;
	}
 
	public void setEtatCase(Etat pEtatCase) {
		etatCase = pEtatCase;
		switch (etatCase)
		{
			case LIBRE :
				imageCaseNom = "libre.jpg";
				break;
			case NOIR :
				imageCaseNom = "noir.jpg";
				break;
			case BLANC :
				imageCaseNom = "blanc.jpg";
				break;
			case PRIS :
				imageCaseNom = "pris.jpg";
				break;
		}
		repaint();
	}
 
	public void paintComponent(Graphics g)
	{
		try {
	        Image img = ImageIO.read(new File(imageCaseNom));
	        g.drawImage(img, x, y, Plateau.tailleCase, Plateau.tailleCase, this);
	      } catch (IOException e) {
	        e.printStackTrace();
	      }    
	}
 
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
 
	}
 
	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
 
	}
 
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
 
	}
 
	@Override
	public void mousePressed(MouseEvent e) {
		setEtatCase(Etat.BLANC);
	}
 
	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
 
	}
}
Et la classe appelante (plateau) :

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
import java.awt.Color;
import javax.swing.JPanel;
import javax.swing.OverlayLayout;
 
public class Plateau extends JPanel {
 
	CasePlateau CasesPlateau [][] ;
	public static int taillePlateau = 9;
	public static int tailleCase = 40;
	public Plateau() 
	{
		setBackground(Color.ORANGE); 
		setLayout( new OverlayLayout( this ));
 
		CasesPlateau = new CasePlateau [taillePlateau] [taillePlateau];
		for (int i = 0 ; i < taillePlateau ; i++)
		{
			for (int j = 0 ; j < taillePlateau ; j++)
			{
				CasesPlateau[i][j] = new CasePlateau(Etat.LIBRE, i, j);
				add(CasesPlateau[i][j]);
			}
		}
	}
 
	public void setCasesTableau(int x, int y, Etat etatCase)
	{	
		CasesPlateau[x][y].setEtatCase(etatCase);
	}
}
Merci d'avoir lu jusque là et de chercher à m'aider !
Désolé si une question du type a déjà été posée dernièrement, je ne l'ai pas trouvée.