📄 clock.java
字号:
//Clock.javaimport java.util.*;import java.awt.*;import java.text.*;import javax.swing.*;public class Clock extends JApplet implements Runnable { private Thread timer; // 显示时钟线程 private int lastxs, lastys, lastxm, lastym, lastxh, lastyh; // 定义用于绘制时钟各针的坐标参数 private SimpleDateFormat formatter; // 定义格式化日期时间 private String lastdate; // 定义用于存放所显示日期时间的字符串 private Font clockFaceFont; // 定义时钟表盘上的字体 private Date currentDate; // 定义一Date,得到日期时间来显示 private Color handColor; // 定义时钟表针的颜色 private Color numberColor; // 定义时钟表盘上数字的颜色 private int xcenter = 80, ycenter = 55; // 定义时钟表盘中心点 public void init() { int x,y; lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0; formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy", Locale.getDefault()); currentDate = new Date(); lastdate = formatter.format(currentDate); clockFaceFont = new Font("Serif", Font.PLAIN, 14); handColor = Color.blue; numberColor = Color.darkGray; getContentPane().setBackground(Color.LIGHT_GRAY); resize(200,150); // 设置时钟窗口大小 } // 程序的主体绘制部分 public void update(Graphics g) { int xh, yh, xm, ym, xs, ys; int s = 0, m = 10, h = 10; String today; currentDate = new Date(); formatter.applyPattern("s"); try { s = Integer.parseInt(formatter.format(currentDate)); } catch (NumberFormatException n) { s = 0; } formatter.applyPattern("m"); try {m = Integer.parseInt(formatter.format(currentDate)); } catch (NumberFormatException n) { m = 10; } formatter.applyPattern("h"); try { h = Integer.parseInt(formatter.format(currentDate)); } catch (NumberFormatException n) { h = 10; } // 设置各时钟表针的末端坐标 xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 45 + xcenter); ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 45 + ycenter); xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + xcenter); ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + ycenter); xh = (int) (Math.cos((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30 + xcenter); yh = (int) (Math.sin((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30 + ycenter); // 在窗口底部输出当前日期时间 formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy"); today = formatter.format(currentDate); g.setFont(clockFaceFont); // 如果时间已经改变,以背景色重绘也就是清除当前各时钟表针 g.setColor(getContentPane().getBackground()); if (xs != lastxs || ys != lastys) { g.drawLine(xcenter, ycenter, lastxs, lastys); g.drawString(lastdate, 5, 125); } if (xm != lastxm || ym != lastym) { g.drawLine(xcenter, ycenter-1, lastxm, lastym); g.drawLine(xcenter-1, ycenter, lastxm, lastym); } if (xh != lastxh || yh != lastyh) { g.drawLine(xcenter, ycenter-1, lastxh, lastyh); g.drawLine(xcenter-1, ycenter, lastxh, lastyh); } // 绘制时钟各表针 g.setColor(numberColor); g.drawString(today, 5, 125); g.drawLine(xcenter, ycenter, xs, ys); g.setColor(handColor); g.drawLine(xcenter, ycenter-1, xm, ym); g.drawLine(xcenter-1, ycenter, xm, ym); g.drawLine(xcenter, ycenter-1, xh, yh); g.drawLine(xcenter-1, ycenter, xh, yh); lastxs = xs; lastys = ys; lastxm = xm; lastym = ym; lastxh = xh; lastyh = yh; lastdate = today; currentDate = null; } public void paint(Graphics g) { super.paint(g); g.setFont(clockFaceFont); // 画出时钟表盘和数字 g.setColor(handColor); g.drawArc(xcenter-50, ycenter-50, 100, 100, 0, 360); g.setColor(numberColor); g.drawString("9", xcenter-45, ycenter+3); g.drawString("3", xcenter+40, ycenter+3); g.drawString("12", xcenter-5, ycenter-37); g.drawString("6", xcenter-3, ycenter+45); // 画出当前日期时间和时钟各指针 g.setColor(numberColor); g.drawString(lastdate, 5, 125); g.drawLine(xcenter, ycenter, lastxs, lastys); g.setColor(handColor); g.drawLine(xcenter, ycenter-1, lastxm, lastym); g.drawLine(xcenter-1, ycenter, lastxm, lastym); g.drawLine(xcenter, ycenter-1, lastxh, lastyh); g.drawLine(xcenter-1, ycenter, lastxh, lastyh); } public void start() { timer = new Thread(this); timer.start(); } public void stop() { timer = null; } public void run() { while (timer != null) { repaint(); try { Thread.currentThread().sleep(100); } catch (InterruptedException e) {} } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -