/**********************************************************************************
 *                                                                                *
 *                          Real time clock Applet                                *
 *                            ( RealClock.java )                                  *
 *                                                         Author : Seiichi Inoue *
 **********************************************************************************/

/********************** << Imported package class definition >> *******************/
import java.applet.Applet;      /* Applet packages                                */
import java.awt.*;              /* All of the Abstract Windowing Toolkit packages */
import java.util.Date;          /* Date packages                                  */

/************************** <<Oneself of class definition>> ***********************/
//    Class name      : RealClock
//    Access control  : public( Access possibility even from which class )
//    Extends class   : Applet
//    Implements class: Runnable( Use of Thread is enabled )
public class  RealClock  extends Applet implements Runnable {

/************************* << Class attribute of definition >> ********************/
    Thread    kicker = null;                  /* Thread(Initial value:suspension) */
    Dimension d;                              /* Display range                    */
    Image     offs;                           /* Off screen                       */
    Graphics  grf;                            /* Drawing range                    */
    String    param;                          /* Parameter reading                */
    int       red,green,blue;                 /* Clock letter color parameter     */
    Color     color;                          /* Clock letter color               */
    int       backred,backgreen,backblue;     /* Background color parameter       */
    Color     backcolor;                      /* Background color                 */
    String    fontname;                       /* Clock letter font parameter      */
    int       fontsize;                       /* Clock letter size parameter      */
    Font      font;                           /* Clock letter font                */
    int       strHeight;                      /* The clock letter height          */
    int       x;                              /* Display start position( Width )  */
    int       y_Point;                        /* Display position ( Height )      */

/***************** << Class of method (implementation procedure) >> ***************/
//
/******** Initialization (init) method *********/
    public void init() {
        d = size();                             /* Set display screen size        */
        offs = createImage(d.width,d.height);   /* Off scr area preparation       */
        grf = offs.getGraphics();               /* Graphics object extraction     */

        param = getParameter("font");           /* Input "font" reading           */
        fontname = (param != null)?             /* Input determination            */
            param: "Dialog";
        param = getParameter("fontsize");       /* Input "fontsize" reading       */
        fontsize = (param != null)?             /* Input determination            */
            Integer.parseInt(param): 35;

        font = new Font(fontname,Font.BOLD,fontsize);     /* Font setting         */
        strHeight  = (getFontMetrics(font)).getLeading(); /* Vertical position    */
        strHeight -= (getFontMetrics(font)).getDescent();
        strHeight += (getFontMetrics(font)).getAscent();
        y_Point    = (d.height + strHeight                /* Set vertical position*/
            - (getFontMetrics(font)).getDescent())/2;

        param = getParameter("x");              /* Display start position reading */
        x = (param != null)?                    /* Input determination            */
            Integer.parseInt(param): 0;

        param = getParameter("backred");        /* Background red reading         */
        backred = (param != null)?              /* Input determination            */
            Integer.parseInt(param): 255;
        param = getParameter("backgreen");      /* Background green reading       */
        backgreen = (param != null)?            /* Input determination            */
            Integer.parseInt(param): 255;
        param = getParameter("backblue");       /* Background blue reading        */
        backblue = (param != null)?             /* Input determination            */
            Integer.parseInt(param): 255;
        backcolor = new Color                   /* Background color assembly      */
            (backred,backgreen,backblue);

        param = getParameter("red");            /* Letter red reading             */
        red = (param != null)?                  /* Input determination            */
            Integer.parseInt(param): 255;
        param = getParameter("green");          /* Letter green reading           */
        green = (param != null)?                /* Input determination            */
            Integer.parseInt(param): 0;
        param = getParameter("blue");           /* Letter blue reading            */
        blue = (param != null)?                 /* Input determination            */
            Integer.parseInt(param): 0;
        color = new Color(red,green,blue);      /* Letter color assembly          */
    }                                           /* End of initial method          */


/************ Start (start) method *************/
    public void start() {
        if ( kicker == null ) {                 /* Kicker is null? ( Suspension? )*/
            kicker = new Thread(this);          /* YES: kicker setting            */
            kicker.start();                     /* Setting of start               */
        }
        repaint();                              /* Drawing triggering             */
    }                                           /* End of start method            */


/*********** Repeatedly (run) method ***********/
    public void run() {
        Thread.currentThread().setPriority(Thread.NORM_PRIORITY-3);
        while( kicker != null) {              /* Repeat until kicker becomes null */
            repaint();                          /* Drawing triggering             */
            try {                               /* Interruption confirmation      */
                kicker.sleep(1000);             /* Wait 1 seconds set             */
            } catch(InterruptedException e) {}  /* Interruption processing        */
        }
        kicker = null;                          /* Repeate process completion set */
    }                                           /* End of run method              */


/*********** Renewal (update) method ***********/
    public void update(Graphics g) {            /* Lost of screen flickering      */
        paint(g);                               /* Drawing                        */
    }                                           /* End of update method           */


/*********** Drawing (paint) method ************/
    public void paint(Graphics g) {
        grf.setColor(backcolor);                /* Set display back color         */
        grf.fillRect(0,0,d.width,d.height);     /* Display range is applied       */

        Date d = new Date();                    /* Read clock information         */

        grf.setColor(color);                    /* Letter color setting           */
        grf.setFont(font);                      /* Font information setting       */
        grf.drawString(d.toString(),x,y_Point); /* Display information setting    */
        g.drawImage(offs,0,0,this);             /* Drawing setting                */
    }                                           /* End of paint method            */


/************ Stop (stop) method ***************/
    public void stop() {
        if( kicker != null ) {                  /* Kicker is not null?( action? ) */
            kicker.stop();                      /* Set kicker to suspension       */
            kicker = null;                      /* Set kicker suspension condition*/
        }
    }                                           /* End of stop method             */

}                                               /* End of class setting           */

/**********************************************************************************
 *                       End of Real time clock Applet                            *
 **********************************************************************************/