1
 class Card31 extends JPanel implements KeyListener, MouseListener {
     public Card31() {
         addKeyListener(this);
         addMouseListener(this);
         x1 = 700;

     }

     public void keyPressed(KeyEvent e) {

     }

     public void keyReleased(KeyEvent e)

     {

     }

     public void keyTyped(KeyEvent e)

     {

         key = e.getKeyCode();
         if (key == KeyEvent.VK_RIGHT)
             x1 += 10;
         else if (key == KeyEvent.VK_LEFT)
             x1 -= 10;
         repaint();
     }
     public void mouseEntered(MouseEvent e) {
         requestFocus();
     }

     public void mouseExited(MouseEvent e) {}
     public void mousePressed(MouseEvent e) {}
     public void mouseReleased(MouseEvent e) {}

     public void mouseClicked(MouseEvent e) {

     }
     public void mouseClicked(MouseEvent e) {

     }
     public void paintComponent(Graphics g) {
         super.paintComponent(g);
         court2 = Toolkit.getDefaultToolkit().getImage("court2.jpg");
         bhoop = Toolkit.getDefaultToolkit().getImage("bhoop2.png");
         balla = Toolkit.getDefaultToolkit().getImage("balla.png");
         ballc = Toolkit.getDefaultToolkit().getImage("ballc.png");
         balld = Toolkit.getDefaultToolkit().getImage("balld.png");
         balle = Toolkit.getDefaultToolkit().getImage("balle.png");
         ballf = Toolkit.getDefaultToolkit().getImage("ballf.png");
         balli = Toolkit.getDefaultToolkit().getImage("balli.png");
         balll = Toolkit.getDefaultToolkit().getImage("balll.png");
         balln = Toolkit.getDefaultToolkit().getImage("balln.png");
         ballo = Toolkit.getDefaultToolkit().getImage("ballo.png");
         ballr = Toolkit.getDefaultToolkit().getImage("ballr.png");
         balls = Toolkit.getDefaultToolkit().getImage("balls.png");
         ballt = Toolkit.getDefaultToolkit().getImage("ballt.png");
         ballu = Toolkit.getDefaultToolkit().getImage("ballu.png");
         bally = Toolkit.getDefaultToolkit().getImage("bally.png");
         g.drawImage(court2, 0, 0, 1600, 1000, this);
         g.drawImage(ballo, 130, 100, 110, 110, this);
         g.drawImage(ballc, 280, 100, 110, 110, this);
         g.drawImage(balln, 430, 100, 110, 110, this);
         g.drawImage(balld, 580, 100, 110, 110, this);
         g.drawImage(balle, 730, 100, 110, 110, this);
         g.drawImage(ballc, 880, 100, 110, 110, this);
         g.drawImage(balle, 1030, 100, 110, 110, this);
         g.drawImage(ballu, 1180, 100, 110, 110, this);
         g.drawImage(bally, 1330, 100, 110, 110, this);
         g.drawRect(30, 30, 80, 20);
         Font plainfont = new Font("Serif", Font.BOLD, 20);
         g.setFont(plainfont);
         g.setColor(Color.BLACK);
         g.drawString("Score: " + score1, 30, 30);
         g.fillRect(700, 800, 200, 20);
         g.drawImage(bhoop, x1, 500, 150, 150, this);

         g.drawString(msg1, 200, 200);
         System.out.println("x1" + x1);
     }
 }

Above I attached part of the code, a nested class. X1 is a global variable and is initialized in the constructor. The KeyPressed, which is used for moving the Iamge bhoop, is not working. I have checked all the code to make it right but the Image is not moving no matter what. Please help me solve this. I would really appreciate your help<3.

TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
Hallllis
  • 31
  • 2
  • 3
    Look [here](http://stackoverflow.com/questions/22741215/how-to-use-key-bindings-instead-of-key-listeners/22741216). – user1803551 May 18 '15 at 23:52
  • 3
    As has been answered in many similar questions -- don't use KeyListeners for this but instead use Key Bindings. Otherwise your forced to use the kludge of making your JPanel retain the focus, not good. Also, don't read all those images in the paintComponent method as this will slow your GUI's responsiveness to a crawl. Instead read the images in once, say in a constructor, and store the images or ImageIcons in fields. – Hovercraft Full Of Eels May 18 '15 at 23:52

2 Answers2

2

The Component which has a registered KeyListener must have focus for the KeyListener to fire. Call requestFocus on the Component to request that the component has focus, or better yet use KeyBindings

copeg
  • 8,290
  • 19
  • 28
0

As explained here: addKeyListener() doesn't work for JPanel

your panel needs to be focusable:

this.setFocusable(true);
this.requestFocusInWindow();
Community
  • 1
  • 1