/*
 * greetings.java - 12 Jun 1996 - Version 2.0
 *(formerly javex.java)
 *
 * Copyright 1996 by Bill Giel
 *
 * E-mail: rvdi@usa.nai.net
 * WWW: http://www.nai.net/~rvdi/home.htm
 *
 *
 * Revision 2.0 - Added background and text colors, and custom text
 * 12 Jun 96      parameters. Overrode reshape to handle resize, (even
 *                though applets cannot be resized in a browser, they
 *                can in the appletviewer.)
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
 * without fee is hereby granted, provided that any use properly credits
 * the author, i.e. "Greetings applet courtesy of <A HREF="mailto:rvdi@usa.nai.net">
 * Bill Giel</A>.
 *
 *
 * 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. THE AUTHOR SHALL NOT BE LIABLE
 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *
 */

import java.applet.*;
import java.awt.*;
import java.net.*;


public class greetings extends Applet
{

    int fontHeight;
    String displayString=null;
    Color bgColor, textColor;

    //Applet name, author and info lines
    public String getAppletInfo()
    {
        return "Greetings Applet (C) 1996 by Bill Giel<rvdi@usa.nai.net>";
    }

    //Users' parameters - self-explanatory?
    public String[][] getParameterInfo()
    {
        String[][] info = {
            {"width",       "int",      "width of the applet, in pixels"},
            {"height",      "int",      "height of the applet, in pixels"},
            {"bgColor",     "string",   "hex color triplet of the background, i.e. FFFFFF for white <white>"},
            {"textColor",   "string",   "hex color triplet of text, i.e. 000000 for black <black>"},
            {"unknown_visitor","string","text to display for unknown host <Welcome!>"},
            {"greetings_text", "string","text to display with host name, no space at end <Greetings to>"}
        };
        return info;
    }    
    

    private Color parseColorString(String colorString)
    {
        if(colorString.length()==6){
            int R = Integer.valueOf(colorString.substring(0,2),16).intValue();
            int G = Integer.valueOf(colorString.substring(2,4),16).intValue();
            int B = Integer.valueOf(colorString.substring(4,6),16).intValue();
            return new Color(R,G,B);
        }
        else return Color.lightGray;
    }


    public void init()
    {

        String mysteryGuest = getParameter("UNKNOWN_VISITOR");
        if(null == mysteryGuest)
            mysteryGuest= "Welcome!";


        displayString = getParameter("GREETINGS_TEXT");
        if(null == displayString)
            displayString="Greetings to";
        

        String param = getParameter("BGCOLOR");
        if(null == param)
            bgColor=Color.white;
        else
            bgColor=parseColorString(param);

        param = getParameter("TEXTCOLOR");
        if(null == param)
            textColor=Color.black;
        else
            textColor=parseColorString(param);

	    try{
			InetAddress thisAddress = InetAddress.getLocalHost();
            displayString += " " + thisAddress.getHostName().toUpperCase() +" !";
 		}catch(Exception e){
		    displayString=mysteryGuest;
		}

        fontHeight=size().height;

        System.out.println(getAppletInfo());
	}

	public void reshape(int x, int y, int width, int height)
	{
	    super.reshape(x,y,width,height);
	    fontHeight=height;
	}

    public void paint(Graphics g)
    {
        FontMetrics fm;

        g.setColor(bgColor);
        g.fillRect(0,0,size().width,size().height);
        g.setColor(textColor);

   		do{
	        g.setFont(new Font("TimesRoman",Font.BOLD,fontHeight));
		    fm=g.getFontMetrics();
		    if(fm.stringWidth(displayString)>size().width)fontHeight-=2;

    	}while(fm.stringWidth(displayString)>size().width);
        g.drawString(displayString,(size().width-fm.stringWidth(displayString))/2,size().height-fm.getMaxDescent());
 	}
}

