java编写时钟代码 java编写时钟类( 二 )


int h = hour % 12;
a = second; // 秒针端点的坐标
b = munite; // 分针端点的坐标
c = h * 5 + munite / 12; // 时针端点的坐标
secondLine.setLine(120, 120, (int) pointSX[a], (int) pointSY[a]);
muniteLine.setLine(120, 120, (int) pointMX[b], (int) pointMY[b]);
hourLine.setLine(120, 120, (int) pointHX[c], (int) pointHY[c]);
repaint();
}
if (e.getSource() == jb) {
secondTime.start();
}
if (e.getSource() == jb1) {
secondTime.stop();
}
}
public static void main(String args[]) {
JFrame win = new JFrame("时钟");
JPanel jp = new JPanel();
jp.add(jb);
jp.add(jb1);
ClockAs clock = new ClockAs();
jb.addActionListener(clock);
jb1.addActionListener(clock);
win.add(clock, BorderLayout.CENTER);
win.add(jp, "South");
win.setVisible(true);
win.setSize(246, 300);
win.setDefaultCloseOperation(3);
win.validate();
}
}
怎么用java编写时钟呀?import
java.util.*;
import
java.awt.*;
import
java.applet.*;
//impelements
Runnable
是线程程序java编写时钟代码的接口
public
class
Clock
extends
Applet
implements
Runnable
{
Thread
timer
=
null;
//
定义线程实体timer
int
xcenter
=
400,
ycenter
=
50;
int
Radius
=
ycenter
-
5;
public
void
init()
{
resize(400,
125);//
设置时钟程序的窗口大小
setBackground(Color.white);//
设置小应用程序的背景色
}
public
void
paint(Graphics
g)
{
int
xh,
yh,
xm,
ym,
xs,
ys,
s,
m,
h;
String
today;
Date
dat
=
new
Date();
//
定义时间类dat
s
=
dat.getSeconds();
//
获得时间秒
m
=
dat.getMinutes();
//
获得时间分
h
=
dat.getHours();
today
=
dat.toLocaleString();
//
获得字符串时间格式
g.clearRect(0,
0,
size().width,
size().height);
//
消除小应用程序
xcenter
=
xcenter
-
1;
//
向左移动一个像素点
if
(xcenter
-50)
xcenter
=
400;
//
如果xcenter小于-50java编写时钟代码,则回到初始位置
//
计算秒的坐标
xs
=
(int)
(Math.cos(s
*
3.14f
/
30
-
3.14f
/
2)
*
(Radius
-
5)
+
xcenter);
ys
=
(int)
(Math.sin(s
*
3.14f
/
30
-
3.14f
/
2)
*
(Radius
-
5)
+
ycenter);
//
计算分钟的坐标
xm
=
(int)
(Math.cos(m
*
3.14f
/
30
-
3.14f
/
2)
*
(Radius
-
10)
+
xcenter);
ym
=
(int)
(Math.sin(m
*
3.14f
/
30
-
3.14f
/
2)
*
(Radius
-
10)
+
ycenter);
//
计算小时的坐标
xh
=
(int)
(Math.cos((h
*
30
+
m
/
2)
*
3.14f
/
180
-
3.14f
/
2)
*
(Radius
-
20)
+
xcenter);
yh
=
(int)
(Math.sin((h
*
30
+
m
/
2)
*

推荐阅读