Search code examples
javakeyboard

Making a picture change when a key is pressed


I currently have my keyboard buttons working. I was wondering if I can switch between 2 pictures when I press my directional keys. My pictures are put in a JLabel called thing. The JLabel is moving and all I need to do is make it so the pictures alternate when a key is pressed.

class start {

start() {
    JFrame mainFrame = new JFrame();
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.setSize(1210, 700);
    mainFrame.setLocation(new java.awt.Point(150, 30));
    mainFrame.setLayout(null);
    mainFrame.setFocusable(true);
    mainFrame.setFocusTraversalKeysEnabled(true);
    mainFrame.setIconImage(new ImageIcon("images\\sword.png").getImage());
    JLabel thing = new JLabel();
    thing.setIcon(new ImageIcon("image\\walkdown.png"));
    thing.setBounds(300, 300, thing.getPreferredSize().width, thing.getPreferredSize().height);

    InputMap inputMap = thing.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap actionMap = thing.getActionMap();

    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false), "move.up");
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0, false), "move.up");
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false), "move.down");
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, false), "move.down");
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false), "move.left");
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, false), "move.left");
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), "move.right");
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), "move.right");

    actionMap.put("move.up", new ThingAction(thing, new Point(0, -5)));



    actionMap.put("move.down", new ThingAction(thing, new Point(0, 5)));
    actionMap.put("move.left", new ThingAction(thing, new Point(-5, 0)));
    actionMap.put("move.right", new ThingAction(thing, new Point(5, 0)));

    mainFrame.add(thing);
    mainFrame.setVisible(true);
}

public class ThingAction extends AbstractAction {

    private JLabel thing;
    private Point delta;

    public ThingAction(JLabel thing, Point delta) {
        this.thing = thing;
        this.delta = delta;
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        thing.setLocation(thing.getX() + delta.x, thing.getY() + delta.y);
    }
}

}


Solution

  • I solved this problem by using a slightly different way. I think it might be less efficient but it works. i created a

    void keylisteners() {
            MainFrame.addKeyListener(new java.awt.event.KeyListener() {
                public void keyPressed(KeyEvent e) {
                    if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
                        if (walking == true && direction != 0) {
                            MT.cancel();
                        }
    
    public void keyTyped(KeyEvent e) {
                }
    
                public void keyReleased(KeyEvent e) {
                    if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
                        if (direction == 0) {
                            thing.setIcon(new ImageIcon("images\\walk0b.png"));
                            MT.cancel();
                        }
                    }
    

    so when the key is presed a image pops up and changes when its relised. i also direction with is a variable that I created to change inbetween the picture to make it like the cahrecter is walking by ulternating between the pics.