title: How to repaint graphics after using a Keylistener?
body: <p>So my project is to create a shoot'em up type of game and i'd like that when i press arrows my square (that will after be the spaceship) change x;y position.
  We managed to have a test that the keylistener works so we're sure that the problem comes from the repaint function.</p>
  
  <p>Pls help, here's our code :      </p>
  
  <p>Class n°1</p>
  
  <pre><code>import java.awt.Color; 
  
  
  import java.awt.Graphics;
  import java.awt.event.KeyEvent;
  import java.awt.event.KeyListener;
  
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  
  import java.awt.Dimension; 
  
  import javax.swing.JFrame;
  
  public class Fenetre extends JFrame implements KeyListener {
  Panneau pan = new Panneau();
    public Fenetre(){             
      //nom de la fenetre
      this.setTitle("Projet");
      //taille
      this.setSize(450, 600);
      //centrer la fenetre
      this.setLocationRelativeTo(null);
      //Fenetre toujours en premier plan
      this.setAlwaysOnTop(true);
      //arrêt du processus à la fermeture de la fenêtre
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      //Fenetre non redimensionnable
      this.setResizable(false);
      //Couleur du fond
      this.setBackground(Color.DARK_GRAY);   
  
      //Instanciation d'un objet JPanel
      JPanel pan = new JPanel();
      JPanel tir = new JPanel();
      //On prévient notre JFrame que notre JPanel sera son content pane
      this.setContentPane(new Panneau());
  
      this.setVisible(true);
      this.addKeyListener(this);
    }
  @Override
  public void keyTyped(KeyEvent e) {
      // TODO Auto-generated method stub
  
  }
  
  
  @Override
  public void keyPressed(KeyEvent e) {
      // TODO Auto-generated method stub
      int c = e.getKeyCode();
          if(c==KeyEvent.VK_RIGHT){
              int x = pan.getPosX();
              x=x+10;
              pan.setPosX(x);
              pan.repaint();
              System.out.print("Droite ");}
          if(c==KeyEvent.VK_LEFT){
              int x = pan.getPosX();
              x=x-10;
              pan.setPosX(x);
              pan.repaint();
              System.out.print("Gauche ");}
          if(c==KeyEvent.VK_UP){
              int y = pan.getPosY();
              y=y+10;
              pan.setPosY(y);
              pan.repaint();
              System.out.print("Haut ");}
          if(c==KeyEvent.VK_DOWN){
              int y = pan.getPosY();
              y=y-10;
              pan.setPosY(y);
              pan.repaint();
              System.out.print("Bas ");}
          if(c==KeyEvent.VK_SPACE){
               this.setBackground(Color.CYAN);
  
  
  
          }
  
  
  }
  @Override
  public void keyReleased(KeyEvent e) {
      // TODO Auto-generated method stub
      int c = e.getKeyCode();
      if(c==KeyEvent.VK_SPACE){
           this.setBackground(Color.DARK_GRAY);}
  }
  
  
  
  
  
   }
  </code></pre>
  
  <p>Class n°2</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Graphics;
  
  import javax.swing.JPanel;
  
  import java.awt.Graphics;
  
  import javax.swing.JPanel;
  
  import java.awt.Graphics;
  import java.awt.event.KeyListener;
  
  import javax.swing.JPanel;
  
  public class Panneau extends JPanel {
  
    public int posX = 450/2-15;
    public int posY = 500;
  
  public void paintComponent(Graphics g){
      g.setColor(Color.red);
      g.fillRect(posX, posY, 30, 30);
      System.out.print("Carré ");
   }
    public int getPosX() {
      return posX;
    }
    public void setPosX(int posX) {
      this.posX = posX;
    }
    public int getPosY() {
      return posY;
    }
    public void setPosY(int posY) {
      this.posY = posY;
    }        
  
  }
  </code></pre>
  
  <p>And our main :</p>
  
  <pre><code>import java.awt.event.KeyEvent;
  import java.awt.event.KeyListener;
  
  import javax.swing.JFrame;
  
  
  public class Main extends JFrame {
  public static void main (String [] Args){
      Fenetre fen = new Fenetre();
  
  
  }}
  </code></pre>
  
  <p>PS: We're beginners so we might well have done a awful lots of things wrong</p>