Friday , July 21 2017
Home / java programs / Java Clock Example using Swings

Java Clock Example using Swings

In this tutorials I am going to show you how to create a clock using Java. Here Java clock is developed using swings and Java Threads.

Java Clock Example :

Clock.java


import java.awt.*; 
import javax.swing.*;        
import java.util.*; 
 
class Clock extends JFrame implements Runnable 
{ 
  Thread runner;  
  Font clockFont; 
  
     public Clock() 
     { 
       super("Java clock"); 
       setSize( 350, 100); 
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       setVisible(true); 
       setResizable(false);                              
    
       clockFont = new Font("Serif", Font.BOLD, 40);     
        
       Container contentArea = getContentPane(); 
       ClockPanel timeDisplay = new ClockPanel(); 
 
  
       contentArea.add(timeDisplay);                     
       setContentPane(contentArea); 
       start();                                          
      
     } 
      
     class ClockPanel extends JPanel 
     { 
      public void paintComponent(Graphics painter ) 
        { 
        Image pic = 
          Toolkit.getDefaultToolkit().getImage("background.jpg"); 
          
         if(pic != null) 
            
          painter.drawImage(pic, 0, 0, this);      
      
          painter.setFont(clockFont);                    
          painter.setColor(Color.black); 
          painter.drawString( timeNow(), 60, 40); 
          
    
        } 
     } 
 
      
     //getting current time 
     public String timeNow() 
     { 
       Calendar now = Calendar.getInstance(); 
       int hrs = now.get(Calendar.HOUR_OF_DAY); 
       int min = now.get(Calendar.MINUTE); 
       int sec = now.get(Calendar.SECOND); 
        
       String time = zero(hrs)+":"+zero(min)+":"+zero(sec); 
        
       return time; 
     } 
 
 
      
     public String zero(int num) 
     { 
       String number=( num < 10) ? ("0"+num) : (""+num); 
       return number;                                    //Add leading zero if needed 
        
     } 
      
      
     public void start() 
     { 
       if(runner == null) runner = new Thread(this); 
       runner.start(); 
                                                              
     } 
 
 
     public void run() 
     { 
       while (runner == Thread.currentThread() ) 
       { 
        repaint(); 
                                                          
           try 
             { 
               Thread.sleep(1000); 
             } 
              catch(InterruptedException e) 
                  { 
                    System.out.println("Thread failed"); 
                  } 
                  
       } 
     } 
      
     public static void main(String [] args) 
     { 
       Clock eg = new Clock(); 
     } 
}

Output :

Java Clock
Happy Learning 🙂

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Recommended

Java Program For Binary Addition

Here we are going to write a Java program for Binary Addition. As part of …

Leave a Reply

Your email address will not be published. Required fields are marked *