Ciao a tutti. Sto cercando di creare un semplicissimo programma che in base ai tasti direzionali della tastiera, mi sposta il quadrato sulla finestra. Il problema è che il quadrato rimane sempre fermo...
codice:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Move extends JPanel
{
private int xCoord = 60;
private int yCoord = 60;
public Move()
{
addKeyListener
(
new KeyAdapter()
{
public void keyPressed(KeyEvent event)
{
if (event.getKeyCode() == 37)
{
xCoord--;
}
else if (event.getKeyCode() == 38)
{
yCoord--;
}
else if (event.getKeyCode() == 39)
{
xCoord++;
}
else if (event.getKeyCode() == 40)
{
yCoord++;
}
repaint();
}
}
);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.fillRect(xCoord,yCoord,60,60);
}
public static void main(String[] args)
{
Move panel = new Move();
JFrame frame = new JFrame("Move");
frame.setSize(400,250);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setVisible(true);
}
}