
package com.hls.Uhrzeit;

import java.applet.Applet;
import java.awt.*;
import java.util.Date;

public class Uhrzeit extends Applet
    implements Runnable
{
    Thread thread;
    long msec=1000;

    Date cur_date=new Date();

    Date d;
    Font f;
    
    Color fcol=Color.red;
    Color bcol=Color.blue;

    int w_cur;
    int h_cur;
    
    int posx=2;
    int posy=14;
    
    int first_time=1;
    
    public Color ColorCreateHtml(String s,Color def) {
       if(s!=null && s.length()==7) {
          int i=Integer.parseInt(s.substring(1),16);
          return new Color(i);
       } 
       return def;
    }

    public void init()
    {
        String background = getParameter("Hintergrund");
        bcol=ColorCreateHtml(background,Color.black);
        setBackground(bcol); 
        
        String foreground = getParameter("Vordergrund");
        fcol=ColorCreateHtml(foreground,Color.white);

        String s = getParameter("MilliSec");
        if(s!=null) {
           msec=Long.parseLong(s);
        }   
        String sart = getParameter("Schriftart");
        if(sart==null) {
           sart="SansSerif"; 
        }
        String sg = getParameter("Schriftgröße");
        if(sg==null) {
           sg="14"; 
        }        
        int isg=Integer.parseInt(sg);     
        f = new Font(sart, 1, isg);
//        f = new Font("TimesRoman", 1, 14);
    }
    
    public String intRetStrCode(int i,int w)
    {
        String s= "0000000000" + Integer.toString(i);
        int start=s.length()-w;
        s = s.substring(start,start+w);
        return s; 
    }

    public void paint(Graphics g)
    {
        Dimension di=getSize();
        int w=di.width;
        int h=di.height;
        Date d = cur_date;
        String s= intRetStrCode(d.getHours(),2) + ":" + 
                  intRetStrCode(d.getMinutes(),2) + ":" +
                  intRetStrCode(d.getSeconds(),2);

        g.setFont(f);
        if((first_time>0) || (w!=w_cur) || (h!=h_cur)) {
           first_time=0;  
           w_cur=w;
           h_cur=h;

           FontMetrics fm=g.getFontMetrics(f);           
           posx=(w-fm.stringWidth(s))/2;
           posy=(h+fm.getAscent()-fm.getDescent())/2;
        }   
        g.setColor(fcol);
        g.drawString(s, posx, posy);
    }

    public void start() {
        if(thread==null) {
           thread = new Thread(this);
           thread.start();
        }   
    }

    public void stop() {
        if(thread!=null) {
           thread.stop();
           thread = null;
        }   
    }

    public void run() {
        while(thread != null) {
            try {
                Thread.sleep(msec);
            } catch(InterruptedException _ex) { 
            
            }
            Date d=new Date();
            if(d.getSeconds() != cur_date.getSeconds()) {
               cur_date=d; 
               repaint();
            }   
        }
    }

    public Uhrzeit() {
    }

}
