je suis entraint de programmer jeu d'echec en java j'ai utilisé la methode bitboard pour la programmation des regle d'echec mais qand j'ai passé au coté graphique j'ai recontré des problèmes:
- qd je clic sur l'icon d'une pièce il y a beaucoup d'exception qui ce genère :
Exception in thread "AWT-EventQueue-0"
java.lang.NullPointerException at interfaceGr.Echiquier$FigurePiece.mousePressed(
Echiquier.java:78 )at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
voici le code source de la class responsable au action graphique :
package interfaceGr;
import gestionReg.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.net.URL;
import java.util.HashMap;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
public class Echiquier extends JLayeredPane {
public static final int TAILLE_CASE =55;
public static final int MAX = (TAILLE_CASE * 7);
private static final int largeurLabelLigne = 15;
private static final int hauteurLabelLigne = TAILLE_CASE;
private static final int largeurLabelColonne = TAILLE_CASE;
private static final int hauteurLabelColonne = 15;
public HashMap icons = new HashMap();
private JLabel[] labelLigne = new JLabel[16];
private JLabel[] labelColonne = new JLabel[16];
public JLabel[][] arrierePlan = new JLabel[8][8];
public JLayeredPane carreau = new JLayeredPane();
private EchiquierVirtuel EchVir =null;
private FenetrePr Fen;
class FigurePiece extends JLabel implements MouseListener, MouseMotionListener {
int ligne, colonne;
int mousePressedX, mousePressedY;
boolean estPieceBlanche;
int type;
private EchiquierVirtuel EchVir =null;
private FenetrePr Fen;
private Echiquier Ech;
//constructeur
public FigurePiece(int _type,Echiquier Ech) {
super((ImageIcon) Ech.icons.get(new Integer(_type)));
type = _type;
estPieceBlanche = type >0;
setSize(getIcon().getIconWidth(), getIcon().getIconHeight());
addMouseMotionListener(this);
addMouseListener(this);
}
public int getligne() {
return 7 - ((getLocation().y + (Echiquier.TAILLE_CASE / 2)) / Echiquier.TAILLE_CASE);
}
public int getcolonne() {
return ((getLocation().x + (Echiquier.TAILLE_CASE / 2)) / Echiquier.TAILLE_CASE);
}
public void setField(int nouveauLigne, int nouveauColonne) {
setLocation(nouveauColonne * Echiquier.TAILLE_CASE, (7 - nouveauLigne) * Echiquier.TAILLE_CASE);
}
//methodes pour MouseListener et MouseMotionListener
public void mousePressed(MouseEvent e) {
//System.out.println("sdsfjfksdjsnjd");
ligne = getligne();
//System.out.println(ligne);
colonne = getcolonne();
//System.out.println(colonne);
mousePressedX = e.getX();
mousePressedY = e.getY();
//System.out.println(mousePressedX);
//System.out.println(mousePressedY);
synchronized (EchVir) {
if (EchVir.champs[(ligne << 3) + colonne] == EchiquierVirtuel.CV) {
Echiquier.this.mettreAJour();
return;
}
for (int l = 0; l < 8; l++)
for (int c = 0; c < 8; c++)
if (EchVir.estCoupPossible(new Coup(ligne, colonne, l, c)))
Ech.arrierePlan[7 - l][c].setBackground(new Color(0,0,0,0));
}
}
public void mouseDragged(MouseEvent e) {
int nouveauX, nouveauY;
nouveauX = getLocation().x + e.getX() - mousePressedX;
if (nouveauX > Echiquier.MAX)
nouveauX = Echiquier.MAX;
if (nouveauX < 0)
nouveauX = 0;
nouveauY = getLocation().y + e.getY() - mousePressedY;
if (nouveauY > Echiquier.MAX)
nouveauY = Echiquier.MAX;
if (nouveauY < 0)
nouveauY = 0;
setLocation(nouveauX, nouveauY);
Ech.carreau.setLayer(this, 10, 0);
}
public void mouseReleased(MouseEvent e) {
synchronized (EchVir) {
int nouveauLigne = getligne();
int nouveauColonne = getcolonne();
Ech.carreau.setLayer(this, 2, 0);
if (ligne != nouveauLigne || colonne != nouveauColonne) {
final Coup coup = new Coup(ligne, colonne, nouveauLigne, nouveauColonne);
if (EchVir.estCoupPossible(coup)) {
if ((EchVir.champs[coup.getSource()] == EchiquierVirtuel.PB && coup.getDestinationLigne() == 7)
|| (EchVir.champs[coup.getSource()] == EchiquierVirtuel.PN && coup.getDestinationLigne() == 0)) {
coup.setPionPromu(Ech.selectionnerPromotion());
}
synchronized (Fen) {
//Fen.faireCoup(coup);
}
} else
Ech.mettreAJour();
} else
Ech.mettreAJour();
}
}
public void mouseMoved(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
}
/**
* fenetre pour selectionner la promotion
* @return entier représentant la piece selectionné
*/
public byte selectionnerPromotion() {
Object[] option = new Object[4];
if (EchVir.getAuBlancDeJouer()) {
option[0] = (ImageIcon) icons.get(new Integer(EchiquierVirtuel.RB));
option[1] = (ImageIcon) icons.get(new Integer(EchiquierVirtuel.TB));
option[2] = (ImageIcon) icons.get(new Integer(EchiquierVirtuel.FB));
option[3] = (ImageIcon) icons.get(new Integer(EchiquierVirtuel.CB));
} else {
option[0] = (ImageIcon) icons.get(new Integer(EchiquierVirtuel.RN));
option[1] = (ImageIcon) icons.get(new Integer(EchiquierVirtuel.TN));
option[2] = (ImageIcon) icons.get(new Integer(EchiquierVirtuel.FN));
option[3] = (ImageIcon) icons.get(new Integer(EchiquierVirtuel.CN));
}
int choix =
JOptionPane.showOptionDialog(
null,
"Promu le pion par:",
"",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
option,
option[0]);
if (choix == 0)
return EchiquierVirtuel.REINE;
else if (choix == 1)
return EchiquierVirtuel.TOUR;
else if (choix == 2)
return EchiquierVirtuel.FOU;
else if (choix == 3)
return EchiquierVirtuel.CAVALIER;
else
return EchiquierVirtuel.RIEN;
}
//constructeur
public Echiquier(FenetrePr fen) {
Fen = fen;
initializer();
}
/**
* créer l'arriére plan
*/
public void restaurerArrierePlan() {
ImageIcon img = new ImageIcon(Echiquier.class.getResource("/images/caseBlanche.jpg"));
ImageIcon img1 = new ImageIcon(Echiquier.class.getResource("/images/caseNoire.jpg"));
for (int ligne = 0; ligne < 8; ligne++)
for (int colonne = 0; colonne < 8; colonne++)
if ((ligne + colonne & 1) == 1)
{
arrierePlan[ligne][colonne].setIcon(img);
}
else
arrierePlan[ligne][colonne].setIcon(img1);
}
private void chargerIcon(int type, String nomFichier) {
String nomCompletFichier = "/images/" + nomFichier;
URL url = Echiquier.class.getResource(nomCompletFichier);
ImageIcon icon = url != null ? new ImageIcon((URL) url) : new ImageIcon("null");
if (icon == null) {
System.out.println("Impossible de trouver le fichier: " + nomFichier + "\n");
System.exit(-1);
}
icons.put(new Integer(type), icon);
}
private void initializer() {
chargerIcon(EchiquierVirtuel.KB, "WK.png");
chargerIcon(EchiquierVirtuel.RB, "WQ.png");
chargerIcon(EchiquierVirtuel.TB, "WR.png");
chargerIcon(EchiquierVirtuel.FB, "WB.png");
chargerIcon(EchiquierVirtuel.CB, "WN.png");
chargerIcon(EchiquierVirtuel.PB, "WP.png");
chargerIcon(EchiquierVirtuel.KN, "BK.png");
chargerIcon(EchiquierVirtuel.RN, "BQ.png");
chargerIcon(EchiquierVirtuel.TN, "BR.png");
chargerIcon(EchiquierVirtuel.FN, "BB.png");
chargerIcon(EchiquierVirtuel.CN, "BN.png");
chargerIcon(EchiquierVirtuel.PN, "BP.png");
setLayout(null);
setBackground(new Color(255,255,255,255));
setOpaque(true);
add(carreau);
carreau.setBounds(largeurLabelLigne, hauteurLabelColonne, 8 * TAILLE_CASE, 8 * TAILLE_CASE);
// Create field labels
for (int n = 0; n < 8; n++) {
labelLigne[n] = new JLabel("" + (8 - n), SwingConstants.CENTER);
labelLigne[n].setFont(new Font("SansSerif", Font.ITALIC, 12));
add(labelLigne[n]);
labelLigne[n].setBounds(0, hauteurLabelColonne + n * hauteurLabelLigne, largeurLabelLigne, hauteurLabelLigne);
labelLigne[n].setForeground(new Color(0,0,0,255));
labelLigne[n + 8] = new JLabel("" + (8 - n), SwingConstants.CENTER);
labelLigne[n+8].setFont(new Font("SansSerif", Font.ITALIC, 12));
add(labelLigne[n + 8]);
labelLigne[n
+ 8].setBounds(
largeurLabelLigne + 8 * TAILLE_CASE,
hauteurLabelColonne + n * hauteurLabelLigne,
largeurLabelLigne,
hauteurLabelLigne);
labelLigne[n + 8].setForeground(new Color(0, 0,0,255));
labelColonne[n] = new JLabel("" + (char) (((int) 'a') + n), SwingConstants.CENTER);
labelColonne[n].setFont(new Font("SansSerif", Font.ITALIC, 12));
add(labelColonne[n]);
labelColonne[n].setBounds(
largeurLabelLigne + n * largeurLabelColonne,
8 * TAILLE_CASE + hauteurLabelColonne,
largeurLabelColonne,
hauteurLabelColonne);
labelColonne[n].setForeground(new Color(0, 0,0,255));
labelColonne[n + 8] = new JLabel("" + (char) (((int) 'a') + n), SwingConstants.CENTER);
labelColonne[n+8].setFont(new Font("SansSerif", Font.ITALIC, 12));
add(labelColonne[n + 8]);
labelColonne[n + 8].setBounds(largeurLabelLigne + n * largeurLabelColonne, 0, largeurLabelColonne, hauteurLabelColonne);
labelColonne[n + 8].setForeground(new Color(0, 0,0,255));
}
// Create the background:
for (int x = 0; x < 8; x++)
for (int y = 0; y < 8; y++) {
JLabel labelArrierePlan = new JLabel();
labelArrierePlan.setOpaque(true);
labelArrierePlan.setBounds(x * TAILLE_CASE, y * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE);
carreau.add(labelArrierePlan, new Integer(1), 0);
arrierePlan[y][x] = labelArrierePlan;
}
mettreAJour();
Dimension taillePanneau = null;
taillePanneau = new Dimension(8 * TAILLE_CASE + 2 * largeurLabelLigne, 8 * TAILLE_CASE + 2 * hauteurLabelColonne);
setPreferredSize(taillePanneau);
setMinimumSize(taillePanneau);
}
public void mettreAJour() {
synchronized (this) {
EchVir= new EchiquierVirtuel();
redessnier();
}
}
public void redessnier() {
FigurePiece figure;
restaurerArrierePlan();
synchronized (EchVir) {
for (int ligne = 0; ligne < 8; ligne++) {
for (int colonne = 0; colonne < 8; colonne++) {
int type = EchVir.champs[(ligne << 3) + colonne];
if (type != EchiquierVirtuel.CV) {
figure = new FigurePiece(type,this);
carreau.add(figure, new Integer(2), 0);
figure.setField(ligne, colonne);
}
}}}
}
public void mettreAJourLabel() {
for (int n = 0; n < 8; n++) {
labelLigne[n].setText("" + (8 - n));
labelLigne[n + 8].setText("" + (8 - n));
labelColonne[n].setText("" + (char) (((int) 'a') + n));
labelColonne[n + 8].setText("" + (char) (((int) 'a') + n));
}
}
public void afficherCoup(Coup coup, int durée) {
arrierePlan[7 - coup.getSourceLigne()][coup.getSourceColonne()].setBackground(new Color(0,0,0,0));
arrierePlan[7 - coup.getDestinationLigne()][coup.getDestinationColonne()].setBackground(new Color(0,0,0,0));
}
}
je vous en pris si vous avez des idéés de me les dire c vraiment urgent et merci
Afficher la suite