Friday, May 7, 2010

JAVA : Analog Clock

JAVA Analog Clock 

!!!!!!!!!!....Applet may take some time to load....!!!!!!!!!!
Java code spot has come up with a simple analog clock written in Java. It comes as an applet, as well with the source code.  

For making the analog clock we have some mathematics calculations to find out the points  where the clock's parts like hands, digits will get paint. The Java program calculates the point positions on the bases of angle and the time. For example every second the clock's hand will move six degrees. In radian the 6 degrees is equal to 0.1047. So if we want the minutes hand to be on 15 minutes than we can can calculate that it should be placed on the angle of  0.1047*15 radians.

Here is the code to calculate points for painting clock:

int h=40;  //radius of clock

Calendar now = Calendar.getInstance(); //creating a Calendar variable for getting current time

   int  p=  (int) (Math.sin(theta) * h);
   int b=  (int) (Math.cos(theta) * h);

This Java Analog Clock can be dragged with mouse form one location to another.

Code for Creating Clock:-

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class Clock extends Frame implements MouseMotionListener{
int xmou=200; //set the center of circle
int ymou=200; //set the center of circle

double theta=-0.1047; //theta for second's hand
int x=xmou; //x position of Second's hand
int y=ymou; //y position of second's hand
int p,b; //perpendicular and base of Second's hand

int h; //hypotenous(heigth) of clock's hand

double the= -0.1047; //theta for creating outer circle

double thetamin=-0.1047;        //theta for minutes hand
int xm=xmou; //x position of minute's hand
int ym=ymou; //y position of minute's hand
int pmin,bmin; //perpendicular and base of Minute's hand

double thetah=-0.1047;         //theta for hour hand
int xh=xmou; //y position of hour's hand
int yh=ymou; //y position of hour's hand
int ph,bh; //perpendicular and base of hour's hand

double thetan=-0.0;          //theta for numbers of clock
int xn=xmou; //x position of Clock numbers
int yn=ymou; //y position of Clock numbers
int pn,bn; //perpendicular and base of clock numbers
int num=0; //for writing the numbers



//method of implemented  mouse interface
public void mouseMoved(MouseEvent me){


public void mouseDragged(MouseEvent me){
xmou=me.getX(); //changing the clock position on mouse drag
ymou=me.getY(); //changing the clock position on mouse drag


//method to paint clock
public void paint(Graphics g){
           //for writing numbers in clock and outer circle

for(int p=0;p<60;p++){
        int xocir=xmou;      //x position of outer circle
     int yocir=ymou;      //y position of outer circle
     int pocir,bocir;     //perpendicular and base of outer circle

            pocir=  (int) (Math.sin(the) * (h+23));
            bocir=  (int) (Math.cos(the) * (h+23));
            the=the - 0.1047;

if(p%5==0 ){


if(thetan<=-6.28318531 ){

thetan=thetan-0.523598776 ;
     pn=  (int) (Math.sin(thetan) * (h+10));
     bn=  (int) (Math.cos(thetan) * (h+10));


     //for drawing Clock hands

     g.drawLine(xmou,ymou,xm,ym); //drawing minute's hand
     g.drawLine(xmou,ymou,xh,yh); //drawing hour's hand
     g.drawLine(xmou,ymou,x,y); //drawing second's hand

 public void newpoint(){

    Calendar now = Calendar.getInstance(); //creating a Calendar variable for getting current time

    //for second hand

    p=  (int) (Math.sin(theta) * h);
    b=  (int) (Math.cos(theta) * h);
    //theta=theta - 0.1047;

  //for minutes hand


    pmin=  (int) (Math.sin(thetamin) * (h-6));
    bmin=  (int) (Math.cos(thetamin) * (h-6));

    //for hour's hand


   if (now.get(Calendar.MINUTE)>=12 && now.get(Calendar.MINUTE)<24){

   else if(now.get(Calendar.MINUTE)>=24 && now.get(Calendar.MINUTE)<36){

   else if(now.get(Calendar.MINUTE)>=36 && now.get(Calendar.MINUTE)<48){

   else if(now.get(Calendar.MINUTE)>=48 && now.get(Calendar.MINUTE)<60){


    ph=  (int) (Math.sin(thetah) * (h-15));
    bh=  (int) (Math.cos(thetah) * (h-15));


   public static void main(String[] args) {
        Clock m=new Clock();
     }catch(Exception e){



  1. Replies
    1. I am technology Enthusiast. Your blog is really awesome, attractive and impressive. I like the way you think. it is very useful for Java SE & Java EE Learners. Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students. or learn thru JavaScript Online Training from India. Appreciating the persistence you put into your blog and detailed information you provide. Kindly keep blogging.

  2. Excuse me, how can I upload an applet to my blog?

    Thank you very much.

  3. Gud One.Now can you suggest how to calculate points to move minute hand at particular place. (like in voltage indicator)

  4. Impressive - most impressive ;-)

    Try drawing to a buffered image - that will stop the 'blinky thing when used elsewhere.

    Here is what we came up with:

    Thanks for the cool example - Lots of fun to watch!

  5. No one understands this code, first of all, no comments. Variables are called like

    int xn=xmou;
    int yn=ymou;
    //and again!


  6. y that choice of wierd variables,and your comments are very few.felicia ghana

  7. Excellent beat ! I would like to apprentice while you amend your web
    site, how can i subscribe for a blog site?
    The account helped me a acceptable deal. I had been tiny bit acquainted of this your
    broadcast offered bright clear concept

    Here is my web-site :: Airplane simulation Games

  8. My brother recommended I might like this website.
    He was totally right. This post actually made my day. You cann't imagine simply how much time I had spent for this information! Thanks!

    Here is my blog post best registry cleaners

  9. For win 7 download. thanks.

    my page ...

  10. thank's wonderful tip, you are much better than samsung's engineer !

    Here is my webpage :: xerox phaser 8560 driver

  11. Why people still make use of to read news papers when in this
    technological world the whole thing is available on net?

    Feel free to visit my weblog ... xerox 8560 cartridges

  12. When I initially commented I clicked the "Notify me when new comments are added" checkbox and now eachtime a remark is added I acquire several e-mails with thesame remark.
    Exists any way you can eliminate me from that service?

    Here is my site ... xerox phaser 8560 ink

  13. many thanks a great deal ... It assist me lot now planning to purchase HP LaserJet Pro P1106 Printer .

    .. when agin thanks.

    Have a look at my homepage: xerox phaser 8560dn driver

  14. Remarkable things here. I'm very happy to see your post. Thanks so much and I am looking forward to touch you. Will you kindly drop me a mail?

    Feel free to surf to my blog xerox 8560mfp driver

  15. You are wonderful. I've been so annoyed.
    Thanks, thank you, thanks.

    My web blog xerox phaser 8560 driver

  16. Sir, pls send that video clip to my e-mail I.D. It will certainly be aid ful 2
    make pcb effortless. Thankyou.

    my web page: xerox phaser 8560mfp

  17. Pls for your kindness send me the video clip clue of making pcb with hp printer.

    Also visit my blog; xerox phaser 8560 manual

  18. Thanκs foг fіnally tаlking about > "JAVA : Analog Clock" < Loved it!

    Here is my web blog: sign up foг gmаil

  19. Good post. I learn something new and challenging on sites I stumbleupon every day.
    It's always helpful to read articles from other authors and practice a little something from their web sites.

    Feel free to surf to my web site; Acne treatment

  20. Very practical info, I have always favored laser printers due to the fact that they are
    much less costly to use, as soon as you make the initial financial investment.
    Toner cartrages are much more expensive yet last
    longer than many lots of inkjet cartrages and are much less messy
    to utilize. You can easily specify your laser printer to print in grayscale and
    utilize much less of the shade toner, I only utilize colour when printing pictures.

    my weblog - xerox phaser 8560 driver

  21. Nice post. I was checking constantly this blog and I'm impressed! Very useful info specially the last part :) I care for such information a lot. I was seeking this particular information for a long time. Thank you and best of luck.

    Also visit my homepage xerox 8560 part numbers

  22. super.... and cool for looking.. i found your code is very useful for my academic project thanks lot...

  23. This blog is the general information for the feature. You got a good work for these blog.We have a developing our creative content of this mind.
    Thank you for this blog. This for very interesting and useful.
    Java training in Chennai
    Java training in Bangalore
    Java online training
    Java training in Pune
    Java training in Bangalore|best Java training in Bangalore