0
votes

Binds W,S and I,k to left and right paddle of pong. According to Eclipse the codes gets into class p1_upaction extends AbstractAction but does not run public void actionPerformed(ActionEvent e). I am not sure what to do.KeyInput is called the action is not registering.

public void frame() {
    frame = new JFrame("Pong");
    frame.setSize(width,height);
    frame.setLayout(new BorderLayout());
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container c = frame.getContentPane();

    frameWidth = frame.getWidth();
    frameHeight = frame.getHeight();        

    menuGraphics = new menuGraphic();
    menuGraphics.addMouseMotionListener(this);
    menuGraphics.addMouseListener(this);

    gameGraphics = new gameGraphic(puck,p1,p2);


    panel = new JPanel();
    panel.setLayout(new BorderLayout());          
    panel.setSize(width,height);
    panel.add(menuGraphics,BorderLayout.CENTER);
    keyInput();

    menuState = true;
    c.add(panel);
    frame.setVisible(true);

    puck = new Puck(frameWidth/2,frameHeight/2,20,20);  
    p1 = new Paddle(frameWidth/8,frameHeight/2,20,100);
    p2 = new Paddle(frameWidth/8*7-20,frameHeight/2,20,100);
    puck.setPaddle(p1, p2);
    p1.setPuck(puck);
    p2.setPuck(puck);
}

public void keyInput() {
    p1_upAction = new p1_upaction();
    p1_downAction = new p1_downaction();
    p2_upAction = new p2_upaction();
    p2_downAction = new p2_downaction();
    panel.getInputMap().put( KeyStroke.getKeyStroke( "W" ),"p1_moveUp" );
    panel.getActionMap().put("p1_moveUp", p1_upAction);
    panel.getInputMap().put( KeyStroke.getKeyStroke( "S" ),"p1_moveDown" );
    panel.getActionMap().put("p1_moveDown", p1_downAction);
    panel.getInputMap().put( KeyStroke.getKeyStroke( "I" ),"p2_moveUp" );
    panel.getActionMap().put("p1_moveUp", p2_upAction);
    panel.getInputMap().put( KeyStroke.getKeyStroke( "K" ),"p2_moveDown" );
    panel.getActionMap().put("p1_moveDown", p2_downAction);
}
class p1_upaction extends AbstractAction {
    public void actionPerformed(ActionEvent e) {
        System.out.println("up");   
    }

}
1

1 Answers

0
votes
panel = new JPanel();
panel.setLayout(new BorderLayout());          
panel.setSize(width,height);
panel.add(menuGraphics,BorderLayout.CENTER);

You create a panel with a BorderLayout and then add your "menuGraphics" component to this panel. This means the menuGraphics component will have focus.

By default the panel must have focus for the key bindings to work with the default InputMap. However, you add the Key Bindings to the "panel". Since it doesn't have focus the bindings don't work.

The solution is to either:

  1. add the key bindings to the "menuGraphics" component, or
  2. use the JComponent.WHEN_IN_FOCUSED_WINDOW InputMap when you add the bindings to the InputMap. This means that the key bindings will work even if the "panel" component doesn't have focus.

Read the section from the Swing tutorial on How to Use Key Bindings for more information about the various InputMaps.

You can also check out Motion Using the Keyboard for a working example of key bindings.