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


  x=100;         
    y=100;
    theta=-0.1047;
    theta=theta*now.get(Calendar.SECOND);
   int  p=  (int) (Math.sin(theta) * h);
   int b=  (int) (Math.cos(theta) * h);
    x=x-p;
    y=y-b;

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


//constructor

Clock(){
super();
setSize(500,500);
setBackground(Color.PINK);
setVisible(true);
addMouseMotionListener(this);
}


//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));
            xocir=xocir-pocir;
            yocir=yocir-bocir;
            the=the - 0.1047;

                 g.setColor(Color.BLUE);
         g.drawLine(xocir+5,yocir+5,xocir,yocir);
                g.setColor(Color.BLACK);
if(p%5==0 ){
num++;
if(num>12){
 num=1;
}

xn=xmou;
     yn=ymou;

if(thetan<=-6.28318531 ){
thetan=0.0;

}
thetan=thetan-0.523598776 ;
     pn=  (int) (Math.sin(thetan) * (h+10));
     bn=  (int) (Math.cos(thetan) * (h+10));
     xn=xn-pn;
     yn=yn-bn;
g.drawString(""+num,xn-3,yn+5);

   }      

     }
  
     //for drawing Clock hands

     g.setColor(Color.BLACK);
  
     g.drawLine(xmou,ymou,xm,ym); //drawing minute's hand
     g.drawLine(xmou,ymou,xh,yh); //drawing hour's hand
  
     g.setColor(Color.RED);
     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

    x=xmou;
    y=ymou;
    theta=-0.1047;
    theta=theta*now.get(Calendar.SECOND);
    p=  (int) (Math.sin(theta) * h);
    b=  (int) (Math.cos(theta) * h);
    x=x-p;
    y=y-b;
    //theta=theta - 0.1047;

  //for minutes hand

    xm=xmou;
    ym=ymou;

    thetamin=-0.1047;
    thetamin=thetamin*now.get(Calendar.MINUTE);
    pmin=  (int) (Math.sin(thetamin) * (h-6));
    bmin=  (int) (Math.cos(thetamin) * (h-6));
    xm=xm-pmin;
    ym=ym-bmin;

    //for hour's hand

    xh=xmou;
    yh=ymou;
    thetah=-0.1047;
    thetah=thetah*now.get(Calendar.HOUR)*5;


   if (now.get(Calendar.MINUTE)>=12 && now.get(Calendar.MINUTE)<24){
  thetah=thetah-0.1047;


   }
   else if(now.get(Calendar.MINUTE)>=24 && now.get(Calendar.MINUTE)<36){
  thetah=thetah-(2*0.1047);


   }
   else if(now.get(Calendar.MINUTE)>=36 && now.get(Calendar.MINUTE)<48){
  thetah=thetah-(3*0.1047);


   }
   else if(now.get(Calendar.MINUTE)>=48 && now.get(Calendar.MINUTE)<60){
  thetah=thetah-(4*0.1047);


   }


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




 }
  

   public static void main(String[] args) {
    
        Clock m=new Clock();
        m.h=60;
    
         while(true){
  
         m.newpoint();
         m.repaint();
         try{
     Thread.sleep(6);
     }catch(Exception e){
    
     }    
      }
    }

}

41 comments:

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

    Thank you very much.

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

    ReplyDelete
  3. 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:

    http://www.soft9000.com/blog9000/index.php?entry=Analog-Clock---Combining-two-examples

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

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

    int xn=xmou;
    int yn=ymou;
    //again
    xm=xmou;
    ym=ymou;,
    //and again!
    x=xmou;
    y=ymou;


    What???

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

    ReplyDelete
  6. 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

    ReplyDelete
  7. 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

    ReplyDelete
  8. For win 7 download. thanks.

    my page ... samul.db.ac.kr

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

    Here is my webpage :: xerox phaser 8560 driver

    ReplyDelete
  10. 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

    ReplyDelete
  11. 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?
    Thanks!


    Here is my site ... xerox phaser 8560 ink

    ReplyDelete
  12. 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

    ReplyDelete
  13. 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

    ReplyDelete
  14. You are wonderful. I've been so annoyed.
    Thanks, thank you, thanks.
    Patty.

    My web blog xerox phaser 8560 driver

    ReplyDelete
  15. 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

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



    Also visit my blog; xerox phaser 8560 manual

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

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

    ReplyDelete
  18. 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

    ReplyDelete
  19. 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

    ReplyDelete
  20. 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

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

    ReplyDelete
  22. Thanks For Sharing The Information The information shared Is Very Valuable Please Keep Updating Us Time just went On reading The article Python Online Training Aws Online Training Hadoop Online Training Data Science Online Training

    ReplyDelete
  23. Your site is very good. Actually, I have seen your post and That was very helpful for me. Turkey do i need a visa to visit Turkey ? it Depends on your country's status . However, some countries do not require a visa to visit Turkey. There are about 104 countries that need a Visa so they can enter Turkey. You can also check the list of countries which require or do not require a visa to visit turkey.

    ReplyDelete
  24. http://javacodespot.blogspot.com/2010/05/java-analog-clock.html

    ReplyDelete
  25. Thanks for the helpful advice!
    betpark

    ReplyDelete
  26. Growth oil might significant ask summer. Organization success set possible ball college receive.<a href="https://tinyurl.com/

    ReplyDelete