// Analog Clock Using Swing
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class analogClock extends JFrame
{
JFrame f;
JPanel p;
public analogClock()
{
f=new JFrame("Analog Clock Using Swing");
p=new JPanel()
{
public void paint(Graphics g)
{
Calendar time=Calendar.getInstance();
int hour=time.get(Calendar.HOUR_OF_DAY);
int minute=time.get(Calendar.MINUTE);
int second=time.get(Calendar.SECOND);
if(hour > 12)
{
hour=hour-12;
}
g.setColor(Color.BLACK);
g.fillOval(300,100,200,200);
g.setColor(Color.WHITE);
g.setFont(new Font("Lucida console",Font.BOLD,20));
g.drawString("1",440,130);
g.drawString("2",470,160);
g.drawString("3",480,200);
g.drawString("4",480,240);
g.drawString("5",450,280);
g.drawString("6",400,290);
g.drawString("7",341,278);
g.drawString("8",310,240);
g.drawString("9",310,200);
g.drawString("10",320,160);
g.drawString("11",350,130);
g.drawString("12",390,120);
double angle;
int x,y;
Graphics2D g2d1=(Graphics2D)g;
Stroke s1=(new BasicStroke(2f));
g2d1.setStroke(s1);
angle=Math.toRadians((15-second)*6);
x=(int)(Math.cos(angle)*100);
y=(int)(Math.sin(angle)*100);
g.setColor(Color.RED);
g.drawLine(400,200,400+x,200-y);
Graphics2D g2d2=(Graphics2D)g;
Stroke s2=(new BasicStroke(2));
g2d2.setStroke(s2);
angle=Math.toRadians((15-minute)*6);
x=(int)(Math.cos(angle)*80);
y=(int)(Math.sin(angle)*80);
g.setColor(Color.GREEN);
g.drawLine(400,200,400+x,200-y);
Graphics2D g2d3=(Graphics2D)g;
Stroke s3=(new BasicStroke(5));
g2d3.setStroke(s3);
angle=Math.toRadians((15-(hour*5))*6);
x=(int)(Math.cos(angle)*50);
y=(int)(Math.sin(angle)*50);
g.setColor(Color.BLUE);
g.drawLine(400,200,400+x,200-y);
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
repaint();
super.paintComponents(g);
f.getContentPane().setBackground(Color.RED);
}
};
f.add(p);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
f.setSize(800,500);
f.setVisible(true);
}
public static void main(String args[])
{
new analogClock();
}
}