[java]代码库
package s0402模拟时钟;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import 工具类.ThreadSleep;
public class Copy extends JFrame implements Runnable {
final int Xpoint = 180;
final int Ypoint = 180;
final int R = 80;
int xHour = 0, yHour = 0, xSecond = 0, ySecond = 0, xMin = 0, yMin = 0;
public Copy() {
setFont(new Font("仿宋体", Font.BOLD, 24)); // 设置时钟的显示字体
new Thread(this).start();
setLocationRelativeTo(null); // 设置窗体出现位置居中
setSize(360, 360); // 设置窗口尺寸
setVisible(true); // 窗口可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 关闭窗口时退出程序
}
public void run() {
while (true) {
repaint(); // 调用paint方法重绘界面
ThreadSleep.threadSleep(1000);
}
}
public void paint(Graphics g) {
Calendar now = Calendar.getInstance(); // 实例化日历对象
now.setTime(new Date());// new Date()为获取当前系统时间
// 得到具体的时间,以string 格式保存在timeInfo中************************
int hour = now.get(Calendar.HOUR_OF_DAY); // 得到小时数
int minute = now.get(Calendar.MINUTE); // 得到分数
int second = now.get(Calendar.SECOND); // 得到秒数
String date=new SimpleDateFormat("yyyy年mm月dd日").format(new Date());//得到日期,保存在date中
String time=new SimpleDateFormat("hh:mm:ss").format(new Date()); //得到时间,保存在time中
// 得到具体的时间,以string 格式保存在timeInfo中************************
g.setColor(Color.WHITE); // 设置当前颜色为白色,为了让黑点显示出来
g.fillRect(0, 0, this.getWidth(), this.getHeight()); // 填充背景色为白色,相当于线程启动一次填充一次,才不会让上次所执行的操作留下残余
g.setColor(Color.green); // 设置当前时间颜色为绿色
g.drawString(date,20, 60);// 显示年月日字符串
g.drawString(time, 130, 340); // 显示时分秒字符串
// 画出时钟界面
for (int i = 0, num = 0; i < 360; i += 6)// i为度数,每一秒有6°
{
double alfa = Math.toRadians(i); // 获得弧度数
int xPos = Xpoint + (int) (R * Math.sin(alfa));
int yPos = Ypoint - (int) (R * Math.cos(alfa));
if (i % 90 == 0) {
if (num == 0) {
num = 12;
g.setColor(Color.RED);
g.drawString("" + num, xPos - 5, yPos + 3);
num = 3;
} else if (num == 3) {
g.setColor(Color.BLUE);
g.drawString("" + num, xPos - 5, yPos + 3);
num = 6;
} else if (num == 6) {
g.setColor(Color.GRAY);
g.drawString("" + num, xPos - 5, yPos + 3);
num = 9;
} else {
g.setColor(Color.MAGENTA);
g.drawString("" + num, xPos - 5, yPos + 3);
}
} else {
g.setColor(Color.GRAY);
g.drawString(".", xPos, yPos);
}
}
g.setColor(Color.black);
g.fillOval(Xpoint - 4, Ypoint - 4, 8, 8);
// 画秒针
xSecond = (int) (Xpoint + (R - 10)
* Math.sin(second * (2 * Math.PI / 60)));
ySecond = (int) (Ypoint - (R - 10)
* Math.cos(second * (2 * Math.PI / 60)));
g.setColor(Color.red);
g.drawLine(Xpoint, Ypoint, xSecond, ySecond);
// 画分针
xMin = (int) (Xpoint + (R - 35)
* Math.sin((minute + second / 60) * (2 * Math.PI / 60)));
yMin = (int) (Ypoint - (R - 35)
* Math.cos((minute + second / 60) * (2 * Math.PI / 60)));
g.setColor(Color.BLUE);
g.drawLine(Xpoint, Ypoint, xMin, yMin);
// 画时针
xHour = (int) (Xpoint + (R - 50)
* Math.sin((hour + minute / 60 + second / 60 / 60)
* (2 * Math.PI / 12)));
yHour = (int) (Ypoint - (R - 50)
* Math.cos((hour + minute / 60 + second / 60 / 60)
* (2 * Math.PI / 12)));
g.setColor(Color.BLACK);
g.drawLine(Xpoint, Ypoint, xHour, yHour);
}
public static void main(String[] args) {
new Copy();
}
}