import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.*;
class TimePanel extends JPanel {
public static int secord = 0, minute = 0, hour = 0, tdate = 0;
Image offScreenImage = null; // 虚拟图片
JLabel label = new JLabel();
// r 半径 ,width 宽度 heigh 高度
final int r = 200, width = 300, heigh = 100, count = 0;
// 数字内半圈 比外径小20
final int nr = r - 20, nheigh = heigh + 20;
// 秒钟 比外径小50
final int sr = r - 50, sheigh = heigh + 50;
// 分钟 比外径小 60
final int mr = r - 60, mheigh = heigh + 60;
// 分钟 比外径小 80
final int hr = r - 100, hheigh = heigh + 100;
TimePanel() {
add(label);
java.util.Timer time1;
TimerTask timetask;
time1 = new java.util.Timer();
timetask = new TimerTask() {
public void run() {
Date date = new Date(); // 日期类型
secord = Integer.parseInt(String.format("%tS", date)); // 取到当前秒数
minute = Integer.parseInt(String.format("%tM", date));
hour = Integer.parseInt(String.format("%tl", date));
tdate = Integer.parseInt(String.format("%td", date));
// System.out.println(secord);
label.setText(String.format("%tF", date) + " " + String.format("%tT", date));
repaint();
}
};
time1.schedule(timetask, 10, 1000);
}
// public void update(Graphics g) { // 双缓冲
// System.out.println("11");
// if (offScreenImage == null) {
// offScreenImage = this.createImage(800, 600);
// }
// Graphics gOffScreen = offScreenImage.getGraphics();
// Color c = gOffScreen.getColor();
// gOffScreen.setColor(Color.GREEN);
// gOffScreen.fillRect(0, 0, 800, 600);
// gOffScreen.setColor(c);
// paint(gOffScreen);
// g.drawImage(offScreenImage, 0, 0, null);
// }
double asin(int i) {
return Math.sin(i * Math.PI / 180);
}
double acos(int i) {
return Math.cos(i * Math.PI / 180);
}
double atan(int i) {
return Math.tan(i * Math.PI / 180);
}
void drawWatch(Graphics2D g2, int angle) {
int x0, y0;
Font font;
font = new Font("宋体", Font.BOLD, 10);
// 钞钟刻度
x0 = (int) (width + r * asin(angle));
y0 = (int) (heigh + r - r * acos(angle));
g2.setColor(Color.blue);
g2.setFont(font);
// 全钟表 60个刻度的*
g2.drawString("*", x0, y0);
}
void drawNumber(Graphics2D g2, int angle, int num) {
int x0, y0;
Font font;
font = new Font("宋体", Font.BOLD, 10);
// 画数v
// 数字
x0 = (int) (width + nr * asin(angle));
y0 = (int) (nheigh + nr - nr * acos(angle));
font = new Font("宋体", Font.BOLD, 20);
g2.setColor(Color.black);
g2.setFont(font);
g2.drawString(Integer.toString(num), x0, y0);
}
// 秒针
void drawSecrod(Graphics2D g2, int angle) {
int x1, y1;
int slagwidht = width;
int slagheigh = heigh + 20;
BasicStroke stroke;
// 画布的宽度是3
stroke = new BasicStroke(3);
if (angle == (secord * 6)) {
g2.setColor(Color.black);
g2.setStroke(stroke);
x1 = (int) (width + sr * asin(angle));
y1 = (int) (sheigh + sr - sr * acos(angle));
slagwidht = (int) (width - 20 * asin(angle));
slagheigh = (int) (heigh + r + 20 * acos(angle));
g2.drawLine(slagwidht, slagheigh, x1, y1);
}
}
// 分钟
void drawMenuit(Graphics2D g2, int angle) {
int x1, y1;
int slagwidht = width;
int slagheigh = heigh + 20;
BasicStroke stroke;
// 画布的宽度是 6
stroke = new BasicStroke(6);
if (angle == (minute * 6)) {
g2.setColor(Color.black);
g2.setStroke(stroke);
// 自身的角度 + 秒针的偏移量
x1 = (int) (width + mr * asin(angle + (6 * secord * 6) / 360));
y1 = (int) (mheigh + mr - mr * acos(angle + (6 * secord * 6) / 360));
slagwidht = (int) (width - 20 * asin(angle + (6 * secord * 6) / 360));
slagheigh = (int) (mheigh + mr + 20 * acos(angle + (6 * secord * 6) / 360));
g2.drawLine(slagwidht, slagheigh, x1, y1);
}
}
// Hour
void drawHour(Graphics2D g2, int angle) {
int x1, y1;
int slagwidht = width;
int slagheigh = heigh + 20;
BasicStroke stroke;
// 画布的宽度是 10
stroke = new BasicStroke(10);
if (angle == (hour * 30)) {
g2.setColor(Color.red);
g2.setStroke(stroke);
// 自身的角度 + 分针的偏移量
x1 = (int) (width + hr * asin(angle + (30 * minute * 6) / 360));
y1 = (int) (hheigh + hr - hr * acos(angle + (30 * minute * 6) / 360));
slagwidht = (int) (width - 20 * asin(angle + (30 * minute * 6) / 360));
slagheigh = (int) (hheigh + hr + 20 * acos(angle + (30 * minute * 6) / 360));
g2.drawLine(slagwidht, slagheigh, x1, y1);
}
}
// 画日历
void drawDate(Graphics2D g2, int angle) {
//
float x0, y0, x1, y1;
x0 = width + 50;
y0 = heigh + r - 10;
x1 = 40;
y1 = 20;
BasicStroke stroke;
// 画布的宽度是 10
stroke = new BasicStroke(3);
Rectangle2D r = new Rectangle2D.Float(x0, y0, x1, y1);
g2.setStroke(stroke);
g2.setColor(Color.black);
g2.draw(r);
g2.drawString(Integer.toString(tdate), x0+8, y0+18);
}
public void paint(Graphics g) {
super.paint(g);
Map mapH = new HashMap();
// 抗锯齿
mapH.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// (抗锯齿总开关)
mapH.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
// 转成2D 效果
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHints(mapH);
// 数字刻度。12个数字,每30度一个数字
int count = 0;
// 时钟刻度 60个刻度,每6度一个刻度
for (int i = 6; i <= 360; i = i + 360 / 60) {
// 带刻度
drawWatch(g2, i);
// 画数字
if (i % 30 == 0) {
count = count + 1;
drawNumber(g2, i, count);
}
}
// 画秒针 (每个秒针的刻度是 6度)
drawSecrod(g2, secord * 6);
// 画分针 (每个分针的刻度是 6度)
drawMenuit(g2, minute * 6);
// 画时针 (每个时针的刻度是 30度)
drawHour(g2, hour * 30);
// 画日期
drawDate(g2, tdate);
}
}
class TimeFrame extends JFrame {
TimeFrame() {
super();
setTitle("这是一个钟表");
setVisible(true);
setSize(600, 600);
TimePanel timepanel = new TimePanel();
Container containpane = getContentPane();
containpane.setLayout(new BorderLayout());
containpane.add(timepanel, BorderLayout.CENTER);
}
}
public class TimeWatch {
public static void main(String[] args) {
new TimeFrame();
}
}
效果如图