import java.awt.*;
import java.awt.event.*;
public class Example2408
extends Frame
implements Runnable
{
//Konstanten
private static final int NUMLEDS = 100;
private static final int SLEEP = 1;
private static final int LEDSIZE = 10;
private static final Color ONCOLOR = new Color(255,0,0);
private static final Color OFFCOLOR = new Color(100,0,0);
//Instanzvariablen
private Thread th;
private int switched;
private int dx;
public static void main(String args[])
{
Example2408 frame = new Example2408();
frame.setSize(1220,150);
frame.setVisible(true);
frame.startAnimation();
}
public Example2408()
{
super("Example2408");
setBackground(Color.lightGray);
//WindowListener
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent event)
{
if (th != null) {
th.stop();
th = null;
}
setVisible(false);
dispose();
System.exit(0);
}
}
);
}
public void startAnimation()
{
th = new Thread(this);
th.start();
}
public void run()
{
switched = -1;
dx = 1;
while (true) {
repaint();
try {
Thread.sleep(SLEEP);
} catch (InterruptedException e){
//nichts
}
switched += dx;
if (switched < 0 || switched > NUMLEDS - 1) {
dx = -dx;
switched += 2*dx;
}
}
}
public void paint(Graphics g)
{
for (int i = 0; i < NUMLEDS; ++i) {
for (int j = 0; j < 10; j++){
g.setColor(i == switched ? ONCOLOR : OFFCOLOR);
g.fillOval(10+i*(LEDSIZE+2),80,LEDSIZE,LEDSIZE);
}
}
}
}