Codex

Digital Clock

Aug 11th, 2011
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.85 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;      
  3. import java.util.*;
  4.  
  5. class Clock extends JFrame implements Runnable
  6. {
  7.   Thread runner; //declare global objects
  8.   Font clockFont;
  9.  
  10.      public Clock()
  11.      {
  12.        super("Java clock");
  13.        setSize( 350, 100);
  14.        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15.        setVisible(true);
  16.        setResizable(false);                             //create window
  17.    
  18.        clockFont = new Font("Serif", Font.BOLD, 40);    //create font instance
  19.        
  20.        Container contentArea = getContentPane();
  21.        ClockPanel timeDisplay = new ClockPanel();
  22.  
  23.  
  24.        contentArea.add(timeDisplay);                    //add components
  25.        setContentPane(contentArea);
  26.        start();                                         //start thread running
  27.      
  28.      }
  29.      
  30.      
  31.      class ClockPanel extends JPanel
  32.      {
  33.       public void paintComponent(Graphics painter )
  34.         {
  35.         Image pic =
  36.           Toolkit.getDefaultToolkit().getImage("background.jpg");
  37.          
  38.          if(pic != null)
  39.            
  40.             painter.drawImage(pic, 0, 0, this);     //create image
  41.      
  42.                  
  43. //if I didn't use a background image I would have used the setColor and fillRect methods to set background
  44.      
  45.           painter.setFont(clockFont);                   //create clock components
  46.           painter.setColor(Color.black);
  47.           painter.drawString( timeNow(), 60, 40);
  48.          
  49.    
  50.         }
  51.      }
  52.  
  53.      
  54.      //get current time
  55.      public String timeNow()
  56.      {
  57.        Calendar now = Calendar.getInstance();
  58.        int hrs = now.get(Calendar.HOUR_OF_DAY);
  59.        int min = now.get(Calendar.MINUTE);
  60.        int sec = now.get(Calendar.SECOND);
  61.        
  62.        String time = zero(hrs)+":"+zero(min)+":"+zero(sec);
  63.        
  64.        return time;
  65.      }
  66.  
  67.  
  68.      
  69.      public String zero(int num)
  70.      {
  71.        String number=( num < 10) ? ("0"+num) : (""+num);
  72.        return number;                                    //Add leading zero if needed
  73.        
  74.      }
  75.      
  76.      
  77.      public void start()
  78.      {
  79.        if(runner == null) runner = new Thread(this);
  80.        runner.start();
  81.                                                              //method to start thread
  82.      }
  83.  
  84.  
  85.      public void run()
  86.      {
  87.        while (runner == Thread.currentThread() )
  88.        {
  89.         repaint();
  90.                                                          //define thread task
  91.            try
  92.              {
  93.                Thread.sleep(1000);
  94.              }
  95.               catch(InterruptedException e)
  96.                   {
  97.                     System.out.println("Thread failed");
  98.                   }
  99.                  
  100.        }
  101.      }
  102.      
  103.      //create main method
  104.      public static void main(String [] args)
  105.      {
  106.        Clock eg = new Clock();
  107.      }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment