113 lines
4.0 KiB
Java
113 lines
4.0 KiB
Java
import javax.swing.*;
|
|
import java.awt.*;
|
|
|
|
public class FenetreJeu extends JFrame {
|
|
|
|
private Grille grille;
|
|
private JButton[][] boutons;
|
|
private JLabel labelCompteur;
|
|
private JButton btnActionBas; // NOUVEAU : On garde le bouton en mémoire
|
|
|
|
public FenetreJeu(Grille g) {
|
|
super("Démineur - Partie en cours");
|
|
this.grille = g;
|
|
this.boutons = new JButton[g.getLignes()][g.getColonnes()];
|
|
|
|
this.setSize(600, 600);
|
|
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
this.setLayout(new BorderLayout());
|
|
|
|
this.labelCompteur = new JLabel("Mines restantes : " + g.getMinesRestantes());
|
|
this.labelCompteur.setHorizontalAlignment(SwingConstants.CENTER);
|
|
this.labelCompteur.setFont(new Font("Arial", Font.BOLD, 16));
|
|
this.add(this.labelCompteur, BorderLayout.NORTH);
|
|
|
|
JPanel panneauGrille = new JPanel();
|
|
panneauGrille.setLayout(new GridLayout(g.getLignes(), g.getColonnes()));
|
|
|
|
ControleurGrille controleurSouris = new ControleurGrille(this, this.grille);
|
|
|
|
for (int i = 0; i < g.getLignes(); i++) {
|
|
for (int j = 0; j < g.getColonnes(); j++) {
|
|
JButton boutonCase = new JButton("");
|
|
this.boutons[i][j] = boutonCase;
|
|
panneauGrille.add(boutonCase);
|
|
boutonCase.addMouseListener(controleurSouris);
|
|
}
|
|
}
|
|
this.add(panneauGrille, BorderLayout.CENTER);
|
|
|
|
// --- MODIFICATION DU BOUTON DU BAS ---
|
|
this.btnActionBas = new JButton("Sauver et Quitter");
|
|
this.add(this.btnActionBas, BorderLayout.SOUTH);
|
|
ControleurJeu controleurQuitter = new ControleurJeu(this);
|
|
this.btnActionBas.addActionListener(controleurQuitter);
|
|
}
|
|
|
|
// --- NOUVEAUX OUTILS ---
|
|
public JButton getBouton(int l, int c) { return this.boutons[l][c]; }
|
|
public Grille getGrille() { return this.grille; } // Permet au contrôleur de lire la grille
|
|
|
|
public void changerBoutonEnRetourMenu() {
|
|
this.btnActionBas.setText("Retour au Menu");
|
|
}
|
|
|
|
public void mettreAJourCompteur() {
|
|
this.labelCompteur.setText("Mines restantes : " + this.grille.getMinesRestantes());
|
|
}
|
|
|
|
public void rafraichirCase(int l, int c) {
|
|
Case caseLogique = this.grille.getCase(l, c);
|
|
JButton btn = this.boutons[l][c];
|
|
|
|
if (caseLogique.isRevelee()) {
|
|
btn.setEnabled(false);
|
|
if (caseLogique.isMinee()) {
|
|
btn.setText("M");
|
|
btn.setBackground(Color.RED);
|
|
} else if (caseLogique.getNbMinesVoisines() > 0) {
|
|
btn.setText("" + caseLogique.getNbMinesVoisines());
|
|
} else {
|
|
btn.setText("");
|
|
}
|
|
} else {
|
|
if (caseLogique.getMarqueur() == 1) {
|
|
btn.setText("★");
|
|
} else if (caseLogique.getMarqueur() == 2) {
|
|
btn.setText("?");
|
|
} else {
|
|
btn.setText("");
|
|
}
|
|
}
|
|
}
|
|
|
|
public void rafraichirTouteLaGrille() {
|
|
for (int i = 0; i < grille.getLignes(); i++) {
|
|
for (int j = 0; j < grille.getColonnes(); j++) {
|
|
rafraichirCase(i, j);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void afficherDefaite(int lExplose, int cExplose) {
|
|
for (int i = 0; i < grille.getLignes(); i++) {
|
|
for (int j = 0; j < grille.getColonnes(); j++) {
|
|
Case c = grille.getCase(i, j);
|
|
JButton btn = boutons[i][j];
|
|
btn.setEnabled(false);
|
|
|
|
if (i == lExplose && j == cExplose) {
|
|
btn.setText("BOUM");
|
|
btn.setBackground(Color.RED);
|
|
} else if (c.isMinee() && c.getMarqueur() != 1) {
|
|
btn.setText("M");
|
|
btn.setBackground(Color.ORANGE);
|
|
} else if (!c.isMinee() && c.getMarqueur() == 1) {
|
|
btn.setText("X");
|
|
btn.setBackground(Color.YELLOW);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|