Best Blogger Tips

Sunday 23 September 2012

DIGITAL CLOCK CODE IN JAVA!


Synchronized digital clock.

//code Start
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;


public class DigitalClock extends JPanel implements Runnable {

 static JMenuBar mainBar;
 static JMenu open,about;
 static JMenuItem exit,aboutme;
 
 Thread t;
 Random rand = new Random();
 
 
 public void paintComponent(Graphics g){
  
  super.paintComponent(g);
  g.setFont(new Font("Serif",Font.BOLD,50));
  //g.setColor(new Color(rand.nextInt(255), rand.nextInt(255),rand.nextInt(255)));
  g.setColor(new Color((0), (0),(255)));
  g.drawString(time(), 27, 62);
 }
 
 //Constructor to INITIALIZE THE CLOCK
 public DigitalClock() {
  Start();//Start The CLock 
  }
 
 public String time() {
  Calendar cal = Calendar.getInstance();
  int hour = cal.get(Calendar.HOUR_OF_DAY);
  int min = cal.get(Calendar.MINUTE);
  int second = cal.get(Calendar.SECOND);
  
  String time = includeZero(hour)+":"+includeZero(min)+":"+includeZero(second);
  
  return time;
 }
 
 public String includeZero(int number){
  
  String num=(number < 10) ? ("0"+number) : (""+number);
  return num;
 }
 
 
 
 public static void main(String[] args) {
  JFrame.setDefaultLookAndFeelDecorated(true);
  try {
   UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
  } catch (Exception e) {
   // TODO: handle exception
  }
  
  
  SwingUtilities.invokeLater(new Runnable() {
   
   
   
   
   @Override
   public void run() {
    JFrame frame=new JFrame("Clock");
    
   
    
    
    frame.add(new DigitalClock());
    frame.setSize(250, 150);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    
    mainBar = new JMenuBar();
    frame.setJMenuBar(mainBar);
    
    open = new JMenu("Open");
    mainBar.add(open);
    about = new JMenu("About");
    mainBar.add(about);
    exit = new JMenuItem("Exit");
    open.add(exit);
    aboutme = new JMenuItem("About me");
    about.add(aboutme);
    
    exit.addActionListener(new ActionListener() {
     
     @Override
     public void actionPerformed(ActionEvent arg0) {
      System.exit(0);
      
     }
    });
    
    
    aboutme.addActionListener(new ActionListener() {
     
     @Override
     public void actionPerformed(ActionEvent arg0) {
     JDialog dialog = new JDialog();
     dialog.setTitle("About Me");
     dialog.setVisible(true);
     dialog.setSize(400, 400);
     dialog.setLocation(790, 300);
     dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
     dialog.setLayout(null);
     Label label =new Label( "Created by : Zeeshan Jamal");
     label.setBounds(0, 0, 200, 20);
     
     dialog.add(label);
       
      
     }
    });
   }
  });
  
 
  
 }

 
 public void Start() {
 if (t==null){
 t= new Thread(this);
 t.start();
 
 
 }
 }
 public void run(){
  while (t == Thread.currentThread()){
   repaint();
   try {
    Thread.sleep(1000);
    
   } catch (Exception e) {
    System.out.println(e.getLocalizedMessage());
   }
  }
  
 }

}
//code end



Kindly Bookmark this Post using your favorite Bookmarking service:
Technorati Digg This Stumble Stumble Facebook Twitter
YOUR ADSENSE CODE GOES HERE

0 comments:

Blogger Tricks And TipsComment here

 

| Bloggers Broadcast © 2010. All Rights Reserved | Back To Top |

Your Text Link Here