I don't know why. At all.
Just that it won't repaint!
My code:
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Frame extends JFrame implements KeyListener{
JPanel content;
int W = 200;
int H = 200;
public Frame(){
setSize(700,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
content = new JPanel();
setContentPane(content);
content.paint(getGraphics());
content.addKeyListener(this);
setVisible(true);
}
@Override
public void paint(Graphics g){
g.clearRect(0, 0, getWidth(), getHeight());
g.drawRect(0, 0, W, H);
g.fillRect(0,0,W,H);
}
public static void main(String[] args){
new Frame();
}
public void keyPressed(KeyEvent e) {
int getKey = e.getKeyCode();
if(getKey == KeyEvent.VK_RIGHT){
W++;
}else if(getKey == KeyEvent.VK_LEFT){
W--;
}
if(getKey == KeyEvent.VK_UP){
H--;
}else if(getKey == KeyEvent.VK_DOWN){
H++;
}
repaint();
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}