Better Color Swirl Applet
// this applet displays a message in a contantly changing color
// update is overridden to reduce flicker by not redrawing the background
import java.awt.*;
public class BetterColorSwirl extends java.applet.Applet
implements Runnable
{
Font f = new Font("TimesRoman",Font.BOLD,48);
Color colors[] = new Color[50];
Thread runThread;
// initialize the color array
public void init()
{
float c = 0;
for (int i = 0 ; i < colors.length ; i++) {
colors[i] =
Color.getHSBColor(c,(float)1.0,(float)1.0);
c += 0.02;
}
}
public void start()
{
if (runThread == null)
{
runThread = new Thread(this);
runThread.start();
}
}
public void stop()
{
if (runThread != null)
{
runThread.stop();
runThread = null;
}
}
// print the message while cycling through the colors
public void run()
{
int i = 0;
while (true)
{
setForeground(colors[i]);
repaint();
i++;
try
{
Thread.sleep(50);
} catch (InterruptedException e) {}
if (i == colors.length)
i = 0;
}
}
// override update so that it doesn't repaint the background
public void update(Graphics g)
{
paint(g);
}
// set the font and draw the string in the current color
public void paint(Graphics g)
{
g.setFont(f);
g.drawString("The Color Cycle",50,75);
}
}