////////////////////////////////////////////////////////////////////////////
//
// Name: DigClk110.java
//
// Version: 1.10
//
// Description: A simple digital 24 hour clock in Java
// (beta applet)
//
// Author: William G. Ogle Jr.
//
// E-Mail: wgogle@cpcnet.com
//
// Copyright 1996 by William G. Ogle Jr.
// All rights reserved.
//
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// Revision History:
//
// Vers. Date Notes
//
// 1.00 02/28/96 1. Initial release.
// 2. No configuration options in this version.
// 3. Blue digits in a grey 3-D frame.
// Digits 16w x 21h
// Colon 9w x 21h
// Frame 122w x 29h
//
// 1.10 03/07/96 1. Added user selectable digit styles from six included
// image sets. Image sets contain two digit styles in
// each of blue, green and red digits. (Five of the
// sets are new, the other is the original set.)
// 2. Added version number to the class name to make it
// easy to see what version of the applet is running.
// 3. Identified import classes specifically.
// 4. Added useage examples and explanations comment block
// to the source code. This text was also added to the
// installation instructions.
// 5. Improved source code commenting. (Had to do this
// because when I started working on this new version,
// I couldn't immediately figure out why I wrote some
// of the code the way I did.)
//
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
//
// Source code uses a 4 column tab format.
// Set editor/viewer for 4 column tabs for proper formatting.
//
////////////////////////////////////////////////////////////////////////////
/***************************************************************************
Examples of useage:
1. Minimum call
If you have placed DigClk110.class in a directory other then "classes",
replace "classes" above with your directory name.
The image "*.gif"s must be in a subdirectory named "dcimages" which
must be located directly under the CODEBASE directory named above.
The digit style DigClk110 uses defaults to "lcdb0", simple blue digits.
Look at example 3 to see how to specify a different digit style.
2. Call that explains missing applet on non-Java capable browsers
3. Specifying a digit style
"DigitStyle" is case sensitive and must be entered as shown.
The digit style values are also case sensitive and should be
entered in lower case.
To determine the possible digit style values you may use, look in
the "dcimages" subdirectory for the names of the "0*.gif" files.
The digit style value would be the filename without the first
number or character and without the extension.
For example:
To use the 0lcdb0.gif set, you would use value="lcdb0"
To use the 0lcdb1.gif set, you would use value="lcdb1"
To use the 0lcdg0.gif set, you would use value="lcdg0"
To use the 0lcdg1.gif set, you would use value="lcdg1"
To use the 0lcdr0.gif set, you would use value="lcdr0"
To use the 0lcdr1.gif set, you would use value="lcdr1"
Note:
A set of images for a digit style includes all of the digits
"0*.gif" - "9*.gif", the colon "c*.gif" and the frame image,
where the asterisks above contain the digit style values.
***************************************************************************/
////////////////////////////////////////////////////////////////////////////
//
// Imports
//
////////////////////////////////////////////////////////////////////////////
import java.awt.Graphics;
import java.awt.Image;
import java.util.Date;
////////////////////////////////////////////////////////////////////////////
//
// Class: DigClk110
//
////////////////////////////////////////////////////////////////////////////
public class DigClk110 extends java.applet.Applet implements Runnable
{
Thread timer = null;
Image[] digit_image = new Image[10]; // Array of digit images 0 - 9
Image buffer_image, // Background image buffer
colon_image, // Colon image
frame_image; // Frame image
Graphics gc;
int digit_height = 21; // Height of the digits (and colons)
int digit_width = 16; // Width of the digits
int colon_width = 9; // Width of the colons
int applet_width = 122; // Width of the frame = (2 * offset) +
// (6 * digit_width) + (2 * colon_width)
int applet_height = 29; // Height of the frame = (2 * offset) +
// digit_height
int offset = 4; // Thickness of the frame
int[] image_start_x = new int[8]; // Array of digit and colon
// starting x positions
String DigitStyle = "lcd0";
////////////////////////////////////////////////////////////////////////
//
// Method: init()
//
// Overrides the init() method to provide initialization behavior.
//
////////////////////////////////////////////////////////////////////////
public void init()
{
// Get digit style parameter
if (getParameter("DigitStyle") != null)
{
DigitStyle = getParameter("DigitStyle");
}
// Check for allowable digit styles
if ((!DigitStyle.equals("lcdb0")) &&
(!DigitStyle.equals("lcdb1")) &&
(!DigitStyle.equals("lcdg0")) &&
(!DigitStyle.equals("lcdg1")) &&
(!DigitStyle.equals("lcdr0")) &&
(!DigitStyle.equals("lcdr1")))
DigitStyle = "lcdb0"; // Default digit style
// Initialize the digit and colon starting x positions
image_start_x[0] = offset; // Offset to the proper position
// inside of the frame image
for (int i = 1; i < 8; i++) // Loop through the remaining
{ // starting x positions
if ((i == 3) || (i == 6)) // Next position after a colon
image_start_x[i] = image_start_x[i - 1] + colon_width;
else // Next position after a digit
image_start_x[i] = image_start_x[i - 1] + digit_width;
}
// Load the digit images
for (int i = 0; i < 10; i++)
{
digit_image[i] = getImage(getCodeBase(), "dcimages/" + i +
DigitStyle + ".gif");
}
// Load the colon image
colon_image = getImage(getCodeBase(), "dcimages/c" + DigitStyle +
".gif");
// Load the frame image
frame_image = getImage(getCodeBase(), "dcimages/f122x29.gif");
try
{
buffer_image = createImage(applet_width, applet_height);
gc = buffer_image.getGraphics();
}
catch (Exception e) gc = null;
}
////////////////////////////////////////////////////////////////////////
//
// Method: start()
//
// Overrides the start() method to provide startup behavior.
//
////////////////////////////////////////////////////////////////////////
public void start()
{
if (timer == null)
{
timer = new Thread(this, "DigClk110");
timer.start();
}
}
////////////////////////////////////////////////////////////////////////
//
// Method: run()
//
////////////////////////////////////////////////////////////////////////
public void run()
{
while (timer != null)
{
try
{
timer.sleep(1000);
}
catch (InterruptedException e) {}
repaint();
}
timer = null;
}
////////////////////////////////////////////////////////////////////////
//
// Method: stop()
//
// Overrides the stop() method to suspend execution.
//
////////////////////////////////////////////////////////////////////////
public void stop()
{
if (timer != null)
{
timer.stop();
timer = null;
}
}
////////////////////////////////////////////////////////////////////////
//
// Method: paintDigClk()
//
// Replaces the paint() method.
//
////////////////////////////////////////////////////////////////////////
public void paintDigClk(Graphics g)
{
// Initialize variables
Date now = new Date(); // Get current time and date
int hour = now.getHours(); // Get hours
int minute = now.getMinutes(); // Get minutes
int second = now.getSeconds(); // Get seconds
int i = 0; // Index for array of starting x positions
// Draw images
// This section could probably be placed in a loop to reduce
// the compiled code size, but . . .
// Execution speed is more important than code size.
g.drawImage(frame_image, 0, 0, this);
g.drawImage(digit_image[hour / 10], // Hours tens digit
image_start_x[i++], offset, this);
g.drawImage(digit_image[hour % 10], // Hours ones digit
image_start_x[i++], offset, this);
g.drawImage(colon_image, image_start_x[i++], offset, this);
g.drawImage(digit_image[minute / 10], // Minutes tens digit
image_start_x[i++], offset, this);
g.drawImage(digit_image[minute % 10], // Minutes ones digit
image_start_x[i++], offset, this);
g.drawImage(colon_image, image_start_x[i++], offset, this);
g.drawImage(digit_image[second / 10], // Seconds tens digit
image_start_x[i++], offset, this);
g.drawImage(digit_image[second % 10], // Seconds ones digit
image_start_x[i], offset, this);
}
////////////////////////////////////////////////////////////////////////
//
// Method: update()
//
// Overrides the update() method to prevent clearing the applet
// display area prior to painting.
//
////////////////////////////////////////////////////////////////////////
public void update(Graphics g)
{
if (buffer_image != null)
{
paintDigClk(gc);
g.drawImage(buffer_image, 0, 0, this);
}
else
{
g.clearRect(0, 0, applet_width, applet_height);
paintDigClk(g);
}
}
// End of class DigClk110
}