0

I am running a simple game of Pong. The game begins when the user presses the spacebar. Works great in Windows XP, but then gets all weird in Windows 7. If you press the spacebar, then click the game window, it runs. If you click first, then hit space, nothing. I have pasted the main class below.

 public class Game extends JPanel {

static final int FW = 400;
static final int FH = 300;

Ball ball = new Ball(this);
Paddle paddleL = new Paddle(this);
PaddleAI paddleR = new PaddleAI(this);
public static boolean go;
static KeyListener key;


public Game(){
   setFocusable(true);
   addKeyListener(key = new KeyListener(){
       @Override
        public void keyTyped(KeyEvent e){

       }
       @Override
        public void keyReleased(KeyEvent e){
           paddleL.keyReleased(e);
           if(e.getKeyCode() == KeyEvent.VK_SPACE)
               go = true;
       }
       @Override
       public void keyPressed(KeyEvent e){
           paddleL.keyPressed(e);
           if(e.getKeyCode()==KeyEvent.VK_SPACE)
               go = true;
       }
   });


 }


void setup(){
   ball.setup();
   paddleL.setup();
   paddleR.setup();
}

void move(){
    ball.move();
    paddleL.move();
    paddleR.move();
}


public void paint(Graphics g){
    super.paint(g);
    ball.paint(g);
    paddleL.paint(g);
    paddleR.paint(g);
}

public static void main(String[] args) throws InterruptedException{

    //final int FW = 400;
   // final int FH = 300;

    JFrame frame = new JFrame();
    Game game = new Game();

    game.setBackground(Color.black);
    frame.add(game);
    frame.getContentPane().setBackground(Color.cyan);
    frame.setBackground(Color.black);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(FW, FH);
    frame.setLocationRelativeTo(null);
    frame.setTitle("Pong");
    frame.setResizable(true);
    frame.setVisible(true);


   game.setup();

    while(true){

        if(go){
            game.move();
            game.repaint();
            Thread.sleep(10);
        }

    }
}

}

mKorbel
  • 109,525
  • 20
  • 134
  • 319
J.Law
  • 3
  • 5
  • 1
    Please see: http://stackoverflow.com/questions/8074316/keylistener-not-working-after-clicking-button – Nick Rippe Mar 05 '14 at 14:56
  • 1) It's recommendend use [Key binding over KeyListener](http://stackoverflow.com/a/15290065/1795530) 2) You should not override `paint()` method but `paintComponent()` instead: [A Closer Look at the Paint Mechanism](http://docs.oracle.com/javase/tutorial/uiswing/painting/closer.html) – dic19 Mar 05 '14 at 15:39

1 Answers1

2

You're going to want to use Key Bindings instead of Key Listeners. A KeyListener is very picky about what component is focused, which is the problem you're running into. Here's a quick example of how to use a Key Binding:

import java.awt.event.*;
import javax.swing.*;

public class KeyBindings extends Box{
    public KeyBindings(){
        super(BoxLayout.Y_AXIS);
        final JTextPane textArea = new JTextPane();
        textArea.insertComponent(new JLabel("Text"));
        add(textArea);

        Action action = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setText("New Text");
            }};
         String keyStrokeAndKey = "control SPACE";
         KeyStroke keyStroke = KeyStroke.getKeyStroke(keyStrokeAndKey);
         textArea.getInputMap().put(keyStroke, keyStrokeAndKey);
         textArea.getActionMap().put(keyStrokeAndKey, action);
    }


    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new KeyBindings());
        frame.pack();
        frame.setVisible(true);
    }
}
Nick Rippe
  • 6,465
  • 14
  • 30