Advertisement
Codex

Analog Clock Applet

Jul 24th, 2011
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.50 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.util.*;
  5.  
  6. public class Clock extends Frame implements MouseMotionListener{
  7. int xmou=200; //set the center of circle
  8. int ymou=200; //set the center of circle
  9.  
  10. double theta=-0.1047; //theta for second's hand
  11. int x=xmou;  //x position of Second's hand
  12. int y=ymou;  //y position of second's hand
  13. int p,b;   //perpendicular and base of Second's hand
  14.  
  15. int h;   //hypotenous(heigth) of clock's hand
  16.  
  17.  
  18. double the= -0.1047;  //theta for creating outer circle
  19.  
  20. double thetamin=-0.1047;        //theta for minutes hand
  21. int xm=xmou;   //x position of minute's hand
  22. int ym=ymou;   //y position of minute's hand
  23. int pmin,bmin;   //perpendicular and base of Minute's hand
  24.  
  25.  
  26. double thetah=-0.1047;         //theta for hour hand
  27. int xh=xmou;   //y position of hour's hand
  28. int yh=ymou;   //y position of hour's hand
  29. int ph,bh;   //perpendicular and base of hour's hand
  30.  
  31.  
  32. double thetan=-0.0;          //theta for numbers of clock
  33. int xn=xmou;   //x position of Clock numbers
  34. int yn=ymou;   //y position of Clock numbers
  35. int pn,bn;   //perpendicular and base of clock numbers
  36. int num=0;   //for writing the numbers
  37.  
  38.  
  39. //constructor
  40.  
  41. Clock(){
  42. super();
  43. setSize(500,500);
  44. setBackground(Color.PINK);
  45. setVisible(true);
  46. addMouseMotionListener(this);
  47. }
  48.  
  49.  
  50. //method of implemented  mouse interface
  51. public void mouseMoved(MouseEvent me){
  52.  
  53. }
  54.  
  55. public void mouseDragged(MouseEvent me){
  56. xmou=me.getX();   //changing the clock position on mouse drag
  57. ymou=me.getY();   //changing the clock position on mouse drag
  58.  
  59. }
  60.  
  61.  
  62. //method to paint clock
  63. public void paint(Graphics g){
  64.        
  65.            //for writing numbers in clock and outer circle
  66.  
  67.  for(int p=0;p<60;p++){
  68.    
  69.          int xocir=xmou;      //x position of outer circle
  70.       int yocir=ymou;      //y position of outer circle
  71.       int pocir,bocir;     //perpendicular and base of outer circle
  72.  
  73.             pocir=  (int) (Math.sin(the) * (h+23));
  74.             bocir=  (int) (Math.cos(the) * (h+23));
  75.             xocir=xocir-pocir;
  76.             yocir=yocir-bocir;
  77.             the=the - 0.1047;
  78.  
  79.                  g.setColor(Color.BLUE);
  80.           g.drawLine(xocir+5,yocir+5,xocir,yocir);
  81.                 g.setColor(Color.BLACK);
  82.  if(p%5==0 ){
  83.   num++;
  84.   if(num>12){
  85.     num=1;
  86.   }
  87.  
  88.   xn=xmou;
  89.        yn=ymou;
  90.  
  91.   if(thetan<=-6.28318531 ){
  92.    thetan=0.0;
  93.  
  94.   }
  95.   thetan=thetan-0.523598776 ;
  96.       pn=  (int) (Math.sin(thetan) * (h+10));
  97.       bn=  (int) (Math.cos(thetan) * (h+10));
  98.       xn=xn-pn;
  99.        yn=yn-bn;
  100.   g.drawString(""+num,xn-3,yn+5);
  101.  
  102.     }      
  103.  
  104.      }
  105.    
  106.        //for drawing Clock hands
  107.  
  108.      g.setColor(Color.BLACK);  
  109.    
  110.      g.drawLine(xmou,ymou,xm,ym); //drawing minute's hand
  111.      g.drawLine(xmou,ymou,xh,yh); //drawing hour's hand
  112.    
  113.      g.setColor(Color.RED);
  114.      g.drawLine(xmou,ymou,x,y);  //drawing second's hand
  115.        
  116.     }
  117.  
  118.  
  119.  public void newpoint(){
  120.  
  121.     Calendar now = Calendar.getInstance(); //creating a Calendar variable for getting current time
  122.  
  123.     //for second hand
  124.  
  125.     x=xmou;
  126.     y=ymou;
  127.     theta=-0.1047;
  128.     theta=theta*now.get(Calendar.SECOND);
  129.     p=  (int) (Math.sin(theta) * h);
  130.     b=  (int) (Math.cos(theta) * h);
  131.     x=x-p;
  132.     y=y-b;
  133.     //theta=theta - 0.1047;
  134.  
  135.   //for minutes hand
  136.  
  137.     xm=xmou;
  138.     ym=ymou;
  139.  
  140.     thetamin=-0.1047;
  141.     thetamin=thetamin*now.get(Calendar.MINUTE);
  142.     pmin=  (int) (Math.sin(thetamin) * (h-6));
  143.     bmin=  (int) (Math.cos(thetamin) * (h-6));
  144.     xm=xm-pmin;
  145.     ym=ym-bmin;
  146.  
  147.     //for hour's hand
  148.  
  149.     xh=xmou;
  150.     yh=ymou;
  151.     thetah=-0.1047;
  152.     thetah=thetah*now.get(Calendar.HOUR)*5;
  153.  
  154.  
  155.    if (now.get(Calendar.MINUTE)>=12 && now.get(Calendar.MINUTE)<24){
  156.   thetah=thetah-0.1047;
  157.  
  158.  
  159.    }
  160.    else if(now.get(Calendar.MINUTE)>=24 && now.get(Calendar.MINUTE)<36){
  161.   thetah=thetah-(2*0.1047);
  162.  
  163.  
  164.    }
  165.    else if(now.get(Calendar.MINUTE)>=36 && now.get(Calendar.MINUTE)<48){
  166.   thetah=thetah-(3*0.1047);
  167.  
  168.  
  169.    }
  170.    else if(now.get(Calendar.MINUTE)>=48 && now.get(Calendar.MINUTE)<60){
  171.   thetah=thetah-(4*0.1047);
  172.  
  173.  
  174.    }
  175.  
  176.  
  177.     ph=  (int) (Math.sin(thetah) * (h-15));
  178.     bh=  (int) (Math.cos(thetah) * (h-15));
  179.     xh=xh-ph;
  180.     yh=yh-bh;
  181.  
  182.  
  183.  
  184.  
  185.  }
  186.    
  187.  
  188.    public static void main(String[] args) {
  189.      
  190.         Clock m=new Clock();
  191.         m.h=60;
  192.      
  193.          while(true){
  194.    
  195.          m.newpoint();
  196.          m.repaint();
  197.          try{
  198.      Thread.sleep(6);
  199.      }catch(Exception e){
  200.      
  201.      }    
  202.       }
  203.     }
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement