0

hello, hello! im probably being really dumb again, but i can't get java to run keyPressed() when i press a key. do i need to initialize the keylistener somewhere? any input is appreciated!(even if it's not help)

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

import javax.swing.JPanel;
import javax.swing.Timer;

public class GamePanel extends JPanel implements ActionListener, KeyListener{

    Blocks blocks = new Blocks();
    Player player = new Player();

    public GamePanel(){

        Timer tim = new Timer(50, this);
        tim.start();
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);

    }

    private void update(){

        blocks.update();
        player.update();

    }

    public void paintComponent(Graphics g){

        blocks.paint(g);
        player.paint(g);

    }

    public void actionPerformed(ActionEvent e){

        update();
        repaint();



    }

    // key listeners

    public void keyPressed(KeyEvent e) {

        System.out.println("a down");

    }

    public void keyReleased(KeyEvent e) {



    }

    public void keyTyped(KeyEvent e) {



    }

}

thank you.

2
Contributors
3
Replies
4
Views
4 Years
Discussion Span
Last Post by NormR1
Featured Replies
  • 1

    Has a keylistener been added? Read More

  • 1

    What code do you expect to call the KeyListener methods? You have to tell that code to make the calls when there are key events. That's done by adding a key listener to the Component that has the focus. Take a look at the tutorial: http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html Read More

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.