réorganisation du code : setVisible fonctionnel

This commit is contained in:
Haïssous Kayyissa 2022-05-01 13:41:44 +02:00
parent c169ba0623
commit 17c66e4b58
5 changed files with 61 additions and 42 deletions

View File

@ -2,42 +2,54 @@ import javax.swing.*;
import java.awt.*; import java.awt.*;
public class Case extends JPanel { public class Case extends JPanel {
private JPanel panel = new JPanel(); private Dimension caseSize;
private int entourage; private int entourage;
private boolean visible; private boolean visible;
private boolean minee; private boolean minee;
private Listener listener; private Listener listener;
public Case(Dimension caseSize, boolean visible, boolean minee, int entourage) { public Case(Dimension caseSize, boolean minee, int entourage) {
super(); super();
this.entourage=entourage; this.entourage=entourage;
this.visible=visible; this.visible=false;
this.minee=minee; this.minee=minee;
this.panel.setSize(caseSize); this.caseSize= caseSize;
this.setSize(caseSize);
this.listener = new Listener(); this.listener = new Listener();
panel.addMouseListener(listener); this.addMouseListener(this.listener);
GridLayout unique = new GridLayout(1,1); GridLayout unique = new GridLayout(1,1);
this.panel.setLayout(unique); this.setLayout(unique);
}
public void setVisible(){
this.visible=true;
this.updateUI();
}
public boolean getVisible(){
return this.visible;
}
public Case getCase(){
if (this.visible == false) { if (this.visible == false) {
Color gray2 = new Color(70, 70, 70); this.setBackground(new Color(70, 70, 70));
this.panel.setBackground(gray2);
} else if (this.visible == true) { } else if (this.visible == true) {
Color gray1 = new Color(80, 80, 80);
this.panel.setBackground(gray1);
if (this.minee == true) { if (this.minee == true) {
Color rose = new Color(236, 0, 140); this.setBackground(new Color(236, 0, 140));
this.panel.setBackground(rose); } else {
} else if (this.entourage>0){ this.setBackground(new Color(80, 80, 80));
this.panel.add(new Chiffre(entourage,caseSize)); if (this.entourage>0){
this.add(new Chiffre(entourage,caseSize));
}
} }
} }
return this;
} }
public JPanel getCase() { public boolean getMine(){
return this.panel; return this.minee;
} }
} }

View File

@ -42,7 +42,7 @@ public class FrameJeu{
miseEnPage.weightx =1; miseEnPage.weightx =1;
miseEnPage.weighty = 0.85; miseEnPage.weighty = 0.85;
miseEnPage.fill = GridBagConstraints.BOTH; miseEnPage.fill = GridBagConstraints.BOTH;
fenetre.add(grille.getGrille(), miseEnPage); fenetre.add(grille, miseEnPage);
// Mise en place de la fenêtre et affichage // Mise en place de la fenêtre et affichage
fenetre.pack(); fenetre.pack();

View File

@ -4,7 +4,7 @@ import java.util.Random;
public class Grille extends JPanel { public class Grille extends JPanel {
private Dimension grilleSize=new Dimension(0,0); private Dimension grilleSize=new Dimension(0,0);
private JPanel grille= new JPanel(); private Case[] plateau;
// On crée La grille de jeu // On crée La grille de jeu
public Grille(int lignes, int colonnes, int mines){ public Grille(int lignes, int colonnes, int mines){
@ -16,8 +16,9 @@ public class Grille extends JPanel {
GridLayout damier = new GridLayout(lignes,colonnes); GridLayout damier = new GridLayout(lignes,colonnes);
System.out.println("Taille des cases : "+caseSize); System.out.println("Taille des cases : "+caseSize);
System.out.println("Taille de la Grille : "+grilleSize); System.out.println("Taille de la Grille : "+grilleSize);
this.grille.setLayout(damier); this.setLayout(damier);
this.grille.setSize(grilleSize); this.setSize(grilleSize);
this.plateau= new Case[lignes*colonnes];
// Initialisation : les cases sont cachées // Initialisation : les cases sont cachées
@ -42,6 +43,7 @@ public class Grille extends JPanel {
} }
} }
} }
// On place maintenant les mines sur la plateau // On place maintenant les mines sur la plateau
boolean[] minee = new boolean[lignes*colonnes]; boolean[] minee = new boolean[lignes*colonnes];
for (int i=0; i<lignes*colonnes;i++){ for (int i=0; i<lignes*colonnes;i++){
@ -264,14 +266,14 @@ public class Grille extends JPanel {
// On place les cases à leur état actuel dans la grille // On place les cases à leur état actuel dans la grille
AffichePlateau(lignes*colonnes, caseSize, entourage, visible, minee); AfficherPlateau(lignes*colonnes, caseSize, entourage, minee);
} }
// Méthode pour afficher l'état des cases // Méthode pour Afficher le plateau
protected void AffichePlateau(int taille, Dimension caseSize, int[] entourage, boolean[] visible, boolean[] minee){ protected void AfficherPlateau(int taille, Dimension caseSize, int[] entourage, boolean[] minee){
for (int i=0;i<taille;i++){ for (int i=0;i<taille;i++){
Case panel = new Case(caseSize,visible[i],minee[i],entourage[i]); this.plateau[i]= new Case(caseSize,minee[i],entourage[i]);
this.grille.add(panel.getCase()); this.add(this.plateau[i].getCase());
} }
} }
@ -279,9 +281,4 @@ public class Grille extends JPanel {
public Dimension getGrilleSize() { public Dimension getGrilleSize() {
return grilleSize; return grilleSize;
} }
// Methode pour obtenir le panel pour l'affichage de la grille
public JPanel getGrille() {
return grille;
}
} }

View File

@ -6,21 +6,31 @@ public class Listener implements MouseListener{
@Override @Override
public void mouseClicked(MouseEvent evenement){ public void mouseClicked(MouseEvent evenement){
System.out.println("mouseClicked"); Case panel = (Case)evenement.getSource();
JPanel lb = (JPanel)evenement.getSource(); if ((evenement.getButton() == MouseEvent.BUTTON1)&&(panel.getVisible()==false)){
lb.setBackground(new Color(255, 255, 255)); panel.setVisible();
panel.getCase();
if (panel.getMine()==true){
System.out.println("Perdu!");
}
}
if (evenement.getButton() == MouseEvent.BUTTON3) {
panel.add(new Etoile(panel.getSize()), BorderLayout.CENTER);
panel.updateUI();
System.out.println("Marquage");
}
} }
public void mouseEntered(MouseEvent evenement){ public void mouseEntered(MouseEvent evenement){
System.out.println("mouseEntered"); // System.out.println("mouseEntered");
} }
public void mouseExited(MouseEvent evenement){ public void mouseExited(MouseEvent evenement){
System.out.println("mouseExited"); // System.out.println("mouseExited");
} }
public void mousePressed(MouseEvent evenement){ public void mousePressed(MouseEvent evenement){
System.out.println("mousePressed"); // System.out.println("mousePressed");
} }
public void mouseReleased(MouseEvent evenement){ public void mouseReleased(MouseEvent evenement){
System.out.println("mouseReleased"); // System.out.println("mouseReleased");
} }
} }

View File

@ -1,6 +1,6 @@
public class Test { public class Test {
public static void main(String[] args) { public static void main(String[] args) {
Grille grille=new Grille(20,25,60); Grille grille=new Grille(10,10,15);
new FrameJeu(grille, 15); new FrameJeu(grille, 15);
} }
} }