import java.applet.*;
import java.awt.*;

public class Smooth extends Applet implements Runnable {
static final int deltax = 4;
static final int deltay = 2;
String message;
int x=0;
int y=0;
Color c1= new Color(0x0000ff);
Color c2= new Color(0xffffff);
Font font =  new Font("Helvetica", Font.BOLD,24);
Image offscreen;
int imagewidth, imageheight;
int stringwidth,stringheight,stringascent;
Thread animator = null;
boolean please_stop=false;

public void init(){
FontMetrics fm= this.getFontMetrics(font);
String message = this.getParameter("Text");
stringwidth=fm.stringWidth(message);
stringheight=fm.getHeight();
stringascent=fm.getAscent();
}

public void start() {
animator = new Thread(this);
animator.start();
}

public void stop() {
if(animator != null) animator.stop();
animator=null;
}

public boolean mouseDown(Event e, int x, int y) {
if(animator != null) please_stop=true;
else {please_stop =false;start(); }
return true;
}

void drawBackground(Graphics gr, Color c1, Color c2, int numsteps) {

int r,g,b;
int dr = (c2.getRed() - c1.getRed())/numsteps;
int dg = (c2.getGreen() - c1.getGreen())/numsteps;
int db = (c2.getBlue() - c1.getBlue())/numsteps;
Dimension size = this.size();
int w = size.width, h= size.height;
int dw = size.width/numsteps;
int dh = size.height/numsteps;
gr.setColor(c1);
gr.fillRect(0,0,w,h);
for (r=c1.getRed(), g=c1.getGreen(), b = c1.getBlue(); h>0; h-=dh, w-=dw, r+=dr, g+=dg, b+=db) {
	gr.setColor(new Color(r,g,b));
	gr.fillArc(-w, -h, 2*w, 2*h, 0, -90);
	}
}

public void paint(Graphics g) {
drawBackground(g,c1,c2,25);
g.setColor(Color.black);
g.setFont(font);
g.drawString(message,x,y);
}

public void run() {
while(!please_stop) {
Dimension d=this.size();
if(offscreen == null) || ((imagewidth !=d.width) || (imagheight != d.height))){
offscreen = this.createImage(d.width, d.height);
imagewidth = d.width;
imageheight = d.height;
}

Rectangle oldrect = new Rectangle(x,y-stringascent,stringwidth,stringheight);
x=((x+deltax)%d.width);
y=(y+deltay)%d.height);

Rectangle newrect = new Rectangle(x,y-stringascent,stringwidth,stringheight);
Rectangle r = newrect.union(oldrect);

Graphics g=offscreen.getGraphics();
g.clipRect(r.x,r.y,r.width,r.height);
g.drawImage(offscreen,0,0,this);

try Thread.sleep(100);catch (InterruptedException e);
}
animator = null;
}
}


