Hi,
i'm starting studing java, i have found a tutorial on youtube where explain to intercept pressions from Keyboard. I try to do this but seems that's something wrong (made by me) somewhere but i can't find the problem..
this is the code:
import java.awt.Graphics; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JFrame; public class JavaGame extends JFrame { int x; int y; public class AL extends KeyAdapter{ public void KeyPressed(KeyEvent e){ int KeyCode = e.getKeyCode(); if (KeyCode == e.VK_UP){ y--; System.out.println(y); } if (KeyCode == e.VK_DOWN){ y++; System.out.println(y); } if (KeyCode == e.VK_LEFT){ x--; System.out.println(x); } if (KeyCode == e.VK_RIGHT){ x++; System.out.println(x); } } public void KeyReleased(KeyEvent e){ } public void paint (Graphics g){ g.fillOval(x, y, 15, 15); repaint(); } } public JavaGame(){ addKeyListener(new AL()); setTitle("JavaGame"); setSize(400,400); setResizable(false); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); x = 150; y = 150; } public static void main(String[] args) { new JavaGame(); } }
with this code i make a window, then with keybord i should paint on this window an oval that follow up down left right buttons on keyboard.
when running, the code write the window, but there isn't interception of keys...