import java.applet.Applet;
import java.awt.*;
public class RainbowText extends Applet
implements Runnable
{
String str;
int strlen;
Thread runner;
char theChars[];
int charOffsets[];
Color colors[];
int phase;
Image offScreenImage;
Graphics offScreenG;
Font f;
FontMetrics fm;
public void init()
{
int i = 20;
str = getParameter("text");
if(str == null)
{
str = "Museum of Java Applets";
}
f = new Font("TimesRoman", 1, 36);
fm = getFontMetrics(f);
resize(40 + fm.stringWidth(str), 40);
setBackground(Color.black);
strlen = str.length();
theChars = new char[strlen];
charOffsets = new int[strlen];
str.getChars(0, strlen, theChars, 0);
colors = new Color[strlen];
for(int j = 0; j < strlen; j++)
{
float f1 = (float)j / (float)strlen;
colors[j] = new Color(Color.HSBtoRGB(f1, 1.0F, 1.0F));
charOffsets[j] = i;
i += fm.charWidth(theChars[j]);
}
offScreenImage = createImage(size().width, size().height);
offScreenG = offScreenImage.getGraphics();
offScreenG.setFont(f);
}
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()
{
while(runner != null)
{
try
{
Thread.sleep(100L);
}
catch(InterruptedException _ex) { }
repaint();
}
}
public void update(Graphics g)
{
offScreenG.setColor(Color.[b]black[/b]);
offScreenG.fillRect(0, 0, size().width, size().height);
phase++;
phase %= str.length();
for(int j = 0; j < strlen; j++)
{
int i = charOffsets[j];
offScreenG.setColor(colors[(phase + j) % strlen]);
offScreenG.drawChars(theChars, j, 1, i, 30);
}
paint(g);
}
public void paint(Graphics g)
{
g.drawImage(offScreenImage, 0, 0, this);
}
public RainbowText()
{
}
}