Repaint
Next
Applet
Java Source
import java.awt.*;
import java.applet.*;
public class Checkers extends Applet implements Runnable {
Thread runner;
int xpos;
public void start() {
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}
public void stop() {
if (runner != null) {
runner.stop();
runner = null;
}
}
public void run() {
setBackground(Color.blue);
while (true) {
for (xpos=5 ; xpos <=105; xpos+=4) {
repaint();
try { runner.sleep(50); } catch (InterruptedException e) {}
}
for (xpos=105 ; xpos > 5; xpos -= 4) {
repaint();
try { runner.sleep(50); } catch (InterruptedException e) {}
}
}
}
public void paint(Graphics g) {
g.setColor(Color.black);
g.fillRect(0,0,100,100);
g.setColor(Color.white);
g.fillRect(101,0,100,100);
g.setColor(Color.red);
g.fillOval(xpos,5,90,90);
}
}