Skip to content

Commit b8ddc15

Browse files
Create clock.java
1 parent 776639e commit b8ddc15

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

clock.java

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import java.awt.Color;
2+
import java.awt.Font;
3+
import java.awt.Graphics;
4+
import java.text.SimpleDateFormat;
5+
import java.util.Date;
6+
import java.util.Locale;
7+
import javax.swing.JFrame;
8+
import javax.swing.JPanel;
9+
public class Clock extends JPanel implements Runnable
10+
{
11+
Thread thread = null;
12+
SimpleDateFormat formatter = new SimpleDateFormat("s", Locale.getDefault());
13+
Date currentDate;
14+
int xcenter = 175, ycenter = 175, lastxs = 0, lastys = 0, lastxm = 0, lastym = 0, lastxh = 0, lastyh = 0;
15+
private void drawStructure(Graphics g) {
16+
g.setFont(new Font("TimesRoman", Font.BOLD, 20));
17+
g.setColor(Color.black);
18+
g.fillOval(xcenter - 150, ycenter - 150, 300, 300);
19+
g.setColor(Color.blue);
20+
g.drawString("abhishek dubey", 113, 300);
21+
g.setColor(Color.green);
22+
g.drawString("9", xcenter - 145, ycenter + 0);
23+
g.drawString("3", xcenter + 135, ycenter + 0);
24+
g.drawString("12", xcenter - 10, ycenter - 130);
25+
g.drawString("6", xcenter - 10, ycenter + 145);
26+
}
27+
public void paint(Graphics g)
28+
{
29+
int xhour, yhour, xminute, yminute, xsecond, ysecond, second, minute, hour;
30+
drawStructure(g);
31+
currentDate = new Date();
32+
formatter.applyPattern("s");
33+
second = Integer.parseInt(formatter.format(currentDate));
34+
formatter.applyPattern("m");
35+
minute = Integer.parseInt(formatter.format(currentDate));
36+
formatter.applyPattern("h");
37+
hour = Integer.parseInt(formatter.format(currentDate));
38+
xsecond = (int)(Math.cos(second * 3.14 f / 30 - 3.14 f / 2) * 120 + xcenter);
39+
ysecond = (int)(Math.sin(second * 3.14 f / 30 - 3.14 f / 2) * 120 + ycenter);
40+
xminute = (int)(Math.cos(minute * 3.14 f / 30 - 3.14 f / 2) * 100 + xcenter);
41+
yminute = (int)(Math.sin(minute * 3.14 f / 30 - 3.14 f / 2) * 100 + ycenter);
42+
xhour = (int)(Math.cos((hour * 30 + minute / 2) * 3.14 f / 180 - 3.14 f / 2) * 80 + xcenter);
43+
yhour = (int)(Math.sin((hour * 30 + minute / 2) * 3.14 f / 180 - 3.14 f / 2) * 80 + ycenter);
44+
// Erase if necessary, and redraw
45+
g.setColor(Color.magenta);
46+
if (xsecond != lastxs || ysecond != lastys)
47+
{
48+
g.drawLine(xcenter, ycenter, lastxs, lastys);
49+
}
50+
if (xminute != lastxm || yminute != lastym)
51+
{
52+
g.drawLine(xcenter, ycenter - 1, lastxm, lastym);
53+
g.drawLine(xcenter - 1, ycenter, lastxm, lastym);
54+
}
55+
if (xhour != lastxh || yhour != lastyh)
56+
{
57+
g.drawLine(xcenter, ycenter - 1, lastxh, lastyh);
58+
g.drawLine(xcenter - 1, ycenter, lastxh, lastyh);
59+
}
60+
g.setColor(Color.magenta);
61+
g.drawLine(xcenter, ycenter, xsecond, ysecond);
62+
g.setColor(Color.red);
63+
g.drawLine(xcenter, ycenter - 1, xminute, yminute);
64+
g.drawLine(xcenter - 1, ycenter, xminute, yminute);
65+
g.setColor(Color.green);
66+
g.drawLine(xcenter, ycenter - 1, xhour, yhour);
67+
g.drawLine(xcenter - 1, ycenter, xhour, yhour);
68+
lastxs = xsecond;
69+
lastys = ysecond;
70+
lastxm = xminute;
71+
lastym = yminute;
72+
lastxh = xhour;
73+
lastyh = yhour;
74+
}
75+
public void start()
76+
{
77+
if (thread == null)
78+
{
79+
thread = new Thread(this);
80+
thread.start();
81+
}
82+
}
83+
public void stop()
84+
{
85+
thread = null;
86+
}
87+
public void run()
88+
{
89+
while (thread != null)
90+
{
91+
try
92+
{
93+
Thread.sleep(100);
94+
}
95+
catch (InterruptedException e) {}
96+
repaint();
97+
}
98+
thread = null;
99+
}
100+
public void update(Graphics g)
101+
{
102+
paint(g);
103+
}
104+
public static void main(String args[])
105+
{
106+
JFrame window = new JFrame();
107+
Color c = new Color(118, 73, 190);
108+
window.setBackground(c);
109+
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
110+
window.setBounds(0, 0, 400, 400);
111+
Clock clock = new Clock();
112+
window.getContentPane().add(clock);
113+
window.setVisible(true);
114+
clock.start();
115+
}
116+
}

0 commit comments

Comments
 (0)