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
|
public class LabyrintheNiv2 extends JPanel{
/************************************** VARIABLES **************************************/
private BarreAction barreAction;
private int[][] grille;
private ImageIcon caseVide = new ImageIcon(new ImageIcon(getClass().getResource("/Vide.png")).getImage().getScaledInstance(35, 35, Image.SCALE_DEFAULT));
private ImageIcon caseMur = new ImageIcon(new ImageIcon(getClass().getResource("/Mur.png")).getImage().getScaledInstance(35, 35, Image.SCALE_DEFAULT));
private ImageIcon caseDepArr = new ImageIcon(new ImageIcon(getClass().getResource("/DepArr.png")).getImage().getScaledInstance(35, 35, Image.SCALE_DEFAULT));
/************************************** CONSTRUCTEUR **********************************/
public LabyrintheNiv2(BarreAction barreAction){
this.barreAction = barreAction;
creerLab();
}
/************************************** FONCTIONS *************************************/
public void creerLab() {
Dimension dim = new Dimension(caseVide.getIconWidth(), caseVide.getIconHeight());
grille = new int[][]{
{ 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1 },
{ 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1 },
{ 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0 },
{ 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0 },
{ 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0 },
{ 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 2 }
};
JButton[][] button = new JButton[10][20];
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(10, 20));
this.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
for (int i = 0; i < grille.length; ++i)
for (int j = 0; j < grille.length; ++j) {
if(grille[i][j] == 0)
button[i][j] = new JButton(caseVide);
else if(grille[i][j] == 1)
button[i][j] = new JButton(caseMur);
else
button[i][j] = new JButton(caseDepArr);
button[i][j].setPreferredSize(dim);
panel.add(button[i][j]);
}
this.add(panel);
}
} |
Partager