java时钟源代码_Java 防真钟表程序源码

本文展示了一个使用Java编写的模拟时钟程序源码,包括秒针、分针、时针的绘制,以及日期显示。程序通过定时任务更新时间,并采用Graphics2D进行抗锯齿处理,提供更清晰的视觉效果。
摘要由CSDN通过智能技术生成

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();

}

}

效果如图

e2df7c2f6587e36ca8448da883fdba10.png

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值