Grille de JLabels avec Images
Bonjour,
voici mon problème: je dois à chaque fois que je clique sur un bouton, le JLabel se trouvant dans une grille de JLables doit changer.
Par exemple, lorqsue je clique sur le bouton (aller à droite), l'image de JLabel doit passer à droite. Merci de me proposer une solution.
Voici mes classes principales:
--> Classe GrilleDeLabels:
Code:
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
| package jp4;
import java.awt.*;
import javax.swing.*;
public class GrilleDeLabels
{
private JPanel p;
private JLabel[][] labels;
private ImageIcon[][] images;
private String[][] write;
public GrilleDeLabels(int nbLignes, int nbColonnes, String[][] textes)
{
write = new String[nbLignes][nbColonnes];
p = new JPanel(new GridLayout(nbLignes, nbColonnes));
labels = new JLabel[nbLignes][nbColonnes];
images = new ImageIcon[nbLignes][nbColonnes];
for(int i = 0; i < textes.length; i++)
{
for(int j = 0; j < textes.length; j++)
{
p.add(new JLabel(new ImageIcon(textes[i][j])));
}
}
write = textes;
}
public String[][] getText()
{
return write;
}
public void set(int a, int b, String c)
{
write[a][b] = c;
}
public JPanel get()
{
return p;
}
} |
--> Classe Jp4:
Code:
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| package jp4;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Jp4
{
private JFrame f;
private GrilleDeLabels g;
private String[][] s = new String[15][15];
private Position pos = new Position(0, 0);
private Monde world = new Monde(15, 15);
private Robot robot = new Robot(pos, "jp4/robot.jpg");
public Jp4()
{
world.setMurs(10);
world.setRobot(robot);
f = new JFrame("Jurassic Park 4");
JMenuBar bar = new JMenuBar();
JMenu menuFile = new JMenu("Fichier");
menuFile.add(new JMenuItem("Nouvelle balade"));
menuFile.add(new JMenuItem("Quitter"));
bar.add(menuFile);
JMenu menuHelp = new JMenu("Aide");
menuHelp.add(new JMenuItem("A propos de Jurassic Park 4"));
bar.add(menuHelp);
JLabel l = new JLabel("Projet LMI2 - Java - Jurassic Park 4 - COLIN Mathieu");
ImageIcon logo = new ImageIcon("jp4/logo.jpg");
Directions d = new Directions();
d.getHaut().addActionListener(new EcouteurHaut(this));
d.getBas().addActionListener(new EcouteurBas(this));
d.getGauche().addActionListener(new EcouteurGauche(this));
d.getDroite().addActionListener(new EcouteurDroite(this));
for(int i = 0; i < s.length; i++)
{
for(int j = 0; j < s.length; j++)
{
if(world.mur(i, j))
{
DinoMur dino = new DinoMur();
s[i][j] = dino.get();
}
else
{
s[i][j] = "jp4/vide.jpg";
}
}
}
s[0][0] = robot.image();
GrilleDeLabels g = new GrilleDeLabels(15, 15, s);
JLabel llogo = new JLabel(logo);
Container c = f.getContentPane();
c.add(bar, BorderLayout.NORTH);
c.add(llogo, BorderLayout.WEST);
c.add(l, BorderLayout.SOUTH);
c.add(g.get(), BorderLayout.CENTER);
c.add(d.get(), BorderLayout.EAST);
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void haut()
{
world.deplacerHaut();
int a = world.robot().position().ligne();
int b = world.robot().position().colonne();
s[a][b] = robot.image();
}
public void bas()
{
world.deplacerBas();
int a = world.robot().position().ligne();
int b = world.robot().position().colonne();
g.set(a, b, world.robot().image());
}
public void gauche()
{
world.deplacerGauche();
int a = world.robot().position().ligne();
int b = world.robot().position().colonne();
s[a][b] = robot.image();
}
public void droite()
{
world.deplacerDroite();
int a = world.robot().position().ligne();
int b = world.robot().position().colonne();
s[a][b] = robot.image();
}
}
class EcouteurHaut implements ActionListener
{
private Jp4 jp;
public EcouteurHaut(Jp4 jp)
{
this.jp = jp;
}
public void actionPerformed(ActionEvent e)
{
jp.haut();
}
}
class EcouteurBas implements ActionListener
{
private Jp4 jp;
public EcouteurBas(Jp4 jp)
{
this.jp = jp;
}
public void actionPerformed(ActionEvent e)
{
jp.bas();
}
}
class EcouteurGauche implements ActionListener
{
private Jp4 jp;
public EcouteurGauche(Jp4 jp)
{
this.jp = jp;
}
public void actionPerformed(ActionEvent e)
{
jp.gauche();
}
}
class EcouteurDroite implements ActionListener
{
private Jp4 jp;
public EcouteurDroite(Jp4 jp)
{
this.jp = jp;
}
public void actionPerformed(ActionEvent e)
{
jp.droite();
}
} |
--> Classe Monde:
Code:
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
| package jp4;
import java.util.*;
import java.io.*;
public class Monde
{
static Grille grille;
private Robot robot;
private Position posDepart;
public Monde()
{
grille = new Grille(10, 10);
posDepart = new Position(0, 0);
}
public Monde(int nbLig, int nbCol)
{
grille = new Grille(nbLig, nbCol);
posDepart = new Position(0,0);
}
public Grille grille()
{
return grille;
}
public boolean mur(int ligne, int colonne)
{
return grille.mur(ligne, colonne);
}
public Robot robot()
{
return robot;
}
public Position positionDepart()
{
return posDepart;
}
public int nbLignes()
{
return grille.nbLignes();
}
public int nbColonnes()
{
return grille.nbColonnes();
}
public boolean valide(int noL, int noC)
{
return (noL <= grille.nbLignes())&&(noC <= grille.nbColonnes());
}
public String toString()
{
return "" + grille() + "" + positionDepart() + "";
}
public boolean deplacementPossibleHaut()
{
robot.deplacerHaut();
int a = robot.position().ligne();
int b = robot.position().colonne();
Grille g = new Grille(a, b);
if(g.mur(a, b) || g.valide(a, b)){return false;} else{return true;}
}
public boolean deplacementPossibleBas()
{
robot.deplacerBas();
int a = robot.position().ligne();
int b = robot.position().colonne();
Grille g = new Grille(a, b);
if(g.mur(a, b) || g.valide(a, b)){return false;} else{return true;}
}
public boolean deplacementPossibleGauche()
{
robot.deplacerGauche();
int a = robot.position().ligne();
int b = robot.position().colonne();
Grille g = new Grille(a, b);
if(g.mur(a, b) || g.valide(a, b)){return false;} else{return true;}
}
public boolean deplacementPossibleDroite()
{
robot.deplacerDroite();
int a = robot.position().ligne();
int b = robot.position().colonne();
Grille g = new Grille(a, b);
if(g.mur(a, b) || g.valide(a, b)){return false;} else{return true;}
}
public void setRobot(Robot robot)
{
this.robot = robot;
}
public void setRobotDepart(Position position)
{
posDepart = position;
}
public void setRobotDepart()
{
this.robot.position();
}
public void setMurs(int nbMurs)
{
this.grille.setMurs(nbMurs);
}
public void deplacerHaut()
{
this.robot.deplacerHaut();
}
public void deplacerBas()
{
this.robot.deplacerBas();
}
public void deplacerDroite()
{
this.robot.deplacerDroite();
}
public void deplacerGauche()
{
this.robot.deplacerGauche();
}
} |
--> Classe Grille:
Code:
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| package jp4;
import java.io.* ;
import java.util.*;
public class Grille
{
public static int MUR = 1 ;
public static int VIDE = 0 ;
private int[][] grille ;
private int nbLignes ;
private int nbColonnes ;
private int nbMurs ;
public Grille (int nbLig, int nbCol)
{
nbLignes = nbLig;
nbColonnes = nbCol;
nbMurs = 0;
grille = new int[nbLig][nbCol];
}
public int nbLignes()
{
return nbLignes;
}
public int nbColonnes()
{
return nbColonnes;
}
public int nbCases()
{
return nbLignes*nbColonnes;
}
public int nbMurs()
{
return nbMurs;
}
public boolean mur(int noLig, int noCol)
{
return (grille[noLig][noCol]) == MUR;
}
public boolean valide(int noL, int noC)
{
return (noL<=nbLignes())&&(noC<=nbColonnes());
}
public int getCase(int noLig, int noCol)
{
int valeur;
if(mur(noLig, noCol))
{
valeur = MUR;
return valeur;
}
else
{
valeur = VIDE;
return valeur;
}
}
public void setCase(int noLig, int noCol, int val)
{
if(getCase(noLig, noCol) == MUR && val == VIDE)nbMurs--;
if(getCase(noLig, noCol) == VIDE && val == MUR)nbMurs++;
grille[noLig][noCol] = val;
}
public void vider()
{
nbMurs = 0;
int a = nbLignes();
int b = nbColonnes();
int[][] grille2 = new int[a][b];
grille = grille2;
}
public void setMurs(int nbMurs)
{
while(nbMurs() < nbMurs)
{
setCase(((int)(Math.random()*nbLignes())), ((int)(Math.random()*nbColonnes())),MUR);
}
}
public String toString() {
StringBuffer res = new StringBuffer("") ;
for(int nbl=0; nbl<nbLignes; nbl++) {
for(int nbc=0; nbc<nbColonnes; nbc++) {
res.append(getCase(nbl, nbc) + " ") ;
}
res.append("\n") ;
}
return res.toString() ;
}
} |
--> Classe Robot:
Code:
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
| package jp4;
import javax.swing.* ;
public class Robot
{
private Position position ;
private String image ;
public Robot (Position pos, String image)
{
this.position = pos;
this.image = image;
}
public Robot (String image)
{
this.image = image;
Position position = new Position(0, 0);
}
public Position position()
{
return position;
}
public String image()
{
return image;
}
public void setPosition(Position pos)
{
position = pos;
}
public void setImage(String im)
{
image = im;
}
public void deplacerHaut()
{
position.deplacer(-1, 0);
}
public void deplacerBas()
{
position.deplacer(1, 0);
}
public void deplacerGauche()
{
position.deplacer(0, -1);
}
public void deplacerDroite()
{
position.deplacer(0, 1);
}
} |
--> Classe Directions:
Code:
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 68 69 70 71 72 73
| package jp4;
import java.awt.*;
import javax.swing.*;
public class Directions
{
private JButton bhaut;
private JButton bbas;
private JButton bgauche;
private JButton bdroite;
private ImageIcon haut;
private ImageIcon bas;
private ImageIcon gauche;
private ImageIcon droite;
private JPanel p;
private JLabel l1;
private JLabel l2;
private JLabel l3;
private JLabel l4;
private JLabel l5;
public Directions()
{
p = new JPanel(new GridLayout(3, 3));
haut = new ImageIcon("jp4/haut.jpg");
bas = new ImageIcon("jp4/bas.jpg");
gauche = new ImageIcon("jp4/gauche.jpg");
droite = new ImageIcon("jp4/droite.jpg");
bhaut = new JButton(haut);
bbas = new JButton(bas);
bgauche = new JButton(gauche);
bdroite = new JButton(droite);
l1 = new JLabel("");
l2 = new JLabel("");
l3 = new JLabel("");
l4 = new JLabel("");
l5 = new JLabel("");
p.add(l1);
p.add(bhaut);
p.add(l2);
p.add(bgauche);
p.add(l3);
p.add(bdroite);
p.add(l4);
p.add(bbas);
p.add(l5);
}
public JPanel get()
{
return p;
}
public JButton getHaut()
{
return bhaut;
}
public JButton getBas()
{
return bbas;
}
public JButton getGauche()
{
return bgauche;
}
public JButton getDroite()
{
return bdroite;
}
} |
--> Classe DinoMur:
Code:
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
| package jp4;
public class DinoMur
{
String dino;
public DinoMur()
{
String r = "";
String mur1 = "jp4/mur1.jpg";
String mur2 = "jp4/mur2.jpg";
String mur3 = "jp4/mur3.jpg";
String mur4 = "jp4/mur4.jpg";
String mur5 = "jp4/mur5.jpg";
int a = (int) (Math.random()*5);
if(a == 0){r = mur1;}
else if(a == 1){r = mur2;}
else if(a == 2){r = mur3;}
else if(a == 3){r = mur4;}
else if(a == 4){r = mur5;}
dino = r;
}
public String get()
{
return dino;
}
} |
--> Classe Position:
Code:
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
| package jp4;
public class Position
{
private int ligne ;
private int colonne ;
public Position (int lig, int col) {
this.ligne = lig ;
this.colonne = col ;
} // Position(int, int)
public int ligne(){
return this.ligne;
} // ligne()
public int colonne(){
return this.colonne;
} // colonne()
public int distance (Position pos){
int dl = ligne() - pos.ligne() ;
int dc = colonne() - pos.colonne() ;
return Math.abs(dl) + Math.abs(dc) ;
} // distance(Position)
public boolean estAGauche(Position p) {
return (ligne == p.ligne() && colonne == p.colonne()-1);
} // estAGauche(Position)
public boolean estADroite(Position p) {
return (ligne == p.ligne() && colonne == p.colonne()+1);
} // estADroite(Position)
public boolean estEnHaut(Position p) {
return (ligne == p.ligne()-1 && colonne == p.colonne());
} // estEnHaut(Position)
public boolean estEnBas(Position p) {
return (ligne == p.ligne()+1 && colonne == p.colonne());
} // estEnBas(Position)
public void deplacer(int tlig, int tcol){
ligne += tlig ;
colonne += tcol ;
} // deplacer(int, int)
public boolean equals(Object o){
if (! (o instanceof Position)) return false;
Position position = (Position)o;
return position.ligne()==ligne && position.colonne()==colonne;
} // equals(Object)
public String toString(){
return "<"+ligne+", "+(char)(colonne+'A'-1)+">";
}
} // class Position |
--> Classe JurassicPark4:
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| package jp4;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JurassicPark4
{
public static void main(String args[])
{
Jp4 exe = new Jp4();
}
} |