Du ændrer ikke koordinaterne for kassen så du tegner det samme sted hele tiden.
Prøv den her:
import java.awt.*;
import java.applet.*;
public class Test extends Applet
{
int n;
int x = 0;
int y = 0;
int moveX = 1;
int moveY = 0;
int w = 50;
int h = 50;
Thread thread;
public void start()
{
thread = new Thread(new Runnable() {
public void run() {
while(true) {
x += moveX;
y += moveY;
if(moveX == 1 && x == (getSize().width - w)) {
moveX = 0;
moveY = 1;
}
else if(moveX == -1 && x == 0) {
moveX = 0;
moveY = -1;
}
else if(moveY == 1 && y == (getSize().height - h)) {
moveY = 0;
moveX = -1;
}
else if(moveY == -1 && y == 0) {
moveY = 0;
moveX = 1;
}
repaint();
try {
Thread.sleep(10);
} catch (Exception e) {
}
}
}
});
thread.start();
}
public void paint(Graphics g)
{
//Nulstil baggrund
g.setColor(Color.white);
g.fillRect(0,0,getSize().width,getSize().height);
g.setColor(Color.black);
g.fillRect(x,y,w,h);
}
public void update(Graphics g)
{
paint(g);
}
}