-1
package Practice.GUI.KeyBinding.P2;

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

public class Game2 {
    Action Up, Down, Left, Right, Up2, Down2, Left2, Right2;
    JFrame frame;
    JLabel label1, label2;

    Game2() {
        ImageIcon icon = new ImageIcon("rocket.png");
        Image resize = icon.getImage().getScaledInstance(50, 70, Image.SCALE_DEFAULT);
        ImageIcon ric = new ImageIcon(resize);

        ImageIcon icon2 = new ImageIcon("rocket.png");
        Image resize2 = icon.getImage().getScaledInstance(50, 70, Image.SCALE_DEFAULT);
        ImageIcon ric2 = new ImageIcon(resize2);

        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.setLayout(null);
        frame.setLocationRelativeTo(null);
        frame.getContentPane().setBackground(Color.black);

        label1 = new JLabel();
        label1.setBounds(0, 300, 100, 100);
        label1.setIcon(ric);

        label2 = new JLabel();
        label2.setBounds(430, 300, 100, 100);
        label2.setIcon(ric2);

        Up = new UpAction();
        Down = new DownAction();
        Left = new LeftAction();
        Right = new RightAction();
        Up2 = new UpAction();
        Down2 = new DownAction();
        Left2 = new LeftAction();
        Right2 = new RightAction();

        label1.getInputMap().put(KeyStroke.getKeyStroke('w'), "upAction1");
        label1.getActionMap().put("upAction1", Up);
        label1.getInputMap().put(KeyStroke.getKeyStroke('s'), "downAction1");
        label1.getActionMap().put("downAction", Down);
        label1.getInputMap().put(KeyStroke.getKeyStroke('a'), "leftAction1");
        label1.getActionMap().put("leftAction1", Left);
        label1.getInputMap().put(KeyStroke.getKeyStroke('d'), "rightAction1");
        label1.getActionMap().put("rightAction1", Right);

        label2.getInputMap().put(KeyStroke.getKeyStroke("UP"), "upAction2");
        label2.getActionMap().put("upAction2", Up2);
        label2.getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "downAction2");
        label2.getActionMap().put("downAction2", Down2);
        label2.getInputMap().put(KeyStroke.getKeyStroke("LEFT"), "leftAction2");
        label2.getActionMap().put("leftAction2", Left2);
        label2.getInputMap().put(KeyStroke.getKeyStroke("RIGHT"), "rightAction2");
        label2.getActionMap().put("rightAction2", Right2);

        frame.add(label1);
        frame.add(label2);
        frame.setVisible(true);
    }

    public class UpAction extends AbstractAction {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == label1) {
                label1.setLocation(label1.getX(), label1.getY() - 10);
            } else if (e.getSource() == label2) {
                label2.setLocation(label2.getX(), label2.getY() - 10);
            }
        }
    }

    public class DownAction extends AbstractAction {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == label1) {
                label1.setLocation(label1.getX(), label1.getY() + 10);
            } else if (e.getSource() == label2) {
                label2.setLocation(label2.getX(), label2.getY() + 10);
            }
        }
    }

    public class LeftAction extends AbstractAction {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == label1) {
                label1.setLocation(label1.getX() - 10, label1.getY());
            } else if (e.getSource() == label2) {
                label2.setLocation(label2.getX() - 10, label2.getY());
            }
        }
    }

    public class RightAction extends AbstractAction {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == label1) {
                label1.setLocation(label1.getX() + 10, label1.getY());
            } else if (e.getSource() == label2) {
                label2.setLocation(label2.getX() + 10, label2.getY());
            }
        }
    }
}

I'm working on a simple two-player game interface using Java Swing, where I have two rocket icons that players can move using keyboard inputs. The rockets should move in four directions (up, down, left, right) based on the WASD keys for the first rocket and the arrow keys for the second rocket.

However, I've encountered an issue where the arrow keys are not moving the labels (rockets) as expected. The WASD keys work fine for the first rocket, but the arrow keys seem to have no effect on the second rocket's movement.

camickr
  • 321,443
  • 19
  • 166
  • 288
M_T
  • 1
  • 2
  • 1
    Does this answer your question? [Keystroke not working with arrow keys](https://stackoverflow.com/questions/49926109/keystroke-not-working-with-arrow-keys) – jhamon Aug 08 '23 at 14:38
  • 1
    You are using the wrong InputMap. Events are passed to the component with focus. Only the first label will ever have focus. Read the Swing tutorial on [How to Use Key Bindings](https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) for information on the InputMap to use even when a component doesn't have focus. Also, variable names should NOT start with an upper case character. Some names are correct, other are not. Be consistent. – camickr Aug 08 '23 at 15:20
  • 1
    Also note that an event will only be generated for the last key pressed. So you can't have the two rockets move at the same time. Check out the "KeyboardAnimation" example in [Motion Using the Keyboard](https://tips4java.wordpress.com/2013/06/09/motion-using-the-keyboard/) for a solution. – camickr Aug 08 '23 at 15:25
  • 1
    `InputMap arrowsInputLabel2 = label2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);` then use that throughout for that label - don't keep calling `getInputMap` Do the same for the other label. Prefer too `arrowsInputLabel2.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "rightAction2");` as you will get a compiler error with a typo – g00se Aug 08 '23 at 15:27
  • @camickr: Could you assist me with a code snippet? I've tried on my own, but it's not working as I anticipated. I'm new to GUI, so your guidance would be greatly appreciated. Thank you! – M_T Aug 08 '23 at 16:30
  • I've just given you code that will make it work – g00se Aug 08 '23 at 16:34
  • @g00se That worked! Thanks – M_T Aug 08 '23 at 16:52
  • 1
    (1-) @M_T *I'm new to GUI* - this has nothing to do with a GUI, it is about reading a tutorial and/or reading the API. Did you go back to the tutorial/API to understand the difference between the 3 InputMaps? Don't ask for the code. Ask a specific question about the part in the tutorial that you didn't understand. Also, if you looked at the "Motion Using the Keyboard" link you would also have found examples using a different InputMap. – camickr Aug 08 '23 at 18:25

0 Answers0