CREATING A DIGITAL CLOCK IN JAVA

In this little short tutorial i 'll teach you how to create a digital clock 
in Java applet programming.This tutorial assumes that you have some 
knowledge of Java applet or console programming so lets start ...
First We'll need to import all those java class libraries we'll need later.

import java.awt.*;
import java.awt.Graphics.*;
import java.util.Date;
import java.applet.Applet;

These are the libraries and classes we'll need the first one java.awt.* 
imports all the abstract window toolkit classes e.g awt.Font,awt.Color 
etc....The Second one awt.Graphics.*, will include or import classes which 
are necessary for painting and repainting ,import java.util.Date will 
include the class for Date and the last but not the least will help us in 
building the applet.
ok now lets start out class:
public class digitalClock extends Applet implements Runnable  /* I have 
included Runnable because we are gonna work with Threads that you will see 
soon*/

Thread run;
Font f=new Font("ArialBlack",Font.BOLD,20);
Date now;

Here i have created an instance  variable run of Thread class the next 
instance  variable f will be used to hold the objects for Font and and now 
is the variable responsible for holding the Date object.
so till now we have:

import java.awt.Graphics.*;
import java.awt.*;
import java.applet.Applet;
import java.util.Date;
public class digitalClock extends Applet implements Runnable{
Thread run;
Font f=new Font("ArialBlack",Font.BOLD,20);
Date now;

Now lets forward this by adding a start and stop methods which will be 
resonsible for starting and stoping of applet.

public void start(){
if(run==null){          /*Checks if the Thread object run is already null */
run=new Thread(this);  / *Creates a new thread for the current object*/
run.start();       //Starts the Thread
}
}

public void stop(){
if (run!=null){ 	//Checks if the Thread is already has already stopped or 
not
run.stop();    	 //Stops the Thread execution
run=null;		//Frees the run
}
}

Next we will add the run method which is very important


public void run(){
while (true){	/*Infinite Loop*/
now=new Date();	/*Initializes now with the current date*/
repaint();		/*repaints the screen to update the changes*/
try{		/*Exception*/
Thread.sleep(1000); 	/*Sleep the thread for 1 second 1000 miliseconds so 
1000/1000=1 */
}catch(InterruptedException ie){}
}
}

The run method will repaint the screen after 1 second

and now the last method :
public void paint(Graphics g){
g.setFont(f);  //sets the font
g.setColor(Color.red); //sets the color
g.drawString(now.toString(),10,25); /*draws the string 
=>g.drawString(String,x,y).*/
}
}

the paint method will at last show the time in the applet
now.toString() will change it into String format instead of Date.

and now the full source code :

/********************Code Starts Here*******************/

import java.awt.Graphics.*;
import java.awt.*;
import java.applet.Applet;
import java.util.Date;
public class myApplet extends Applet implements Runnable{
Thread run;
Font f=new Font("ArialBlack",Font.BOLD,20);
Date now;

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

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

public void run(){
while (true){
now=new Date();
repaint();
try{
Thread.sleep(1000);
}catch(InterruptedException ie){}
}
}

public void paint(Graphics g){
g.setFont(f);
g.setColor(Color.red);
g.drawString(now.toString(),10,25);
}
}

/********************************Code Ends 
Here*******************************/
Also if you dont know how to compile and run the code here are some hints:
First create an html page for it and include the tag:

<applet code=myApplet.class  height=30 width=420 >
But if you are a dumbass then i have the full html code:
<!--code goes here-->
<html>
<head><title>Chapter 9 </title></head>
<body bgColor="pink">
<p>


<applet code=myApplet.class  height=30 width=420 align=ABSbottom>


</applet>
</body>
</html>
<!----- and ends here -->
Save the above html code as "digitalClock.html"

The code has been compiled on JDK 1.3 and works 100%.So there are no errors 
in the code but the article may have some grammatical or spelling mistakes 
so forgive me for that.
Compiled as
javac digitalClock.java
appletviewer digitalClock.html


I hope you will like this article Its not for beginners though .
Written for Ebcvg E-zine
Copyright(C) NrAziz
cyberghost4@hotmail.com
www.nraziz.tk (For updates and more tutorials)
9:41 AM 11/28/02



