1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Java digital clock

Discussion in 'Java' started by Systemerror, Dec 29, 2008.

  1. Systemerror

    Systemerror New Member

    Joined:
    Jan 11, 2008
    Messages:
    18
    Likes Received:
    1
    Trophy Points:
    0
    Home Page:
    This is a basic digital clock in Java that works off your operating system time, it works in a multithreaded environment and have coded it to put my own background in.

    Code:
    import java.awt.*;
    import javax.swing.*;       
    import java.util.*;
    
    class Clock extends JFrame implements Runnable
    {
      Thread runner; //declare global objects
      Font clockFont;
      
         public Clock()
         {
           super("Java clock");
           setSize( 350, 100);
           setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           setVisible(true);
           setResizable(false);                             //create window
       
           clockFont = new Font("Serif", Font.BOLD, 40);    //create font instance
           
           Container contentArea = getContentPane();
           ClockPanel timeDisplay = new ClockPanel();
    
      
           contentArea.add(timeDisplay);                    //add components
           setContentPane(contentArea);
           start();                                         //start thread running
         
         }
         
         
         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);     //create image
          
                     
    //if I didn't use a background image I would have used the setColor and fillRect methods to set background
          
              painter.setFont(clockFont);                   //create clock components
              painter.setColor(Color.black);
              painter.drawString( timeNow(), 60, 40);
             
        
            }
         }
    
         
         //get 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();
                                                                 //method to start thread
         }
    
    
         public void run()
         {
           while (runner == Thread.currentThread() )
           {
            repaint();
                                                             //define thread task
               try
                 {
                   Thread.sleep(1000);
                 }
                  catch(InterruptedException e)
                      {
                        System.out.println("Thread failed");
                      }
                      
           }
         }
         
         //create main method
         public static void main(String [] args)
         {
           Clock eg = new Clock();
         }
    }
     
    Kujuna likes this.
  2. skp819

    skp819 New Member

    Joined:
    Dec 8, 2008
    Messages:
    89
    Likes Received:
    3
    Trophy Points:
    0
    good post thanks for sharing it....
     
  3. hanleyhansen

    hanleyhansen New Member

    Joined:
    Jan 24, 2008
    Messages:
    336
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    Drupal Developer/LAMP Developer
    Location:
    Clifton
    Home Page:
    Nice looking forward to playing around with it.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
  6. akash2008

    akash2008 New Member

    Joined:
    Jan 20, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    that works off your operating system time, it works in a multithreaded environment and have coded it to put my own
     
  7. rkudoz

    rkudoz New Member

    Joined:
    Jan 29, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for sharing... atleast we can play with it as well....:)

    Nice Share BTW! :D)
     

Share This Page