How to get keyEvent to work in Java?

7.3k views Asked by At

I'm completely new at programming Java. I can't get my keyEvents to wont work. I need this for a little game I'm making. Here is my code:

package markusrytter.pingpong;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class main extends JPanel implements KeyListener {

    static int ballX;
    static int ballY;
    static int ballR = 15;

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.fillOval(ballX - ballR, ballY - ballR, ballR * 2, ballR * 2);
    }
    public static void main(String[] args) throws InterruptedException {
        JFrame frame = new JFrame("Sample Frame");
        main game = new main();
        frame.add(game);
        frame.setSize(1400, 800);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        ballX = frame.getContentPane().getSize().width / 2;
        ballY = frame.getContentPane().getSize().height / 2;

        while (true) {
            game.repaint();
            Thread.sleep(10);
        }
    }

    public void keyPressed(KeyEvent e) {
        System.out.println("A key is Pressed: " + e.getKeyCode());
        if(e.getKeyCode() == KeyEvent.VK_SPACE){
            System.out.println("Spacebutton pressed");
        }
    }
}

I do hope someone can help, I have tried to watch videos but again, I'm new at java.

2

There are 2 answers

8
ItamarG3 On BEST ANSWER

Most importantly: change the name of your class. It's called main and that's a bad idea. Also it's much more advisable to override paintComponent() instead of paint.

You didn't add the keylistener to the JFrame. You should call this in your main after creating game:

frame.addKeyListener(game);

You also need to add the remaining KeyListener methods.

and that should do it.

0
camickr On
  1. Your code won't compile since you didn't implement KeyListener (completely)

  2. You didn't add the key listener to the panel in its constructor.

  3. KeyEvents are only dispatched to components with focus. Your panel is not focusable so it will never receive events. You use the method setFocusable(true).

  4. Don't override paint(). Instead you should be overriding paintComponent(...).

  5. You shouldn't even be using a KeyListener. Instead when using Swing you should be using Key Bindings.

  6. Don't use static variables for the properties of your class.

  7. Your GUI is not created on the Event Dispatch Thread (EDT).

i have read articles, i have watched videos, and done everything exactly like them,

Obviously not or it would work.

For a proper tutorial start with the Swing Tutorial. There are sections on:

  1. How to Write a KeyListener
  2. Custom Painting
  3. Key Bindings.
  4. Concurrency