Digital Clock Using Swing in Java

This article describes the creation of a digital clock using Java.

Digital clock using Java

 
This article describes the creation of a digital clock using Java. This application shows the system's current time.
 
Packages Imported
  1. import javax.swing.JFrame  
Jframe is an extended version of Frame that supports the Swing component architecture.
  1. import java.awt.Color  
The Format RGB shows the combination of red, green and blue color, and its value ranges between 0 and 255.
  1. import javax.swing.WindowConstants  
WindowConstants is used for operations like window closing and so on.
 
Syntax
  1. public class DimensionDemo extends Object class  
  2.   
  3. import java.awt.event.ActionEvent  
It imports only the "ActionEvent" class.
  1. import java.awt.event.ActionListener  
An action event is fired, whenever any action is done by the user. 
  1. import java.util.Calendar  
The class java.util.Date defines an instance of time.
  1. import java.awt.Graphics  
Graphics exists under the class that extends Object. Graphics is used to represent graphics on the window.
 
Syntax
  1. public class GraphicsDemo extends Object  
  2.   
  3. import java.awt.Dimension  
Dimension is a class that extends the Object class.
  1. import javax.swing.JPanel  
JPanel can be used in place of Canvas. The new feature of JPanel is Border.
  1. import javax.swing.Timer;  
A Swing timer fires one or more action events after a specified delay.
  1. import java.awt.Font  
Font is a class that represents fonts and it implements the serializable interface.
 
Syntax
 
public class FontDemo implements Serializable interface
 
Coding
  1. import java.awt.*;  
  2. //In place of star we use Color,Dimension,Font,Graphics,event.ActionEvent,event.ActionListner.   
  3. import java.util.*;  
  4. //In place of star we use Calendar.   
  5. import javax.swing.*;  
  6. //In place of star we use Jframe.JPane1,WindowConstants.  
  7. import java.awt.event.ActionListener;  
  8. import javax.swing.Timer;  
  9. import java.awt.event.ActionEvent;  
  10. public class DigitalClock1 {  
  11.     public static void main(String[] args) {  
  12.         JFrame frm = new JFrame();  
  13.         frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);  
  14.         SimpleDigitalClock clock1 = new SimpleDigitalClock();  
  15.         frm.add(clock1);  
  16.         frm.pack();  
  17.         frm.setVisible(true);  
  18.     }  
  19.     static class SimpleDigitalClock extends JPanel {  
  20.         String stringTime;  
  21.         int hour, minute, second;  
  22.         String aHour = "";  
  23.         String bMinute = "";  
  24.         String cSecond = "";  
  25.         public void setStringTime(String abc) {  
  26.             this.stringTime = abc;  
  27.         }  
  28.         public int Number(int a, int b) {  
  29.             return (a <= b) ? a : b;  
  30.         }  
  31.         SimpleDigitalClock() {  
  32.             Timer t = new Timer(1000new ActionListener() {  
  33.                 public void actionPerformed(ActionEvent e) {  
  34.                     repaint();  
  35.                 }  
  36.             });  
  37.             t.start();  
  38.         }  
  39.         @Override  
  40.         public void paintComponent(Graphics v) {  
  41.             super.paintComponent(v);  
  42.             Calendar rite = Calendar.getInstance();  
  43.             hour = rite.get(Calendar.HOUR_OF_DAY);  
  44.             minute = rite.get(Calendar.MINUTE);  
  45.             second = rite.get(Calendar.SECOND);  
  46.             if (hour < 10) {  
  47.                 this.aHour = "0";  
  48.             }  
  49.             if (hour >= 10) {  
  50.                 this.aHour = "";  
  51.             }  
  52.             if (minute < 10) {  
  53.                 this.bMinute = "0";  
  54.             }  
  55.             if (minute >= 10) {  
  56.                 this.bMinute = "";  
  57.             }  
  58.             if (second < 10) {  
  59.                 this.cSecond = "0";  
  60.             }  
  61.             if (second >= 10) {  
  62.                 this.cSecond = "";  
  63.             }  
  64.             setStringTime(aHour + hour + ":" + bMinute + minute + ":" + cSecond + second);  
  65.             v.setColor(Color.BLACK);  
  66.             int length = Number(this.getWidth(), this.getHeight());  
  67.             Font Font1 = new Font("SansSerif", Font.PLAIN, length / 5);  
  68.             v.setFont(Font1);  
  69.             v.drawString(stringTime, (int) length / 6, length / 2);  
  70.         }  
  71.         @Override  
  72.         public Dimension getPreferredSize() {  
  73.             return new Dimension(200200);  
  74.         }  
  75.     }  
  76. }  
Output
 
 Clipboard02.gif