Thursday, 16 January 2014

Design Analog Clock Using AWT Swing in Java



                                               Design Analog Clock Using Swing in Java


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Watch extends JPanel implements Runnable
{
JLabel l,l1;
JPanel p,p1;
JButton b,b1;
double x,y,r=90,q=270,x1,y1,x2,y2;
Watch3()
{

x=150+r*Math.cos((q*Math.PI)/180);
y=138+r*Math.sin((q*Math.PI)/180);
x1=150+r*Math.cos((q*Math.PI)/180);
y1=138+r*Math.sin((q*Math.PI)/180);
x2=150+r*Math.cos((q*Math.PI)/180);
y2=138+r*Math.sin((q*Math.PI)/180);

Thread t=new Thread(this);
t.start();
}
public void paint(Graphics g)
{
setBackground(Color.red);
g.setColor(Color.red);
g.drawOval(25,10,250,250);
g.fillOval(25,10,250,250);
g.setColor(Color.green);
///g.setStroke(new BasicStroke(5));
g.drawLine((int)x,(int)y,150,138);
g.drawLine(150,80,150,138);
g.drawLine(130,70,150,138);                                              
g.setColor(Color.green);
g.fillOval(145,135,10,10);
g.setColor(Color.blue);
g.setFont(new Font("snserif",Font.BOLD,20));
g.drawString("12",150,40);
g.drawString("6",150,245);
g.drawString("9",40,140);
g.drawString("3",250,140);
g.drawString("1",205,60);
g.drawString("2",240,95);
g.drawString("11",90,55);
g.drawString("10",55,95);
g.drawString("8",55,190);
g.drawString("VIJAY",120,190);
g.drawString("4",240,190);
g.drawString("7",90,230);
g.drawString("5",205,230);
g.setColor(Color.black);
//g.drawString("VIJAY",120,190);
}
public void run()
{

for(;;)
{
repaint();
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
}
x=150+r*Math.cos((q*Math.PI)/180);
y=138+r*Math.sin((q*Math.PI)/180);

q+=6;
if(q==360)
{
q=0;
}
}
}
public static void main(String...s)
{
JFrame f=new JFrame();
f.setSize(315,350);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Watch3());
f.setVisible(true);

}
}

How to Run
1-Copy this code and save as Watch.java
2-Compile it as- javac Watch.java
3-Run it as- java Watch

No comments:

Post a Comment