Hello guys!
Today we are creating a clock
with using java program. We are using some packages like SimpleDateFormat. And
also we are using the variables for coordinate x and y to declare center
coordinate of a clock, second, minute and hour.
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import
java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ClockEx extends
JPanel implements Runnable
{
Thread thread = null;
SimpleDateFormat formatter = new
SimpleDateFormat("s", Locale.getDefault());
Date currentDate;
//declareing the variables for
coordinates and x_sec,x_min,x_hw,y_sec,y_min, and y_hw defining the //end coordinate of the minutes
and hours.
int x = 175, y = 175, x_sec = 0,
y_sec = 0, x_min = 0, y_min = 0, x_hw = 0,y_hw= 0;
private void
drawStructure(Graphics gr)
{
gr.setFont(new
Font("TimesRoman", Font.BOLD, 20));
gr.setColor(Color.yellow);
gr.fillOval(x - 150, y - 150,
300, 300);
gr.setColor(Color.gray);
gr.drawString("JAYANT"
,130, 60);
gr.setColor(Color.blue);
gr.drawString("9", x -
145, y +0); // difining the
gr.drawString("12", x -
10, y - 130);
gr.drawString("6", x -
10, y + 145);
}
public void paint(Graphics gr1) {
int xhour, yhour, xminute,
yminute, xsecond, ysecond, second, minute, hour;drawStructure
(gr1);
currentDate = new Date();
formatter.applyPattern("s");
second =
Integer.parseInt(formatter.format(currentDate));
formatter.applyPattern("m");
minute =
Integer.parseInt(formatter.format(currentDate));
formatter.applyPattern("h");
hour =
Integer.parseInt(formatter.format(currentDate));
xsecond = (int) (Math.cos(second
* 3.14f / 30 - 3.14f / 2) * 120 + x);
ysecond = (int) (Math.sin(second
* 3.14f / 30 - 3.14f / 2) * 120 + y);
xminute = (int) (Math.cos(minute
* 3.14f / 30 - 3.14f / 2) * 100 + x);
yminute = (int) (Math.sin(minute
* 3.14f / 30 - 3.14f / 2) * 100 + y);
xhour = (int) (Math.cos((hour *
30 + minute / 2) * 3.14f / 180 - 3.14f / 2) * 80 + x);
yhour = (int) (Math.sin((hour *
30 + minute / 2) * 3.14f / 180 - 3.14f / 2) * 80 + y);
// Erase if necessary, and redraw
gr1.setColor(Color.pink);
if (xsecond != x_sec || ysecond
!= y_sec)
{
gr1.drawLine(x, y,x_sec, y_sec);
}
if (xminute != x_min || yminute
!= y_min)
{
gr1.drawLine(x , y - 1, x_min, y_min);
gr1.drawLine(x - 1, y , x_min, y_min);
}
if (xhour != x_hw || yhour !=
y_hw)
{
gr1.drawLine(x , y - 1, x_hw, y_hw);
gr1.drawLine(x - 1, y , x_hw, y_hw);
}
gr1.setColor(Color.pink);
gr1.drawLine(x , y , xsecond,
ysecond);
gr1.setColor(Color.red);
gr1.drawLine(x , y - 1, xminute, yminute);
gr1.drawLine(x - 1, y , xminute, yminute);
gr1.setColor(Color.green);
gr1.drawLine(x , y - 1, xhour, yhour);
gr1.drawLine(x - 1, y , xhour, yhour);
x_sec = xsecond;
y_sec = ysecond;
x_min = xminute;
y_min = yminute;
x_hw = xhour;
y_hw = yhour;
}
public void start() {
if (thread == null) {
thread = new Thread(this);
thread.start();
}
}
public void stop()
{
thread = null;
}
public void run() {
while (thread != null) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
repaint();
}
thread = null;
}
public void update(Graphics g) {
paint(g);
}
public static void main(String args[]) {
JFrame window = new JFrame();
Color c=new Color(118,73,190);
window.setBackground(c);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(0, 0, 400, 400);
ClockEx clock = new ClockEx();
window.getContentPane().add(clock);
window.setVisible(true);
clock.start();
}
}
OUTPUT :
0 comments:
Post a Comment