Login    New User    Forgot Password    Help      

Digital Clock

Java Tutorials

// digital clock

import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
import java.util.Date;

public class app8 extends java.applet.Applet implements Runnable
{

Font theFont = new Font("TimesRoman",Font.BOLD,24);

// declaring theDate variable datatype Date
Date theDate;
Thread t;

public void init()
{
setBackground(Color.red);
setForeground(Color.white);
}

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

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

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

public void paint(Graphics g)
{
g.setFont(theFont);
g.drawString(theDate.toString(),10,50);
}
}

Digital Clock Example

// digital clock

import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
import java.util.Date;

public class app8 extends java.applet.Applet implements Runnable
{

Font theFont = new Font("TimesRoman",Font.BOLD,24);

// declaring theDate variable datatype Date
Date theDate;
Thread t;

public void init()
{
setBackground(Color.red);
setForeground(Color.white);
}

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

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

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

public void paint(Graphics g)
{
g.setFont(theFont);
g.drawString(theDate.toString(),10,50);
}
}

Output

Output in applet

Java Tutorials