主页 > 程序人生 > Java写的创意时钟,纯属好玩,没任何实用价值的说~

Java写的创意时钟,纯属好玩,没任何实用价值的说~

先上图吧,有个图比较好解释:)

绿色的圈代表秒,蓝色代表分,品红代表小时
分别以当前的秒所占一分钟的比率,当前的分所占一小时的比率,以及当前的小时所占1天的比率画出弧度~
思路相当简单,不过实现起来还是比较好玩的:)
稍稍修改了一下,现在应该符合常识了
编译好的jar文件在这里下载
http://cid-d94239a09a81fece.skydrive.live.com/self.aspx/.Public/aa.jar
代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import javax.swing.*;
import java.util.*;
import java.awt.*;
 
public class ClockPanel {
 
	/**
	 * @param args
	 */
	JFrame frame;
	Canvas ab;
	Clock kk;
	int hh, mm, ss;
	Calendar cal = null;
 
	ClockPanel() {
		frame = new JFrame("Clock");
		frame.setBackground(Color.white);
		ab = new Canvas();
		ab.setSize(400, 400);
		ab.setBackground(Color.red);
		frame.setSize(400, 400);
		frame.add(ab);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		kk = new Clock(this);
		begin();
	}
 
	/**
	 * 画图函数,就是很简单的用Calendar类获得当前时间,然后弧形
	 */
	void draw() {
		Graphics g = ab.getGraphics();
		g.setColor(Color.red);
		g.fillRect(0, 0, 400, 400);
		g.setColor(Color.green);
		cal = Calendar.getInstance();
		hh = cal.get(Calendar.HOUR_OF_DAY);
		mm = cal.get(Calendar.MINUTE);
		ss = cal.get(Calendar.SECOND);
		g.drawString("Nowtime:" + hh + ":" + mm + ":" + ss, 40, 60);
		int ars = 6 * ss;
		int arm = 6 * mm;
		int arh = 360 / 24 * hh;
		g.fillArc(100, 100, 200, 200, 90, -ars);
		g.setColor(Color.blue);
		g.fillArc(130, 130, 140, 140, 90, -arm);
		g.setColor(Color.magenta);
		g.fillArc(150, 150, 80, 80, 90, -arh);
	}
 
	void begin() {
		(new Thread(kk)).start();// 线程启动!
	}
 
	public static void main(String[] args) {
		ClockPanel a = new ClockPanel();
	}
 
}
 
/**
 * 这个是用来控制画图的线程,每秒刷新
 * 
 * @author yanglingfeng
 * 
 */
class Clock implements Runnable {
	ClockPanel a = null;
 
	Clock(ClockPanel in) {
		a = in;
		a.draw();
	}
 
	@Override
	public void run() {
		while (true) {
			try {
				a.draw();
				Thread.sleep(1000);// 1秒刷新
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
 
		}
	}
 
}

相关日志

, , , ,

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

引用:0

下面所列的是引用到本博客的链接
Java写的创意时钟,纯属好玩,没任何实用价值的说~ 来自 混沌的云
顶部