/**			

*   				          Text Gradient Blinking
*				

*			 Copyright 1996 Gian Luca Farina Perseu - PRS Soft
* 					Version 1.0

*

*/



import java.awt.*;

import java.util.StringTokenizer;


/*

  Text Gradient Blinking. (remotely) Based on 'Blink.class' by  Arthur Van Hoff / Sun Microsystems

 

  author	:	Gian Luca Farina Perseu  
  email 	: 	perseu@comune.torino.it
  www	: 	http://www2.comune.torino.it/~ticgf1

 
 
    PARAMETERS :
		
	message	:	The text of the message 
	speed	:	Fading speed 
	font	:	Type of font. It must be writter excactly as the system need (normally with the 
			first letter capitalized) (default = Arial)
	style 	:	Font style ( "plain" / "bold" / "italic" ) (default = plain)
	size 	:	Font size ( default = 24 )
	align	:	Alignement ( "left" / "center" / "right" ) (default = center)
	bgcolor	:	Background color in Hex format ( es. "#FF88EE" )
	
	
    ------------------------------------------------------------------------------------------------------------------------------
    EXAMPLE :

	<APPLET 
	    CODEBASE="java"
	    CODE="GradBlink.class" 
     	    WIDTH=450 
	    HEIGHT=50>
	
	<PARAM NAME="message" VALUE="InfoCenter - Comune di Torino">
	<PARAM NAME="speed" VALUE="4">
	<PARAM NAME="bgcolor" VALUE="#000080">
	<PARAM NAME="align" VALUE="center">
	<PARAM NAME="font" VALUE="Arial">
	<PARAM NAME="style" VALUE="bold">
	<PARAM NAME="size" VALUE="28">	

	</APPLET>
    ----------------------------------------------------------------------------------------------------------------------------------
*/

public class GradBlink extends java.applet.Applet implements Runnable {

    Thread blinker;

    String lbl, align;

    Font font;

    int iFontStyle, iFontSize, speed, x = 0, y = 0;
    Dimension d;

    CycleColor color;

    boolean bSfondo = false;

	   

    public void init() {

	// Init Vari
	// Init
	color = new CycleColor();
	d = size();	
	
	// Settaggio font ***********************************************************
	// Font Setting
	// ------------------------------------------------------------------ Font Style  ( PARAM NAME="style")
	String att = getParameter("style");
	if (att.equalsIgnoreCase("plain"))
		iFontStyle = Font.PLAIN;
	else if (att.equalsIgnoreCase("bold"))
		iFontStyle = Font.BOLD;
	else if (att.equalsIgnoreCase("italic"))
		iFontStyle = Font.ITALIC;
	else
		iFontStyle = Font.PLAIN;

	// ------------------------------------------------------------------- Font Size  ( PARAM NAME="size")
	att = getParameter("size");
	iFontSize = (att == null) ? 24 : Integer.valueOf(att).intValue();

	// ------------------------------------------------------------------ Font Type  ( PARAM NAME="font")
	att = getParameter("font");
	font = (att == null) ? new java.awt.Font("Verdana", Font.PLAIN,10) : 
			new java.awt.Font(att, iFontStyle, iFontSize);
	
	// Velocita' di fading ( PARAM NAME="speed") ******************************

	// Fading speed
	att = getParameter("speed");

	speed = (att == null) ? 400 : (1000 / Integer.valueOf(att).intValue());
	
	// Stringa da visualizzare  ( PARAM NAME="message") ****************************

	// String to display
	att = getParameter("message");

	lbl = (att == null) ? "Text Gradient" : att;
	
	// Colore di sfondo  ( PARAM NAME="bgcolor")

	// Background color
	Color bg = TransColor(getParameter("bgColor"), getBackground());

	setBackground(bg);
	
	// Allineamento testo ( PARAM NAME="align")
	// Text alignement
	align = getParameter("align");

    }

    

   public void paint(Graphics g) {

	int x=0, y = font.getSize();

	g.setFont(font);
	
	x = StringYPosition(g,lbl);
	
	color.step();

	g.setColor(color.color());

	g.drawString(lbl, x, y);



    }

    // To avoid flickering:
    public void update(Graphics g) {
       paint(g);
    }

    public boolean mouseDown(Event e, int x, int y) {
	String sStr = "";
	showStatus(sStr);
    	return true;
    }



    
   public int StringYPosition(Graphics g, String sStr) {
	
	int iPos = 0,iTotalWidth = 0;
	// g.setFont(font);
	iTotalWidth = g.getFontMetrics().stringWidth(sStr);
	
	// Lunghezza totale della scritta.
	if (iTotalWidth>size().width) 
		iTotalWidth = size().width;
	
	// Verifica il tipo di allineamento 
	// Verify the alignement type	
	if (align.equalsIgnoreCase("left")) 
		iPos = 0;
	else if (align.equalsIgnoreCase("center"))
		iPos = ( size().width - iTotalWidth )/2;
	else if (align.equalsIgnoreCase("right"))
		iPos = ( size().width - iTotalWidth);
	
	return iPos;
    }
    

    public void start() {

	blinker = new Thread(this);

	blinker.start();

    }



    public void stop() {

	blinker.stop();

    }



    public void run() {

	while (true) {

	try {Thread.currentThread().sleep(speed);} catch (InterruptedException e){}

	    repaint();

	}

    }



    public Color TransColor(String aColor, Color aDefault)  {

	if ((aColor == null) || (aColor.charAt(0) != '#') || (aColor.length() != 7 ))  {

		return aDefault;

	}



	try  {

		Integer rgbValue = new Integer(0);

		rgbValue = Integer.valueOf(aColor.substring(1,7), 16);

		return new Color(rgbValue.intValue());

	}

	catch (Exception e)  {

		return aDefault;

	}

    }

  

}	// End of class GradClass





//-----------------------------------------------------------------------
//
// Class:	CycleColor
//
// Purpose:	Instances of this object represent a color that cycles
//		around the color wheel.
//

class CycleColor {  int  MAX_RGB = 255;
		  int  COLOR_CYCLE = 6 * MAX_RGB;
		  int  CYCLE_STEP  = 1;  
		  int  hue;

	public CycleColor(){
		  hue = (int) (Math.random() * COLOR_CYCLE);
	}

	public void step(){
		 hue = (hue + CYCLE_STEP) % COLOR_CYCLE;
	}

	public Color color(){
		  int  r, g, b;

		  r = triad_value(0);
		  g = triad_value(4 * MAX_RGB);
		  b = triad_value(2 * MAX_RGB);
		  return new Color(r, g, b);
	}
	
	private int triad_value(int phase){
		  int  value;

		  value = (hue + phase) % COLOR_CYCLE;
		  if (value > 3 * MAX_RGB)
		  	value = Math.abs(value - COLOR_CYCLE);
		
		  if (value <= MAX_RGB)
			return MAX_RGB;
		  else if (value <= 2 * MAX_RGB)
			return MAX_RGB - (value - MAX_RGB);
		  else    return 0;
	}
	
} /* class CycleColor */


