用 java 写一个clock的类 , 100毫秒的时钟 求代码 。。越简单越好 。。最好有注释这里有一个时钟的类,你看一下吧,不明白你的100毫秒的时钟 是什么意思 。
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Line2D;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ClockAs extends Canvas implements ActionListener {
static JButton jb = new JButton("开始");
static JButton jb1 = new JButton("暂停");
Date date;
Timer secondTime;
int hour, munite, second;
Line2D secondLine, muniteLine, hourLine;
int a, b, c;
double pointSX[] = new double[60],// 用来表示秒针端点坐标的数组
pointSY[] = new double[60], pointMX[] = new double[60], // 用来表示分针端点坐标的数组
pointMY[] = new double[60], pointHX[] = new double[60], // 用来表示时针端点坐标的数组
pointHY[] = new double[60];
ClockAs() {
secondTime = new Timer(1000, this);
pointSX[0] = 0; // 12点秒针位置
pointSY[0] = -100;
pointMX[0] = 0; // 12点分针位置
pointMY[0] = -90;
pointHX[0] = 0; // 12点时针位置
pointHY[0] = -70;
double angle = 6 * Math.PI / 180; // 刻度为6度
for (int i = 0; i59; i++) // 计算出各个数组中的坐标
{
pointSX[i + 1] = pointSX[i] * Math.cos(angle) - Math.sin(angle)
* pointSY[i];
pointSY[i + 1] = pointSY[i] * Math.cos(angle) + pointSX[i]
* Math.sin(angle);
pointMX[i + 1] = pointMX[i] * Math.cos(angle) - Math.sin(angle)
* pointMY[i];
pointMY[i + 1] = pointMY[i] * Math.cos(angle) + pointMX[i]
* Math.sin(angle);
pointHX[i + 1] = pointHX[i] * Math.cos(angle) - Math.sin(angle)
* pointHY[i];
pointHY[i + 1] = pointHY[i] * Math.cos(angle) + pointHX[i]
* Math.sin(angle);
}
for (int i = 0; i60; i++) {
pointSX[i] = pointSX[i] + 120; // 坐标平移
pointSY[i] = pointSY[i] + 120;
pointMX[i] = pointMX[i] + 120; // 坐标平移
pointMY[i] = pointMY[i] + 120;
pointHX[i] = pointHX[i] + 120; // 坐标平移
pointHY[i] = pointHY[i] + 120;
}
secondLine = new Line2D.Double(0, 0, 0, 0);
muniteLine = new Line2D.Double(0, 0, 0, 0);
hourLine = new Line2D.Double(0, 0, 0, 0);
secondTime.start(); // 秒针开始计时
}
public void paint(Graphics g) {
for (int i = 0; i60; i++) // 绘制表盘上的小刻度和大刻度
{
int m = (int) pointSX[i];
int n = (int) pointSY[i];
if (i % 5 == 0) {
g.setColor(Color.red);
g.fillOval(m - 4, n - 4, 8, 8);
} else {
g.setColor(Color.cyan);
g.fillOval(m - 2, n - 2, 4, 4);
}
}
g.fillOval(115, 115, 10, 10); // 钟表中心的实心圆
Graphics2D g_2d = (Graphics2D) g;
g_2d.setColor(Color.red);
g_2d.draw(secondLine);
BasicStroke bs = new BasicStroke(3f, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_MITER);
g_2d.setStroke(bs);
g_2d.setColor(Color.blue);
g_2d.draw(muniteLine);
bs = new BasicStroke(6f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
g_2d.setStroke(bs);
g_2d.setColor(Color.green);
g_2d.draw(hourLine);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == secondTime) {
date = new Date();
String s = date.toString();
hour = Integer.parseInt(s.substring(11, 13));
munite = Integer.parseInt(s.substring(14, 16));
second = Integer.parseInt(s.substring(17, 19)); // 获取时间中的秒
推荐阅读
- 空间文章公众号推荐,怎么搜空间公众号
- jquery字符转换成数字,jquery 字符串替换
- 女性的体育竞技叫什么游戏,女性的体育竞技叫什么游戏名字
- php数据库需要什么 php需要数据库吗
- linuxpythondev安装的简单介绍
- 区块链物流应用,区块链物流应用场景
- 火箭直播技巧视频app,火箭直播在线直播观看
- php数据库一对多 php查询数据库输出多个结果
- go语言模版语法怎么样,go语言模块