Clarinetncleats
Clarinetncleats

Reputation: 111

Java KeyListener for class not extending jpanel or something

I'm trying to make a KeyListener in a new data type called Player which implements KeyListner. How could I make Player its own KeyListener (i.e. this.addKeyListner(this)) without implementing or extending anything further? If this is not posssible, what would I want Player to extend/implement to avoid extraneous methods in the class?

Edit: To clarify, I'm not asking how to implement a KeyListener without the the keyReleased, keyPressed, and keyTyped methods. I'm asking how I can make Player add itself as a KeyListener without it being a JPanel, for example.

Upvotes: 0

Views: 1184

Answers (2)

Infinite Recursion
Infinite Recursion

Reputation: 6557

Steps:

  • Create a class which extends JPanel, say MyJPanel.
  • The JPanel class should provide methods to modify it's attributes.
  • Create a separate class which implements KeyListener, say Player.
  • Add an object of Player to the JPanel class.
  • Call the JPanel methods from the Player class on key events, and then repaint.

Code:

The Panel class:

public class MyJPanel extends JPanel {

    private int x = 0;
    private int y = 0;

    public MyJPanel() {
        addKeyListener(new Player(this));
        setFocusable(true);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.GREEN);
        g.fillRect(x, y, 100, 100);
    }

    public static void main(String[] args) {
        MyJPanel player = new MyJPanel();
        player.setSize(200, 200);
        JFrame frame = new JFrame();
        frame.add(player);
        frame.setLayout(null);
        frame.setTitle("Key listener example");
        frame.setSize(500, 500);
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    void incrementX() {
        x++;
        if (x > 500)
            x = 500;
    }

    void decrementX() {
        x--;
        if (x < 0)
            x = 0;
    }

    void incrementY() {
        y++;
        if (y > 500)
            y = 500;
    }

    void decrementY() {
        y--;
        if (y < 0)
            y = 0;
    }
}

The Player class which implements KeyListener

public class Player implements KeyListener {
    MyJPanel panel;

    public Player(MyJPanel myJPanel) {
        this.panel = myJPanel;
    }

    @Override
    public void keyPressed(KeyEvent pKeyEvent) {
        int key = pKeyEvent.getKeyCode();
        if (key == KeyEvent.VK_LEFT) {
            panel.decrementX();
        } else if (key == KeyEvent.VK_RIGHT) {
            panel.incrementX();
        } else if (key == KeyEvent.VK_UP) {
            panel.decrementY();
        } else if (key == KeyEvent.VK_DOWN) {
            panel.incrementY();

        }
        panel.repaint();
    }

    @Override
    public void keyReleased(KeyEvent pKeyEvent) {
    }

    @Override
    public void keyTyped(KeyEvent pKeyEvent) {
    }
}

Upvotes: 0

RossC
RossC

Reputation: 1230

If you implement KeyListener (assuming there is no instance of Abstract), then you must implement the 3 methods. They are not extraneous for the interface, as these are the 3 things that can happen to a key, they may not all be relevant to you but it's only a couple of lines of code to implement them.

void keyPressed(KeyEvent e)

Invoked when a key has been pressed.

void keyReleased(KeyEvent e)

Invoked when a key has been released.

void keyTyped(KeyEvent e)

Invoked when a key has been typed.

There's only the 3 and this is the nature of interfaces.

Upvotes: 1

Related Questions