/* dnynamicText by Nick Heinle *********************
 * This Code is Copyright (c) 1996 by Nick Heinle  * 
 * - setText(String text), changes the text        *
 * - setXY(int x, int y), changes the x,y coords   *
 * - setCenter(), toggles the centering of the text*
 * - setfColor(String col) change foreground color *
 * - setfSize(int size) change font size           *
 * Use with LiveConnect for best results :)        *
 * Last Mod on 12/6/96 using JDK 1.0.2             *
 ***************************************************/

import java.awt.*;
import java.lang.*;
import java.util.*;

public class dynamictext extends java.applet.Applet implements Runnable {

    Thread ticking;
    Color fcolor;
    Color bgcolor;
    Font curFont;
    FontMetrics curMets;
    String curText = "";
    String lstText = "";
    String fface;
    int fsize;
    int lst_fsize;
    int fcenter;
    int x_pos;
    int y_pos;
    int lst_x_pos;
    int lst_y_pos;

    /* start thread (in this case it's somewhat pointless) */

    public void start() {
        if (ticking == null) {
        ticking = new Thread(this);
        ticking.start();
        }
    }

    public void stop() {
        if (ticking != null) {
        ticking.stop();
        ticking = null;
        }
    }

    /* Grab parameters, font face, color, center, etc. */

    public void init() {
    x_pos = Integer.parseInt(getParameter("x_pos"));
    y_pos = Integer.parseInt(getParameter("y_pos"));
    lst_x_pos = x_pos;
    lst_y_pos = y_pos;
    fcenter = Integer.parseInt(getParameter("center"));
    fcolor = convColor(getParameter("fontcolor"), getForeground());
    bgcolor = convColor(getParameter("bgcolor"), getBackground());
    setBackground(bgcolor); 
   
    fface = getParameter("fontface");
    fsize = Integer.parseInt(getParameter("fontsize"));
    lst_fsize = fsize;
    curFont = new Font (fface, Font.PLAIN, fsize);
    }

    /* When loaded, display copyright and let things go JavaScript from there */

    public void run() {
    getAppletContext().showStatus("dynamicText 1.1, Copyright (c) 1996 Nick Heinle");
        try { Thread.sleep(2000); }
        catch (InterruptedException e) {}
        getAppletContext().showStatus("");
    }

    /* Allow JavaScript to set the text to be displayed */

    public void setText(String text) {
    lstText = curText;
    curText = text;
    repaint();
    }

    /* Allow JavaScript to set the color of the font */

    public void setfColor(String col) {
    fcolor = convColor(col, getForeground());
    repaint();
    }

    /* Allow JavaScript to set the size of the font */

    public void setfSize(int size) {
    lst_fsize = fsize;
    fsize = size;
    curFont = new Font (fface, Font.PLAIN, fsize);
    repaint();
    }

    /* Allow JavaScript to set the exact X and Y coords of the text */

    public void setXY(int x, int y) {
    lst_x_pos = x_pos;
    lst_y_pos = y_pos;
    x_pos = x;
    x_pos = y;
    repaint();
    }

    /* Allow JavaScript to toggle centering on/off */

    public void setCenter() {
    if (fcenter == 1) fcenter = 0;
    else fcenter = 1;
    repaint();
    }

    /* Center text using fontMetrics, half screen height and width */

    public void textCenter() {
    lst_x_pos = x_pos;
    lst_y_pos = y_pos;
    curMets = getFontMetrics(curFont);
    x_pos = (size().width - curMets.stringWidth(curText)) / 2;
    y_pos = (size().height + curMets.getHeight()) / 2;
    }
    
    /* Convert string to color object */

    public Color convColor (String col, Color colDef) {
    int r, g, b;
    StringTokenizer st = new StringTokenizer(col, ",");
        try {
        r = Integer.valueOf(st.nextToken()).intValue();
        g = Integer.valueOf(st.nextToken()).intValue();
        b = Integer.valueOf(st.nextToken()).intValue();
        return new Color(r, g, b);
        }
    catch (Exception e) { return colDef; }
    }

    public void paint(Graphics g) {
        if (fcenter == 1) textCenter();

    /* Erase the old text using last coords, disabled temporarily
       untill I find a better fix.

    curFont = new Font (fface, Font.PLAIN, lst_fsize + 1);
    g.setFont(curFont);
    g.setColor(bgcolor);
    g.drawString(lstText, lst_x_pos, lst_y_pos);
     
    */

    /* Display the new text */

    curFont = new Font (fface, Font.PLAIN, fsize);
    g.setFont(curFont);
    g.setColor(fcolor);
    g.drawString(curText, x_pos, y_pos);
    }

    /*public void update(Graphics g) {
    paint(g);
    }*/

/*
    public boolean mouseDown(java.awt.Event evt, int x, int y) {
    }
*/

}
