I'm trying to implement a simple keylistener to make a graphic of an oval move with the left key arrow. The oval does not move nor even read the key press after testing with system.out.println. Any help would be appreciated.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Game extends JFrame implements KeyListener{
int x = 450;
int y = 600;
public Game() {
setTitle("Game");
setSize(1024, 768);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.fillOval(x, y, 100, 100);
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
x = x + 20;
System.out.println("works");
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public static void main(String[] args) {
Game game = new Game();
}
}