Clockdemo.java
/*
* To-Change the license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
Package newpackage;
Import java.awt.*;
Import java.util.*;
Import javax.swing.*;
Digital clock
public class Clockdemo extends JFrame implements runnable{
Thread clock;
Public Clockdemo () {
Super ("Digital Clock");//Call Parent class constructor
SetFont (New Font ("Times New Roman", font.bold,60));//Set the display font for the clock
Start (); Start process
SetSize (280,100); Set Window size
SetVisible (TRUE); Window Visual
Setdefaultcloseoperation (Jframe.exit_on_close); Exit the program when the window is closed
}
public void Start () {//START process
if (clock==null) {//If the process is a null value
Clock=new Thread (this); Instantiating a process
Clock.start (); Start process
}
}
public void Run () {//Run process
while (Clock!=null) {
Repaint (); Call the Paint method to redraw the interface
try{
Thread.Sleep (1000); Thread paused for one second (1000 milliseconds)
}
catch (Interruptedexception ex) {
Ex.printstacktrace (); Output error message
}
}
}
public void Stop () {//Stop process
Clock=null;
}
public void Paint (Graphics g) {//Reload component's Paint method
Graphics2D g2= (graphics2d) G; Get Graphics2D Object
Calendar now=new GregorianCalendar (); Instantiating a Calendar object
String timeinfo= ""; Output information
int Hour=now.get (calendar.hour_of_day); Get the number of hours
int Minute=now.get (calendar.minute); Get points
int Second=now.get (Calendar.second); Get the number of seconds
if (hour<=9)
timeinfo+= "0" +hour+ ":"; Formatted output
Else
timeinfo+=hour+ ":";
if (minute<=9)
timeinfo+= "0" +minute+ ":";
Else
Timeinfo+=minute+ ":";
if (second<=9)
timeinfo+= "0" +second;
Else
Timeinfo+=second;
G.setcolor (Color.White); Set the current color to white
Dimension dim=getsize (); Get window Size
G.fillrect (0,0,dim.width,dim.height); Fill background color to white
G.setcolor (Color.orange); Set the current color to orange
g.DrawString (timeinfo,20,80); Show Time string
}
public static void Main (string[] args) {
New Clockdemo ();
}
}
Java Programming-Digital clock