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");
}
}