IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AWT/Swing Java Discussion :

Probleme de keyLister sur une Frame


Sujet :

AWT/Swing Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Inscrit en
    Mars 2006
    Messages
    41
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 41
    Par défaut Probleme de keyLister sur une Frame
    Bonjour,
    je suis en train de faire un pacman dans le cadre d'un projet de programmation, et j'ai un souci à propos des Keylistener sur la Frame qui doivent écouter les directions que je rentre au clavier pour diriger le pacman.
    En effet, lorsque j'appuie sur le clavier, j'ai le code clavier correspondant à la touche appuyée qui apparait sur le terminal, mais cela n'est pas pris en compte par le jeu, et le pacman continue a avancer tout droit.
    De plus, qui je clique autre part que sur la fenetre de jeu, et que je reviens ds cette derniere, les keylisteners ne sont plus actifs!
    par ailleurs, la fenetre a un probleme de clipping, j'ai bien essayé d'y intégrer un double buffer(maladroitement je suppose) mais n'a pas sensiblement amélioré le rendu.
    Pouvez vous m'aider?


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    import fr.jussieu.script.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.image.*;
    class Pacman implements Runnable {
        public int[][] lab;
        public int i,j;
        public int x,y;
        public char direction;
        public Thread t;
        public decor d;
        public KeyListener k;
     
     
     
        public Pacman(int[][] v, decor d) {
    	this.i=0;
    	this.j=0;
    	this.lab=v;
    	this.direction='d';
    	this.t = new Thread();
    	this.d = d;
    	this.run();
        }
     
        public void run() {
    	this.d.start("Pacman");
    	this.d.getf().addKeyListener(this.d);
    		  while(this.t.isAlive() || !this.t.isInterrupted()){
    		      Deug.println(this.d.directionPressed);
    		      try {
    			  Thread.sleep(500);
    		      } catch (InterruptedException e) {}
    		      switch(this.direction) {
    		      case 'h': 
    			  for (int x=this.i+1;x<0;x--){
    			      if(!this.updatePacman(x,this.j) || this.updateDirection()) break;
    			      Deug.println("haut");
    			      try {
    				  Thread.sleep(10);
    			      } catch (InterruptedException e) {} 
    			  }
    			  break;
    		      case 'b':
    			  for (int x=this.i+1;x<this.lab.length;x++){
    			      if(!this.updatePacman(x,this.j) || this.updateDirection()) break;
    			      Deug.println("bas");
    			      try {
    				  Thread.sleep(500);
    			      } catch (InterruptedException e) {} 
    			  }
    			  break;
    		      case 'd':
    			  for (int y=this.j+1;y<this.lab[0].length;y++){
    						if(!this.updatePacman(this.i,y) || this.updateDirection()) break;
    						Deug.println("droite");
    						try {
    						    Thread.sleep(500);
    						} catch (InterruptedException e) {} 
    			  }
    			  break;
    		      case 'g':
    			  for (int y=this.j+1;y<0;y--){
    			      if(!this.updatePacman(this.i,y) || this.updateDirection()) break;
    			      Deug.println("haut");
    			      try {
    				  Thread.sleep(500);
    			      } catch (InterruptedException e) {} 
    			  }
    			  break;
    		      default:
    		      }
    		      if( this.d.directionPressed != 's') {
    			  this.updateDirection(); //change direction
    		      }    		
    		      try {
    			  Thread.sleep(10);
    		      } catch (InterruptedException e) {}
    		  }
        }
     
     
        public boolean updatePacman(int w,int t) {
    	if (this.direction=='g'){ 
    	    if (this.lab[w][t] != 1 || this.lab[w][t] != 2 ) {
    		this.x=this.i;
    		this.y=this.j;
    		this.d.traceVide(this.i,this.j);
    		this.j--;	
    		this.d.tracePacman(this.i,this.j);
    		return true;
    	    }
    	    else { this.d.tracePacman(this.i,this.j); return false;}
    	}
    	else if (this.direction=='d') {
    	    if (this.lab[w][t] != 1 || this.lab[w][t] != 2 ) {
    		this.x=this.i;
    		this.y=this.j;
    		this.d.traceVide(this.i,this.j);
    		this.j++;
    		this.d.tracePacman(this.i,this.j);
    		return true;
    	    }
    	    else { this.d.tracePacman(this.i,this.j); return false;}
    	} 	
    	else if (this.direction=='h') {
    	    if (this.lab[w][t] != 1 || this.lab[w][t] != 2 ) {
    		this.x=this.i;
    		this.y=this.j;
    		this.d.traceVide(this.i,this.j);
    		this.i--;
    		this.d.tracePacman(this.i,this.j);
    		return true;
    	    }
    	    else { this.d.tracePacman(this.i,this.j); return false;}
    	}
    	else if (this.direction=='b') {
    	    if (this.lab[w][t] != 1 || this.lab[w][t] != 2 ) {
    		this.x=this.i;
    		this.y=this.j;
    		this.d.traceVide(this.i,this.j);
    		this.i++;
    		this.d.tracePacman(this.i,this.j);
    		return true;
    	    }
    	    else { this.d.tracePacman(this.i,this.j);return false;}
    	}
    	return false;
        }
        public boolean updateDirection() {
    	Deug.println(this.d.directionPressed);
    	if(this.d.directionPressed == 'h' && this.lab[this.i-1][this.j] != 1 && this.lab[this.i-1][this.j] != 2 && this.i>-1 && this.i<this.lab[0].length && this.j>-1 && this.j<this.lab.length) {
    	    this.direction = 'h';
    	    this.d.directionPressed = 's';
    	    return true;
    	}
    	if(this.d.directionPressed == 'b' && this.lab[this.i+1][this.j] != 1 && this.lab[this.i+1][this.j] != 2 && this.i>-1 && this.i<this.lab[0].length && this.j>-1 && this.j<this.lab.length) {
    	    this.direction = 'b';
    	    this.d.directionPressed = 's';
    	    return true;
    	}		
    	if(this.d.directionPressed == 'g' && this.lab[this.i][this.j-1] != 1 && this.lab[this.i][this.j-1] != 2 && this.i>-1 && this.i<this.lab[0].length && this.j>-1 && this.j<this.lab.length) {
    	    this.direction = 'g';
    	    this.d.directionPressed = 's';
    	    return true;
    	}
    	if(this.d.directionPressed == 'd' && this.lab[this.i][this.j+1] != 1 && this.lab[this.i][this.j+1] != 2 && this.i>-1 && this.i<this.lab[0].length && this.j>-1 && this.j<this.lab.length) {
    	    this.direction = 'd';
    	    this.d.directionPressed = 's';
    	    return true;
    	}
    	return false;
        }
    }
    et aussi

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    import fr.jussieu.script.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    /**
     * @author humel
     *
     */
    public class decor extends Canvas implements KeyListener {
    	public int[][] lab;
    //	private static Drawable d = null;
    	private static JFrame f=null;
    	private int width, height, x, y;
        private Image Im;
        private Graphics Gr;
    	private Image[][] Images;
    	public char directionPressed;
    //	private TexturePaint texture;
    	private Pacman pacman;
    	public decor(int[][] t,int h, int l){
    		this.lab=t;
    		this.width=l;
    		this.height=h;
    		Images = new Image[t.length][t[0].length];
     
    	}
    	public void setDirectionPressed(int i) {
    				if (i == 37) this.directionPressed='g'; 
    				if (i == 38) this.directionPressed='h';
    				if (i == 39) this.directionPressed='d';
    				if (i == 40) this.directionPressed='b';
    	}
    	public void keyPressed(KeyEvent e){
    			  this.setDirectionPressed(e.getKeyCode());
    		 Deug.println(e.getKeyCode());
    			}
    			public void keyReleased(KeyEvent e){ }
    			public void keyTyped(KeyEvent e){ }
        public Dimension getPreferredSize() {
    		return new Dimension(width,height);
        }
    	public JFrame getf() { return f;}
    	public void paint(Graphics g) {
    		if (this.Im==null) {
    	    	Im = createImage(this.width,this.height);
    	   		this.Gr = Im.getGraphics();
    		}
    		g.drawImage(Im,this.x,this.y,this);
        }
        public void update(Graphics g) {
    		paint(g);
        }
     
    	public void start(String s) {
    		if (f!=null) return;
    		f = new JFrame(s);
    		f.setResizable(false);
    		f.getContentPane().add(this);
    		f.pack();
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setVisible(true);
    		try {
    	    	Thread.sleep(3000);
    		} catch(Exception e) {}
    		for(int j=0;j<lab.length;j++){
    	    	for(int i=0;i<lab[0].length;i++){
    	    		if(lab[i][j]==1){ 
    		    		traceBloc(i,j);
     
    				}
    				if (lab[i][j]==0){
    					traceVide(i,j);
    				}
    				f.getContentPane().add(this);
     
    			}
    		f.getContentPane().add(this);
     
    		}
    //	this.f.setVisible(true);
    //	this.t.start();
    	}
     
     
     
    	public void traceBloc(int i,int j){
    			this.Images[i][j]= f.getToolkit().getImage("brick.gif");
    			this.Gr.drawImage(this.Images[i][j],25*i,25*j,this.f);
    /*//			d.getGr().drawImage(f.getToolkit().getImage("brick.gif"),x,y,this.f);
    			this.bufferedImage = this.toBufferedImage(Toolkit.getDefaultToolkit().getImage("brick.gif"));
    			this.texture = new TexturePaint(Image,new Rectangle(0, 0, 25, 25)); 
    			Graphics2D g2d = (Graphics2D)this.Gr;
    			g2d.setPaint(texture);
    			g2d.fillRect(x,y,25,25);
    			f.getContentPane().add(); */
    			f.getContentPane().add(this);
    			}
    	public void traceVide(int i,int j){
    			this.Images[i][j]= f.getToolkit().getImage("vide.gif");
    			this.Gr.drawImage(this.Images[i][j],i*25,j*25,this.f);
    //			d.getGr().drawImage(f.getToolkit().getImage("brick.gif"),x,y,this.f);
    /*			this.x=x;
    			this.y=y;
    			this.Im = f.getToolkit().getImage("vide.gif");
    			this.Gr.drawImage(Im,x,y,this.f);
    //			this.Gr.drawImage(b,x,y,this.f); */
    			f.getContentPane().add(this);
    		}
     
    	public void tracePacman(int i,int j){
    			this.Images[i][j]= f.getToolkit().getImage("pacman.gif");
    			this.Gr.drawImage(this.Images[i][j],i*25,j*25,this.f);		
    			f.getContentPane().add(this);
    	}
    }

  2. #2
    Membre chevronné Avatar de let_me_in
    Inscrit en
    Mai 2005
    Messages
    441
    Détails du profil
    Informations forums :
    Inscription : Mai 2005
    Messages : 441
    Par défaut
    avec des InputMaps ca marcherai tres bien je pense.

  3. #3
    Expert confirmé
    Avatar de Baptiste Wicht
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2005
    Messages
    7 431
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Suisse

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2005
    Messages : 7 431
    Par défaut
    Je suis pas sur, mais essaie de l'ajouter à ton contenPane plutot que directement sur ta JFrame

  4. #4
    Membre averti
    Homme Profil pro
    Inscrit en
    Mars 2006
    Messages
    41
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 41
    Par défaut
    imputmaps?
    Je viens de regarder un peu, mais je ne vois pas quelle est la différence fondamentale entre les deux.
    Sont'ils plus faciles a implémenter?

  5. #5
    Membre Expert
    Avatar de xavlours
    Inscrit en
    Février 2004
    Messages
    1 832
    Détails du profil
    Informations forums :
    Inscription : Février 2004
    Messages : 1 832
    Par défaut
    Le KeyListener ne réagit que si le composant a le focus.

    L'InputMap permet de faire parvenir les évènements clavier au conteneur tant qu'ils ne sont pas gérés.

    Par exemple alt+f4 est géré avec une InputMap. Meme si c'est un bouton qui a le focus, la JFrame est informée de l'évènement clavier, ce qui ne serait pas arrivé avec un KeyListener.

    Et c'est sensiblement plus difficile à implémenter.
    "Le bon ni le mauvais ne me feraient de peine si si si je savais que j'en aurais l'étrenne." B.V.
    Non au langage SMS ! Je ne répondrai pas aux questions techniques par MP.
    Eclipse : News, FAQ, Cours, Livres, Blogs.Et moi.

  6. #6
    Membre averti
    Homme Profil pro
    Inscrit en
    Mars 2006
    Messages
    41
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 41
    Par défaut
    Oki merci pour l'info.
    Si j'ai bien compris, meme si je sors de la fenetre et que je reviens ensuite dedans, l'évènement clavier continuera a etre géré?

Discussions similaires

  1. Probleme d'evaluation sur une liste numérique
    Par cryptorchild dans le forum Langage
    Réponses: 2
    Dernier message: 16/01/2006, 19h39
  2. [MySQL 4.1] probleme d'accents sur une requete SQL
    Par tatayoyo dans le forum Langage SQL
    Réponses: 4
    Dernier message: 10/11/2005, 16h06
  3. probleme de texture sur une sphère
    Par sebpp dans le forum OpenGL
    Réponses: 2
    Dernier message: 12/04/2005, 19h46
  4. Mount / fstab - Problème de droit sur une partition
    Par Chance666 dans le forum Administration système
    Réponses: 5
    Dernier message: 17/01/2005, 15h57
  5. Probleme de pointeur sur une fonction
    Par nicky78 dans le forum C
    Réponses: 2
    Dernier message: 23/05/2004, 20h26

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo