Tuesday, June 14, 2011

Color Swirl in Both background and foreground

import java.awt.Font;
import java.awt.Graphics;
import java.util.Date;
import java.awt.Color;

public class ColorSwirl extends java.applet.Applet implements Runnable {

Font swirlFont = new Font("Arial", Font.BOLD, 24 );
Color[] colors = new Color[50];
Thread runner;

public void start() {
if (runner == null) {
runner = new Thread(this);
runner.start();
}

}

public void stop() {
if (runner != null) {
runner.stop();
runner = null;
}

}

public void update(Graphics g){
paint(g);
}

public void run() {

// Initialize the color array
float c = 0;
for (int i=0; i < colors.length; i++){
colors[i] = Color.getHSBColor(c, (float) 1.0, (float) 1.0);
c = c + 0.02f;
}

// Cycle through the colors
int j = 0;
while (true) {
setBackground(colors[49-j]);
setForeground(colors[j]);
repaint();
j++;
try { Thread.sleep(100); }
catch (InterruptedException e) {}
if (j == colors.length) j = 0;

}
}

public void paint (Graphics g) {
g.setFont(swirlFont);
g.drawString("I love my dear DaXiongXiong", 15, 50);
}

}

No comments:

Post a Comment