A JAVA TIMER SAMPLE

back

This little java applet ... will just show the local time ...

The current time :

This is the HTML portion :

<p>
<center>
The current time :
<APPLET CODE=TimerClass.class WIDTH=90 HEIGHT=20>
<param name=MODE value="1">
<param name=background value="eeeeee">
<param name=foreground value="000000">
</APPLET>
</center>
</p>

This the TimerClass.java source

 
// © 2001-2003, Sergey Zaytsev, All Rights Reserved

import java.applet.*;
import java.awt.*;
import java.util.*;

// 1 day = 86,400 regular seconds = 100,000 digital seconds (deconds)
// 864 regular milliseconds = 1000 digital milliseconds (millideconds)
public class TimerClass extends Applet
    implements Runnable
{
    Thread timer;
    int Mode = 1;         // 1 - regular time, 2 - digital time
    int SleepMS;  // sleep milliseconds
    int y;
    int x;
    public void init()
    {
                   String strMode = getParameter("MODE");
                   if (strMode != null)
                             Mode = Integer.parseInt(strMode);

                   SleepMS = (Mode == 1) ? 1000 : 864;

                   String strBackGround = getParameter("background");

                   if (strBackGround != null)
                             this.setBackground(stringToColor(strBackGround));

                   String strForeGround = getParameter("foreground");
                   if (strForeGround != null)
                             this.setForeground(stringToColor(strForeGround));

                   String strFontName = getParameter("FONTNAME");
                   if (strFontName == null)
                             strFontName = this.getFont().getName();

                   int intFontSize = this.getFont().getSize();

                   String strFontSize = getParameter("FONTSIZE");

                   if (strFontSize != null)
                             intFontSize = Integer.parseInt(strFontSize);

                   int intFontStyle = this.getFont().getStyle();

                   String strFontStyle = getParameter("FONTSTYLE");

                   if (strFontStyle != null)
                             intFontStyle = Integer.parseInt(strFontStyle);

                   // Set font              
                   this.setFont(new Font(strFontName, intFontStyle, intFontSize));

                   y = this.size().height / 2 + intFontSize / 2;

                   FontMetrics fm = getFontMetrics(this.getFont());

                   x = this.size().width / 2 - fm.stringWidth((Mode == 1) ? "00:00:00 AM" : "00:00:00") / 2;

    }

    public void paint(Graphics screen) 
    {
                   Calendar now = Calendar.getInstance();
                   int intHour = now.get(Calendar.HOUR);
                   int intHourOfDay = now.get(Calendar.HOUR_OF_DAY);
                   int intAM_PM = now.get(Calendar.AM_PM);
                   int intMinute = now.get(Calendar.MINUTE);
                   int intSecond = now.get(Calendar.SECOND);
                   String strShow;

                   if (Mode == 1)
                             strShow = FormatTime(intHour, intMinute, intSecond, intAM_PM);
                   else
                   {
                             int intMilliSecond = now.get(Calendar.MILLISECOND);
                             int intMilliSecondOfDay = ((intHourOfDay * 60 + intMinute) * 60 + intSecond) * 1000 +
                                                                                        intMilliSecond;

                             int intDecondOfDay = intMilliSecondOfDay / 864;

                             int intDour = intDecondOfDay / 10000;

                             int intDinute = (intDecondOfDay - (intDour * 10000)) / 100;

                             int intDecond = intDecondOfDay % 100;

                             strShow = FormatDime(intDour, intDinute, intDecond);

                   }

                   screen.drawString(strShow, x, y);
    }

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

    public void run()
    {
        while (true)
        {
            repaint();
            try { Thread.sleep(SleepMS); }
            catch (InterruptedException e) { }
        }
    }

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

    public String FormatTime(int intHour, int intMinute, int intSecond, int intAM_PM)
    {
                if ((intHour == 0) && (intAM_PM == 1) /* PM */)
                intHour = 12;
        
                String strTime = "";
        if (intHour < 10)
                strTime = "0";
        strTime += intHour + ":";
        if (intMinute < 10)
                strTime += "0";
        strTime += intMinute + ":";
        if (intSecond < 10)
                strTime += "0";
        strTime += intSecond + " " + ((intAM_PM == 0) ? "AM" : "PM");

        return strTime;
    }

    public String FormatDime(int intDour, int intDinute, int intDecond)
    {
                   String strDime = "0" + intDour + ".";
                   if (intDinute < 10)
                             strDime += "0";

                   strDime += intDinute + ".";
                   if (intDecond < 10)
                             strDime += "0";

                   strDime += intDecond;

                   return strDime;
    }

          // Converts a string formatted as "rrggbb" to an awt.Color object
          private Color stringToColor(String paramValue)
          {
                   return new Color(Integer.decode("0x" + paramValue).intValue());
          }
          // External interface used by design tools to show properties of an applet.
          public String[][] getParameterInfo()
          {
                   String[][] info =
                   {
                             { "background", "String", "Background color, format \"rrggbb\"" },
                             { "foreground", "String", "Foreground color, format \"rrggbb\"" },
                             { "fontname", "String", "Font Name" },
                             { "fontsize", "int", "Font Size" },
                             { "fontstyle", "int", "Font Style" },
                             { "mode", "int", "1 - Regular Time; 2 - Decimal Time" }
                   };
                   return info;
          }

}
 
// eof

Simple huh? Of course, this 'java' source must be compiled (javac), to build the 'class' BYTECODE, to be uploaded to the web site ...

back

top


checked by tidy  Valid HTML 4.01 Transitional