/**********************************************************************************
 *                                                                                *
 *                            Digital clock Applet                                *
 *                            ( DigitalClock.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      : DigitalClock
//    Access control  : public( Access possibility even from which class )
//    Extends class   : Applet
//    Implements class: Runnable( Use of Thread is enabled )
public  class  DigitalClock  extends Applet implements Runnable {

/************************* << Class attribute of definition >> ********************/
    Dimension    d;                           /* Display range                    */
    Thread       kicker=null;                 /* Thread(Initial value:suspension) */
    Image        offs;                        /* Off screen                       */
    Graphics     grf;                         /* Drawing range                    */
    Image        img;                         /* Background image                 */
    MediaTracker mt;                          /* MediaTracker                     */
    int          imgsw;                       /* Background image presence info   */
    String       param;                       /* Parameter reading                */
    Font         font;                        /* Clock letter font                */
    Color        color;                       /* Clock letter color               */
    int          h;                           /* Hour information                 */
    String       sh,h10,h01;                  /* Hour display letter information  */
    int          h_fontsize;                  /* Hour display font information    */
    int          h10x,h10y,h01x,h01y;         /* Hour display position information*/
    int          m;                           /* Minute information               */
    String       sm,m10,m01;                  /* Minute display letter information*/
    int          m_fontsize;                  /* Minute display font information  */
    int          m10x,m10y,m01x,m01y;         /* Minute display position info     */
    int          s;                           /* Second information               */
    String       ss,s10,s01;                  /* Second display letter information*/
    int          s_fontsize;                  /* Second display font information  */
    int          s10x,s10y,s01x,s01y;         /* Second display position info     */
    int          am_pm;                       /* Morning/Afternoon information    */

/***************** << 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("image");          /* Background image input reading */
        if ( param != null ) {                  /* Input parameter existence?     */
            mt = new MediaTracker(this);        /* MediaTracker instance          */
            img = getImage(getCodeBase(),param);/* Background image reading       */
            mt.addImage(img,0);                 /* Image load surveillance reg    */
            imgsw = 1;                          /* Background image existence info*/
        }
        else                                    /* No : no designated input       */
            imgsw = 0;                          /* No background image info       */

        param = getParameter("h_fontsize");     /* Hour display font size reading */
        h_fontsize = (param != null)?           /* Input determination            */
            Integer.parseInt(param): 20;
        param = getParameter("m_fontsize");     /* Minute disp font size reading  */
        m_fontsize = (param != null)?           /* Input determination            */
            Integer.parseInt(param): 20;
        param = getParameter("s_fontsize");     /* Second disp font size reading  */
        s_fontsize = (param != null)?           /* Input determination            */
            Integer.parseInt(param): 20;

        param = getParameter("h10x");           /* Hour 10 places disp position(H)*/
        h10x = (param != null)?                 /* Input determination            */
            Integer.parseInt(param): 0;
        param = getParameter("h10y");           /* Hour 10 places disp position(V)*/
        h10y = (param != null)?                 /* Input determination            */
            Integer.parseInt(param): 20;
        param = getParameter("h01x");           /* Hour 1 place disp position(H)  */
        h01x = (param != null)?                 /* Input determination            */
            Integer.parseInt(param): 10;
        param = getParameter("h01y");           /* Hour 1 place disp position(V)  */
        h01y = (param != null)?                 /* Input determination            */
            Integer.parseInt(param): 20;

        param = getParameter("m10x");           /* Minute 10 places disp posi(H)  */
        m10x = (param != null)?                 /* Input determination            */
            Integer.parseInt(param): 30;
        param = getParameter("m10y");           /* Minute 10 places disp posi(V)  */
        m10y = (param != null)?                 /* Input determination            */
            Integer.parseInt(param): 20;
        param = getParameter("m01x");           /* Minute 1 place disp position(H)*/
        m01x = (param != null)?                 /* Input determination            */
            Integer.parseInt(param): 40;
        param = getParameter("m01y");           /* Minute 1 place disp position(V)*/
        m01y = (param != null)?                 /* Input determination            */
            Integer.parseInt(param): 20;

        param = getParameter("s10x");           /* Second 10 places disp posi(H)  */
        s10x = (param != null)?                 /* Input determination            */
            Integer.parseInt(param): 60;
        param = getParameter("s10y");           /* Second 10 places disp posi(V)  */
        s10y = (param != null)?                 /* Input determination            */
            Integer.parseInt(param): 20;
        param = getParameter("s01x");           /* Second 1 place disp position(H)*/
        s01x = (param != null)?                 /* Input determination            */
            Integer.parseInt(param): 70;
        param = getParameter("s01y");           /* Second 1 place disp position(V)*/
        s01y = (param != null)?                 /* Input determination            */
            Integer.parseInt(param): 20;

    }                                           /* 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();
    }                                           /* End of start method            */

/*********** Repeatedly (run) method ***********/
    public void run() {
        Thread.currentThread().setPriority(Thread.NORM_PRIORITY-3);
        if ( imgsw != 0 ) {                     /* It is designated an image?     */
            try {                               /* Interruption confirmation      */
                mt.waitForID(0);                /* Image load waiting             */
            } catch( InterruptedException e ) { /* Interruption processing        */
                System.out.println("Wait Error");
                return;
            }
        }

        while( kicker != null) {              /* Repeat until kicker becomes null */
            repaint();                          /* Drawing triggering             */
            try {                               /* Interruption confirmation      */
                kicker.sleep(100);              /* Wait 100 milli 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) {
        if ( imgsw != 0 ) {                  /* Background image infor existence? */
            if ( mt.isErrorID(0)) {             /* Load error?                    */
                g.drawString("Image Load Error....",50,20); /* Error display      */
                return;
            }
            if( ! mt.checkID(0)) {              /* Loading ?                      */
                g.drawString("Loading....",d.width/2-30,20); /* Loading display   */
                return;
            }
            grf.drawImage(img,0,0,this);        /* Image information setting      */
        }
        else {                                  /* NO :                           */
            grf.setColor(Color.black);          /* Set backcolor ( black )        */
            grf.fillRect(0,0,d.width,d.height); /* Paint backcolor                */
        }

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

        h = d.getHours();                       /* Hour information reading       */
        if ( h < 12 )                           /* Morning?                       */
            am_pm = 0;                          /* YES:Morning information setting*/
        else {                                  /* NO :Afternoon                  */
            h -= 12;                            /* 12 hours reduction from hour   */
            am_pm = 1;                          /* Afternoon information setting  */
        }

        if ( h < 10 )                           /* One column ( 0 - 9 )?          */
            sh = " " + h + " ";                 /* Two column (  0 -  9 ) + Blank */
        else
            sh = h + " ";                       /* Figure + Blank(for extraction) */
        h10 = sh.substring(0,1);                /* Hour 10 places figure extracte */
        h01 = sh.substring(1,2);                /* Hour 1 place figure extracte   */
        if ( am_pm == 0 )                       /* Morning?                       */
            grf.setColor(Color.red);            /* Set morning letter color(red)  */
        else
            grf.setColor(Color.green);         /*Set afternoon letter color(green)*/
        grf.setFont(new Font("Dialog",Font.BOLD,h_fontsize)); /* Set font info    */
        grf.drawString( h10, h10x, h10y );      /* Set hour 10 places display     */
        grf.drawString( h01, h01x, h01y );      /* Set hour 1 place display       */

        m = d.getMinutes();                     /* Minute information reading     */
        if ( m < 10 )                           /* One column ( 0 - 9 )?          */
            sm = "0" + m + " ";                 /* Two column ( 00 - 09 ) + Blank */
        else
            sm = m + " ";                       /* Figure + Blank(for extraction) */
        m10 = sm.substring(0,1);                /* Minute 10 places fig extracte  */
        m01 = sm.substring(1,2);                /* Minute 1 place fig extracte    */
        if ( am_pm == 0 )                       /* Morning?                       */
            grf.setColor(Color.red);            /* Set morning letter color(red)  */
        else
            grf.setColor(Color.green);         /*Set afternoon letter color(green)*/
        grf.setFont(new Font("Dialog",Font.BOLD,m_fontsize)); /* Set font info    */
        grf.drawString( m10, m10x, m10y );      /* Set minute 10 places display   */
        grf.drawString( m01, m01x, m01y );      /* Set minute 1 place display     */

        s = d.getSeconds();                     /* Second information reading     */
        if ( s < 10 )                           /* One column ( 0 - 9 )?          */
            ss = "0" + s + " ";                 /* Two column ( 00 - 09 ) + Blank */
        else
            ss = s + " ";                       /* Figure + Blank(for extraction) */
        s10 = ss.substring(0,1);                /* Second 10 places fig extracte  */
        s01 = ss.substring(1,2);                /* Second 1 place fig extracte    */
        grf.setColor(Color.red);                /* Letter color setting (red)     */
        grf.setFont(new Font("Dialog",Font.BOLD,s_fontsize)); /* Set font info    */
        grf.drawString( s10, s10x, s10y );      /* Set second 10 places display   */
        grf.drawString( s01, s01x, s01y );      /* Set second 1 place display     */


        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 Digital clock Applet                           *
 **********************************************************************************/