Friday, 19 June 2015

Analog Clock program in java

1]. Seconds hand completes 360 degrees in 1 minute.
2]. Minute hand completes 360 degrees in 1 hour.
3]. Hour hand completes 360 degrees in 12 hours.
4]. Set of points traced by circle is given by :
                                                        x=cx+r*angle; y=cy+r*angle;
# (cx,cy)=coordinates of centre of circle.
# r= radius.
                                      => i.e path traced by minute and seconds hands in same.

                                      =>Where as hour hand cover 5 degrees when minute hand sweeps 360 degrees.(360/5=12 used in looping).





                                                                                      ......................HAPPY CODING

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Random;
public class WallClock extends Applet implements Runnable {
Thread ct= null ;
int cx=330,cy=230,rs=100,rm=75,rh=55,pxs,pys,pxm,pym,pxh,pyh;int  ts=0,tm=0,th=0;float cs,cm,ch;
public void start() {
   if (ct == null) {
     ct = new Thread(this);
     ct.start();}}

 public void run() {
   Thread myThread = Thread.currentThread();
   while (ct == myThread) { repaint(); }  }
public void paint(Graphics g)
{Font f = new Font("MV Boli",Font.ITALIC,30);
    g.setFont(f);
    int R = (int)(Math.random()*256);
    int G = (int)(Math.random()*256);
    int B= (int)(Math.random()*256);
    Color c = new Color(R, G, B);
    g.setColor(c);
g.drawString("ViswaDeep",460,380);

g.setColor(Color.pink);
g.fillOval(200, 100, 260, 260);
g.setColor(Color.orange);
g.drawOval(199, 99, 262, 262);
g.drawOval(198, 98, 264, 264);
g.drawOval(197, 97, 266, 266);
g.drawOval(196, 96, 268, 268);
Font font = new Font("Arial",Font.BOLD,30);
   g.setFont(font);
g.setColor(Color.darkGray);
g.drawString("12", 313, 130);
g.drawString("6", 325, 352);
g.drawString("9", 206, 245);
g.drawString("3", 430, 240);

if(ts%60==0&&ts!=0){tm++;}
cm=(float) (0.105*tm);
pxm = (int) (cx + rm * Math.sin(cm));
pym = (int) (cy - rm * Math.cos(cm));
   g.setColor(Color.red);
g.drawLine(cx, cy, pxm, pym);

if(tm%12==0&&tm!=0){if(ts%60==0&&ts!=0){th++;}}
ch=(float) (0.105*th);
pxh = (int) (cx + rh * Math.sin(ch));
pyh = (int) (cy - rh * Math.cos(ch));
   g.setColor(Color.BLUE);
g.drawLine(cx, cy, pxh, pyh);

cs=(float) (0.105*ts);
pxs = (int) (cx + rs * Math.sin(cs));
pys = (int) (cy - rs * Math.cos(cs));
g.setColor(Color.black);
g.drawLine(cx, cy, pxs, pys);
try {Thread.sleep(1000);} catch (InterruptedException e){ }

ts++;
}
}

1 comment: