//
//  rctext.java -- rolling color text demo by Y.Shinjo <yas@is.tsukuba.ac.jp>
//  Start: 1997/06/18 17:45:00
//

import java.awt.* ;

public class rctext extends java.applet.Applet implements Runnable 
{
    Thread thread ;
    int fontsize = 24 ;
    Font font ;
    Color color[] ;
    int sleep = 100 ;
    int step = 5 ;
    int x, y, n ;
    String message ;

    public void init()
    {
	font = new java.awt.Font("TimesRoman",Font.PLAIN, fontsize );
	message = getParameter("message");
	if( message == null )
	    message="null" ;
	x = 0 ; y = 0 ; n = 0 ;
	color = new Color[6] ;
	color[0] = Color.green ;
	color[1] = Color.yellow ;
	color[2] = Color.orange ;
	color[3] = Color.red ;
	color[4] = Color.magenta ;
	color[5] = Color.blue ;
    }

    public void paint(Graphics g)
    {
	g.setFont( font );
	g.setColor( getBackground() );
	g.drawString( message, x, y );
	x += step ;
	if( x > size().width )
	{
	    FontMetrics fm = g.getFontMetrics();
	    x = - fm.stringWidth( message );
	}
	y += step ;
	if( y > size().height + fontsize )
	{
	    y = 0 ;
	    n ++ ;
	    if( n>= color.length )
	        n = 0 ;
	}
    	g.setColor( color[n] );
	g.drawString( message, x, y );
    }

    public void start()
    {
	thread = new Thread(this);
	thread.start();
    }

    public void stop()
    {
	thread.stop();
    }

    public void run()
    {
	while(true)
	{
	    try
	    {
		Thread.currentThread().sleep( sleep ) ;
	    }
	    catch( InterruptedException e )
	    {
		stop();
	    }
	    repaint();
	}
    }
}
