/*
**	Program:		DocumentDate
**	Version:		1.0.1
**	Author:		    Dario de Judicibus
**	Address:		dejudicibus@geocities.com
**	Created on:		December 22nd, 1997
**	Modified on:	January 22nd, 1998
**	Language:		Java (jdk 1.1.3) VisualCafé 2.0
**	-------------------------------------------------------------
**	(c) 1998 - All rights reserved
**	-------------------------------------------------------------
**	The author makes no representations or warranties about the suitability
**	of the software, either express or implied, including but not limited
**	to the implied warranties of merchantability, fitness for a
**	particular purpose, or non-infringement. He will not be liable for
**	any damages suffered by licensee as a result of using, modifying or
**	distributing this software or its derivatives.
*/

/**
 *	@author Dario de Judicibus
 *	@version 1.0.1
 *	@see jdk 1.1.3
 *	@see Applet
 */

/**
 *	<B>AppletTempale</B> is a basic template that can be used to
 *  develop reusable applets. It includes all the recommended
 *  methods for applets intended for distribution. It also shows
 *  how to use javadoc tags in source code.
 */
import java.applet.Applet ;
import java.awt.* ;         // Needed ONLY if AWT class used

public class AppletTemplate extends Applet implements Runnable
{
	// --- CLASS VARIABLES ---
	private static String   cvPrefix = "user" ;

	// --- INSTANCE VARIABLES ---
	Thread ivAppletThread ;

    private String    ivText = null ;             // text to display
    private String    ivString = "domain.net" ;   // variable part of text
    private int       ivInteger = 42 ;            // insets
    private char      ivCharacter = '@' ;         // separator of text
    private Color     ivColor = Color.white ;

	// --- METHOD : GETAPPLETINFO ---
	/**
	 *	Return information about this applet
	 *	@return Applet purpose and copyright notice
	 */
	public String getAppletInfo( )
	{
		String info = "AppletTemplate\n"
				+ "\tis a basic template for reusable applets\n"
				+ "\tCopyright (C) 1998 Dario de Judicibus\n"
				;

		return info ;
	}

	// --- METHOD : GETPARAMETERINFO ---
	/**
	 *	Return information about this applet's parameters
	 *	@return Applet parameter list: names, types, and descriptions
	 */
	public String[][] getParameterInfo( )
	{
		String[][] info =
			{
				{ "aString"    , "string"   , "just a string of text - def: domain.net" }
			,	{ "anInteger"  , "integer"  , "just an integer - def: 42" }
			,	{ "aCharacter" , "character", "just a character - def: @" }
			,	{ "aColor"     , "color"    , "just a color - def: white" }
			} ;
		return info ;
	}

	// --- METHOD : INIT ---
	/**
	 *	Initialize applet:
	 *	<UL>
	 *	<LI>read parameters, and set variables' values
	 *	<LI>set value of the string to display
	 *	<LI>resize applet to preferred size
	 *	</UL>
	 */
	public void init( )
	{
		/*
		**  READ PARAMETERS FROM <APPLET> TAG
		*/
        String string = null ;

		// Default string is STRING
		string = getParameter( "aString" ) ;
		if ( string != null ) ivString = string ;

        string = getParameter( "anInteger" ) ;
		if ( string != null ) ivInteger = Integer.parseInt( string ) ;

		string = getParameter( "aCharacter" ) ;
		if ( string != null ) ivCharacter = string.charAt( 0 ) ;

		string = getParameter( "aColor" ) ;
		if ( string != null ) ivColor = decodeColor( ivColor, string ) ;

        ivText = cvPrefix + ivCharacter + ivString ;

		// ***************************
		// *** HERE GOES YOUR CODE ***
		// ***************************

		setSize( getPreferredSize( ) ) ;
	}

	// --- METHOD : START ---
	/**
	 *	Create a new thread, if none, and start it
	 */
	public void start( )
	{
	    /*
		 *  Start a new thread for this applet, so that page does
		 *  not wait for this applet being loaded
		 */
		if ( ivAppletThread == null )
		{
			ivAppletThread = new Thread( this );
			ivAppletThread.start( ) ;
		}
    }

	// --- METHOD : STOP ---
	/**
	 *	Stop thread
	 */
	public void stop( )
	{
		if ( ivAppletThread != null )
		{
			ivAppletThread.stop( ) ;
		}
    }

	// --- METHOD : RUN ---
	/**
	 *	Do nothing but run the thread
	 */
	public void run( )
	{
	}

	// --- METHOD : PAINT ---
	/**
	 *	Paint applet, use color for background, print string,
	 *  character and integer.
	 *	@param g Graphics instance for painting operations
	 */
	public void paint( Graphics g )
	{
        int x = getLocation( ).x ;
		int y = getLocation( ).y ;
		int w = getSize( ).width ;
		int h = getSize( ).height ;

		FontMetrics fm = g.getFontMetrics( ) ;

		// Set paper and pen color
		// String is rendered white on black
		g.setColor( ivColor ) ;
		g.fillRect( x, y, w, h ) ;
		g.setColor( Color.black ) ;

		Point origin = stringLocation( w, h, ivText ) ;
		g.drawString( ivText, origin.x, origin.y ) ;
	}

    // --- METHOD : PREFERREDSIZE ---
	/**
	 *	Calculate the preferred size for this applet.
	 *	That size ensure that the whole string be enclosed inside the
	 *	applet bounds, taking in account insets too.
	 *	@return Preferred size
	 */
	public Dimension getPreferredSize( )
	{
        Graphics g = getGraphics( ) ;
		FontMetrics fm = g.getFontMetrics( ) ;

		int strWidth ;

		try
		{
			strWidth = fm.stringWidth( ivText ) ;
		}
		catch( NullPointerException exc )
		{
			strWidth = 0 ;
		}

        // Use ivInteger for insets
		int w = ivInteger + strWidth + ivInteger ;
		int h = ivInteger + fm.getHeight( ) + ivInteger ;

		return new Dimension( w, h ) ;
	}

	// --- METHOD : DECODECOLOR ---
	/**
	 *	Decode a color string whose format must be "#RRGGBB"
	 *	@param color Default color if string cannot be decoded
	 *	@param colorString String to decode
	 *	@return Decoded color
	 */
	private Color decodeColor( Color color, String colorString )
	{
		Color test ;

		try
		{
			test = Color.decode( colorString ) ;
		}
		catch( NumberFormatException exc )
		{
			test = color ;
		}

		return test ;
	}

	// --- METHOD : STRINGLOCATION ---
	/**
	 *	Calculate string location so that string is centered within
	 *	applet's bounds. Note: applet width and height are passed
	 *	since this method could be called to calculate the new string location
	 *	<B>before</B> applet is resized.
	 *	@param w Applet width to use to center string
	 *	@param h Applet height to use to center string
	 *	@return A point representing the string location
	 */
	private Point stringLocation( int w, int h, String string )
	{
		Graphics g = getGraphics( ) ;
		FontMetrics fm = g.getFontMetrics( ) ;

		int strWidth ;

		try
		{
			strWidth = fm.stringWidth( string ) ;
		}
		catch( NullPointerException exc )
		{
			strWidth = 0 ;
		}

		int x = ( w / 2 ) - ( strWidth / 2 ) ;
		int y = ( h / 2 ) + ( fm.getAscent( ) / 2 ) ;
		return new Point( x, y ) ;
	}
}
