Original Post
Ok im trying to get the rectangle to move down when the down key is pushed, ive done this before, with an ActionListener also but its not working now which must mean i have a simple little error somewhere that i cannot catch, help is appreciate thanks!
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class CKS extends Applet implements Runnable, KeyListener
{
int y=0;
Thread th;
public void init()
{
setBackground(Color.blue);
th = new Thread(this);
th.start();
}
public void run()
{
while(true)
{
repaint();
try{
th.sleep(1000/300);
}
catch(InterruptedException e)
{
}
}
}
public void paint(Graphics g)
{
g.drawRect(0,y,10,20);
}
public void keyPressed(KeyEvent evt)
{
int key = evt.getKeyCode();
if(key == KeyEvent.VK_DOWN)
{
y++;
repaint();
}
}
public void keyReleased(KeyEvent evt)
{
}
public void keyTyped(KeyEvent evt)
{
}
}