Bobby C. Robillard
Bobby C. Robillard

Reputation: 157

How can I get Animation to run with KeyBoard input in JAVA?

today's problem rattling my mind is this, I'm trying to draw a red rectangle that moves whenever the user presses space. The problem is that whenever I press space the rectangle doesn't move. Any ideas why and how I could fix this problem?

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;

public class Animation extends JPanel implements ActionListener, KeyListener{

    int x = 0, y = 0, velx=0;

    public Animation(){
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }

    public static void main(String args[]){
        JFrame frame = new JFrame("Animation Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setSize(500, 500);
        Animation a = new Animation();
        frame.add(a);

    }

    public void paintComponent(Graphics g){
    super.paintComponent(g);
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, 500, 500);
        g.setColor(Color.RED);
        g.fillRect(x, 50, 30, 20);
        }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        x = x + velx;
        repaint();
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub
        int keyCode = e.getKeyCode();
        if(keyCode == e.VK_SPACE){
            velx = 2;
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }

}

Upvotes: 3

Views: 874

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347214

The problem is KeyListener is just a pain in the ... code.

It will only respond to key events when the component it is registered to is focusable AND has keyboard focus. The problem is, focus changes and it's a pain to manage and control.

Instead, you should be using the Key Bindings API which was designed to help solve this problem

There are plenty of examples of key bindings on SO, maybe something like this might help

Upvotes: 3

Sajeev
Sajeev

Reputation: 848

Made the following changes to your code. Pressing back space should now move the rectangle.

  1. Add the KeyListener to JFrame.
  2. Changes to KeyBoard event listener method.

CODE:

public class Animation extends JPanel implements ActionListener, KeyListener{

int x = 0, y = 0, velx=0;

public Animation(){
    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);
}

public static void main(String args[]){
    JFrame frame = new JFrame("Animation Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setSize(500, 500);
    Animation a = new Animation();
    frame.add(a);
    frame.repaint();
    frame.addKeyListener(a);

}

protected void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, 500, 500);
    g.setColor(Color.RED);
    g.fillRect(x, 50, 30, 20);
 }

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    x = x + velx;
}

@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    int keyCode = e.getKeyCode();
    if(keyCode == e.VK_SPACE){
        velx = 2;
        x = x + velx;
    }
    repaint();
}

@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

}

}

Upvotes: 1

Related Questions