/*  Glenn Richard, Center for High Pressure
    Research, SUNY, Stony Brook.
    May, 1996
*/
import java.applet.*;
import java.awt.*;

public class JavaCentral extends Applet  implements Runnable
{
  	String str = null;
  	int direction = 1; // 1 is clockwise, -1 is counterclockwise
  	int horizontalRadius = 10;
  	int verticalRadius = 10;
  	Thread runner = null;
  	char theChars[];
  	int phase = 0;
  	Image offScreenImage;
  	Graphics offScreenG;
  	private boolean done;

	public void init()
	{
  		//NOTE:  You can add text, direction, horizontalRadius, verticalRadius
  		//to the HTML file and retrieve here as parameters
/*  		String paramStr = null;
  		str = getParameter ("text");
  		paramStr = getParameter ("direction");
  		if (paramStr != null)
    			direction = Integer.parseInt (paramStr);
  		paramStr = getParameter ("horizontalRadius");
  		if (paramStr != null)
    			horizontalRadius = Integer.parseInt (paramStr);
  		paramStr = getParameter ("verticalRadius");
  		if (paramStr != null)
    			verticalRadius = Integer.parseInt (paramStr); */
    			
  		setBackground (Color.black);
  		setFont (new Font ("Serif", Font.BOLD, 36));
  		if (str == null)
  		{
			str = "Java Central";
  		}
  		resize(30 + 25 * str.length() + 2 * horizontalRadius, 80 + 2 * verticalRadius);

  		theChars =  new char [str.length()];
  		str.getChars (0, str.length(), theChars, 0);
  		
  		Dimension d = getSize();
  		int w = d.width;
  		int h = d.height;
  		offScreenImage = createImage(w, h);
  		
  		offScreenG = offScreenImage.getGraphics();
  		offScreenG.setFont(new Font("Serif",Font.BOLD,36));
 	}

	public void start()
	{
			done = false;
    			runner = new Thread (this);
    			runner.start();
 	}

	public void stop()
	{
  		done = true;
	}

	public void run()
	{
  		while (runner != null)
  		{
			try
			{
	  			Thread.sleep(120);
			}
			catch (InterruptedException e) { }
			repaint();
  		}
	}

  	public void update (Graphics g)
  	{
    		int x, y;
    		double angle;
    		offScreenG.setColor (Color.black);
    		
    		Dimension d = getSize();
    		int w = d.width;
    		int h = d.height;
    		offScreenG.fillRect (0, 0, w, h);
    		
    		phase += direction;
    		phase %= 8;
    		for (int i=0; i<str.length(); i++)
    		{
  	  		angle = ((phase - i * direction) % 8) / 4.0 * Math.PI;
	  		
	  		// Horizontal motion
	  		x = 20 + 25 * i + (int) (Math.cos(angle) * horizontalRadius); 
	  		
	  		// Vertical motion
	  		y = 60 + (int) (Math.sin(angle) * verticalRadius); 
      			
      			if (i == 0 || theChars[i-1] == ' ')  // Each word starts in blue
        				offScreenG.setColor (Color.blue);
      			else
        				offScreenG.setColor (Color.red);  // Each word continues in red
	  		
	  		offScreenG.drawChars (theChars, i, 1, x, y);
		}
    		paint(g);
  	}

 	 public void paint(Graphics g)
 	{
    		g.drawImage (offScreenImage, 0, 0, this);
  	}
}
