2023-04-18 12:29:43 +02:00
|
|
|
import java.awt.*;
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
public class FenetreRndmGrille extends Fenetre{
|
2023-04-23 23:34:51 +02:00
|
|
|
public static final int RANDOM=1;
|
|
|
|
|
|
|
|
private int taille;
|
2023-04-18 12:29:43 +02:00
|
|
|
private int ValeurEntre;
|
|
|
|
private int ValeurSortie;
|
|
|
|
|
2023-04-23 23:34:51 +02:00
|
|
|
private int[] tabCouleur;
|
|
|
|
private boolean[][] grille;
|
2023-04-25 11:42:59 +02:00
|
|
|
private Cellules[][] grilleCellules;
|
|
|
|
|
|
|
|
private Modifications modif;
|
2023-04-18 12:29:43 +02:00
|
|
|
|
2023-04-23 23:34:51 +02:00
|
|
|
public FenetreRndmGrille(int taille){
|
2023-04-18 12:29:43 +02:00
|
|
|
super();
|
2023-04-23 23:34:51 +02:00
|
|
|
this.taille = taille;
|
|
|
|
this.grille = new boolean[this.taille][this.taille];
|
2023-04-25 11:42:59 +02:00
|
|
|
this.grilleCellules = new Cellules[this.taille][this.taille];
|
2023-04-23 23:34:51 +02:00
|
|
|
this.tabCouleur = new int[] {1, 1};
|
2023-04-18 12:29:43 +02:00
|
|
|
}
|
|
|
|
|
2023-04-23 23:34:51 +02:00
|
|
|
public void randomGrille(){
|
|
|
|
|
2023-04-18 12:29:43 +02:00
|
|
|
this.fenetre.setSize(600, 600);
|
|
|
|
this.fenetre.setLocation(450, 200);
|
|
|
|
|
2023-04-23 23:34:51 +02:00
|
|
|
GridLayout gestionnaire = new GridLayout(this.taille,this.taille);
|
2023-04-18 12:29:43 +02:00
|
|
|
this.fenetre.setLayout(gestionnaire);
|
|
|
|
|
2023-04-25 11:42:59 +02:00
|
|
|
PanneauModification interfacePanel = new PanneauModification(this.grille, this.taille, this.grilleCellules, this.fenetre);
|
2023-04-23 23:34:51 +02:00
|
|
|
interfacePanel.SetUp();
|
2023-04-25 11:42:59 +02:00
|
|
|
|
2023-04-23 23:34:51 +02:00
|
|
|
/* ======= Valeurs aléatoire pour l'entre et la sortie ========== */
|
2023-04-18 12:29:43 +02:00
|
|
|
|
|
|
|
Random rand = new Random();
|
2023-04-23 23:34:51 +02:00
|
|
|
ValeurEntre = rand.nextInt(this.taille*this.taille);
|
|
|
|
ValeurSortie = rand.nextInt(this.taille*this.taille);
|
|
|
|
|
|
|
|
if(ValeurEntre == ValeurSortie){
|
|
|
|
while(ValeurEntre == ValeurSortie){
|
|
|
|
ValeurSortie = rand.nextInt(this.taille*this.taille);
|
2023-04-18 12:29:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-23 23:34:51 +02:00
|
|
|
int compteur=0;
|
2023-04-18 12:29:43 +02:00
|
|
|
|
2023-04-23 23:34:51 +02:00
|
|
|
for(int i=0; i<taille; i++){
|
|
|
|
for(int j=0; j<taille; j++){
|
|
|
|
int nombreAleatoire = rand.nextInt(2); // génère un nombre entre 0 et 2
|
2023-04-18 12:29:43 +02:00
|
|
|
|
2023-04-23 23:34:51 +02:00
|
|
|
if(nombreAleatoire == 0){
|
|
|
|
grille[i][j] = false;
|
|
|
|
} else {
|
|
|
|
grille[i][j] = true;
|
|
|
|
}
|
2023-04-18 12:29:43 +02:00
|
|
|
|
2023-04-25 11:42:59 +02:00
|
|
|
this.modif = new Modifications(interfacePanel, grille,this.tabCouleur);
|
2023-04-18 12:29:43 +02:00
|
|
|
|
2023-04-23 23:34:51 +02:00
|
|
|
if(compteur == ValeurEntre)
|
|
|
|
{
|
2023-04-26 13:13:15 +02:00
|
|
|
Cellules cellules = new Cellules(i,j, Cellules.ENTREE);
|
2023-04-23 23:34:51 +02:00
|
|
|
this.fenetre.add(cellules);
|
|
|
|
cellules.addMouseListener(modif);
|
2023-04-25 11:42:59 +02:00
|
|
|
grilleCellules[i][j] = cellules;
|
2023-04-23 23:34:51 +02:00
|
|
|
}
|
|
|
|
else if(compteur == ValeurSortie)
|
|
|
|
{
|
2023-04-26 13:13:15 +02:00
|
|
|
Cellules cellules = new Cellules(i, j, Cellules.SORTIE);
|
2023-04-23 23:34:51 +02:00
|
|
|
this.fenetre.add(cellules);
|
|
|
|
cellules.addMouseListener(modif);
|
2023-04-25 11:42:59 +02:00
|
|
|
grilleCellules[i][j] = cellules;
|
2023-04-23 23:34:51 +02:00
|
|
|
}
|
|
|
|
else if(grille[i][j] == true)
|
|
|
|
{
|
2023-04-26 13:13:15 +02:00
|
|
|
Cellules cellules = new Cellules(i, j, Cellules.COULOIR);
|
2023-04-23 23:34:51 +02:00
|
|
|
this.fenetre.add(cellules);
|
|
|
|
cellules.addMouseListener(modif);
|
2023-04-25 11:42:59 +02:00
|
|
|
grilleCellules[i][j] = cellules;
|
2023-04-23 23:34:51 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-26 13:13:15 +02:00
|
|
|
Cellules cellules = new Cellules(i, j, Cellules.MUR);
|
2023-04-23 23:34:51 +02:00
|
|
|
this.fenetre.add(cellules);
|
|
|
|
cellules.addMouseListener(modif);
|
2023-04-25 11:42:59 +02:00
|
|
|
grilleCellules[i][j] = cellules;
|
2023-04-18 12:29:43 +02:00
|
|
|
}
|
|
|
|
|
2023-04-23 23:34:51 +02:00
|
|
|
this.tabCouleur = modif.getGateState();
|
2023-04-18 12:29:43 +02:00
|
|
|
compteur++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.fenetre.setVisible(true);
|
|
|
|
}
|
|
|
|
}
|