__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 clockCalendar 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){ } } } } |

nice ......very nice

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

DeleteExcuse me, how can I upload an applet to my blog?

ReplyDeleteThank you very much.

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

ReplyDeleteImpressive - most impressive ;-)

ReplyDeleteTry 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!

so cool :)

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

ReplyDeleteint xn=xmou;

int yn=ymou;

//again

xm=xmou;

ym=ymou;,

//and again!

x=xmou;

y=ymou;

What???

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

ReplyDeletenyc bro

ReplyDeleteExcellent beat ! I would like to apprentice while you amend your web

ReplyDeletesite, 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

My brother recommended I might like this website.

ReplyDeleteHe 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

For win 7 download. thanks.

ReplyDeletemy page ... samul.db.ac.kr

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

ReplyDelete!!

Here is my webpage :: xerox phaser 8560 driver

Why people still make use of to read news papers when in this

ReplyDeletetechnological world the whole thing is available on net?

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

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.

ReplyDeleteExists any way you can eliminate me from that service?

Thanks!

Here is my site ... xerox phaser 8560 ink

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

ReplyDelete.. when agin thanks.

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

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?

ReplyDeleteFeel free to surf to my blog xerox 8560mfp driver

You are wonderful. I've been so annoyed.

ReplyDeleteThanks, thank you, thanks.

Patty.

My web blog xerox phaser 8560 driver

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

ReplyDeletemake pcb effortless. Thankyou.

my web page: xerox phaser 8560mfp

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

ReplyDeleteAlso visit my blog; xerox phaser 8560 manual

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

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

Good post. I learn something new and challenging on sites I stumbleupon every day.

ReplyDeleteIt'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

Very practical info, I have always favored laser printers due to the fact that they are

ReplyDeletemuch 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

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.

ReplyDeleteAlso visit my homepage xerox 8560 part numbers

really awesome...

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

ReplyDelete