Déplacer un listener d'un objet à un autre.
Bonjour les gens!
Donc comme il est dit dans le titre, j'aimerais déplacer un listener d'un objet à un autre. Je m'explique : j'ai une liste de joueurs
Code:
/*....*/ Joueur[] listeJoueur = new Joueur[7]; /*....*/
C'est le listeJoueur[0] qui peut bouger et je voudrais faire quand j'appuis sur espace, le listener passe au joueur le plus proche. Voilà mon code en entier :
la Class Joueur
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
|
public class Joueur extends JPanel{
private String chemin_sariny;
private int a_couper_sur_x;
private int a_couper_sur_y;
private boolean a_la_balle;
private int position_selon_x;
private int position_selon_y;
public void setPathName(String ouioui)
{
chemin_sariny = ouioui;
}
public String getPathName()
{
return chemin_sariny;
}
public void setTapahanaY(int x)
{
a_couper_sur_x = x;
}
public int getTapahanaY()
{
return a_couper_sur_x;
}
public void setTapahanaX(int y)
{
a_couper_sur_y = y;
}
public int getTapahanaX()
{
return a_couper_sur_y;
}
public void setMitazonaBolVe(boolean quoi){
a_la_balle = quoi;
}
public boolean getMitazonaBolVe(){
return a_la_balle;
}
public void setPositionX(int x)
{
position_selon_x = x;
}
public int getPositionX()
{
return position_selon_x;
}
public void setPositionY(int y)
{
position_selon_y = y;
}
public int getPositionY()
{
return position_selon_y;
}
public Joueur(String chemin, int x, int y){
setOpaque(false);
setFocusable(true);
setLocation(new Point(getPositionX(),getPositionY()));
setPathName(chemin);
setTapahanaX(x);
setTapahanaY(y);
setPositionX(0);
setPositionY(10);
JLabel le_joueur = new JLabel();
JoueurTrottine.setImageInJLabel(le_joueur,getPathName(),getTapahanaX(),getTapahanaY());
JoueurTrottine.bouge(getPathName(),getTapahanaX(),getTapahanaY(),le_joueur);
le_joueur.setPreferredSize(new Dimension(41,41));
add(le_joueur);
}
public void passeBallon(Joueur aPasser){
addKeyListener(new JoueurListener(aPasser));
}
} |
Ma class JoueurTrottine
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
|
public class JoueurTrottine{
static int tap;
static int tap_y;
public static void bouge(String pathName, int CoupeX, int CoupeY, JLabel lab) //bouger
{
tap = CoupeX;
tap_y = CoupeY;
Timer tm = new Timer(10,new ActionListener() {
public void actionPerformed(ActionEvent e) {
setImageInJLabel(lab, pathName, tap, tap_y);
tap = tap + 41;
if(tap >= 329 )
{
tap = 0;
}
}
});
tm.start();
}
public static void upDownLeftRight(JPanel pane){
pane.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
int X = pane.getX();
int Y = pane.getY();
if (key == KeyEvent.VK_LEFT)
{
X = X-5;
tap_y = 41;
}
if (key == KeyEvent.VK_RIGHT) {
X = X+5;
tap_y = 123;
}
if (key == KeyEvent.VK_UP) {
Y = Y-5;
tap_y = 0;
}
if (key == KeyEvent.VK_DOWN) {
Y = Y+5;
tap_y = 82;
}
pane.setLocation(new Point(X,Y));
}
});
}
public static void setImageInJLabel(JLabel lab, String chemin_image, int tapahana_x, int tapahana_y){
ImageIcon icon = new ImageIcon(Sprite.DecoupeImage(chemin_image, tapahana_x, tapahana_y));
lab.setIcon(icon);
}
} |
et le JoueurListener qui devrait être passé au fur et à mesure où on passe la balle
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
|
public class JoueurListener implements KeyListener{
Joueur pane;
int x;
int y;
public JoueurListener(Joueur pa){
pane = pa;
}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_SPACE)
{
JoueurTrottine.upDownLeftRight(pane);
System.out.println("space");
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
} |
Merci pour ceux qui vont répondre. :)