Java实现时钟

一、核心的表达式

因为需要动态显示小时的指针、分钟的指针、秒的指针的位置,所以确认三个指针的角度非常重要; 

X:三个指针相交的原点的X坐标;

Y:三个指针相交的原点的Y坐标;

HOUR_LENGTH、MINUTE_LENGTH、SECOND_LENGTH表示时针、分针、秒针的长度;

hour、minute、second表示现在是几时、几分、几秒;  

hourLine.x2 = X+HOUR_LENGTH*Math.cos(hour*(Math.PI/6)-Math.PI/2);
hourLine.y2 = Y+HOUR_LENGTH*Math.sin(hour*(Math.PI/6)-Math.PI/2);
minLine.x2 = X+MINUTE_LENGTH*Math.cos(minute*(Math.PI/30)-Math.PI/2);
minLine.y2 = Y+MINUTE_LENGTH*Math.sin(minute*(Math.PI/30)-Math.PI/2);
secondLine.x2 = X+SECOND_LENGTH*Math.cos(second*(Math.PI/30)-Math.PI/2);
secondLine.y2 = Y+SECOND_LENGTH*Math.sin(second*(Math.PI/30)-Math.PI/2);

二、怎样动态显示

简单的说就是每一秒更新一次屏幕,因此用Timer和TimerTask非常符合要求;每一秒刷新一次;

三、代码

package org.Demo00;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test5 extends JFrame {
	MyPanel clockPanel;
	Ellipse2D.Double e;
	int x;
	int y;
	Line2D.Double hourLine;
	Line2D.Double minLine;
	Line2D.Double secondLine;
	GregorianCalendar calendar;
	int hour;
	int minute;
	int second;
	public static final int X = 60;
	public static final int Y = 60;
	public static final int X_BEGIN = 10;
	public static final int Y_BEGIN = 10;
	public static final int RADIAN = 50;

	public Test5() {
		setSize(300, 400);
		clockPanel = new MyPanel();
		add(clockPanel);
		Timer t = new Timer();
		Task task = new Task();
		t.schedule(task, 0, 1000);
	}

	public static void main(String[] args) {
		Test5 t = new Test5();
		t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		t.setVisible(true);

	}

	class MyPanel extends JPanel {
		public MyPanel() {
			e = new Ellipse2D.Double(X_BEGIN, Y_BEGIN, 100, 100);
			hourLine = new Line2D.Double(X, Y, X, Y);
			minLine = new Line2D.Double(X, Y, X, Y);
			secondLine = new Line2D.Double(X, Y, X, Y);
		}

		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			Graphics2D g2 = (Graphics2D) g;
			g2.drawString("12", 55, 25);
			g2.drawString("6", 55, 105);
			g2.drawString("9", 15, 65);
			g2.drawString("3", 100, 65);
			g2.draw(e);
			g2.draw(hourLine);
			g2.draw(minLine);
			g2.draw(secondLine);
		}
	}

	class Task extends TimerTask {
		public void run() {
			calendar = new GregorianCalendar();
			hour = calendar.get(Calendar.HOUR);
			minute = calendar.get(Calendar.MINUTE);
			second = calendar.get(Calendar.SECOND);
			hourLine.x2 = X + 40 * Math.cos(hour * (Math.PI / 6) - Math.PI / 2);
			hourLine.y2 = Y + 40 * Math.sin(hour * (Math.PI / 6) - Math.PI / 2);
			minLine.x2 = X + 45
					* Math.cos(minute * (Math.PI / 30) - Math.PI / 2);
			minLine.y2 = Y + 45
					* Math.sin(minute * (Math.PI / 30) - Math.PI / 2);
			secondLine.x2 = X + 50
					* Math.cos(second * (Math.PI / 30) - Math.PI / 2);
			secondLine.y2 = Y + 50
					* Math.sin(second * (Math.PI / 30) - Math.PI / 2);
			repaint();
		}
	}

}


 

 

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 实现时钟效果可以使用 Java Swing 组件库中的 `JLabel` 和 `Timer` 组件。具体实现步骤如下: 1. 创建一个 `JFrame` 窗口,并添加一个 `JLabel` 组件用于显示当前时间。 2. 创建一个 `Timer` 组件,每隔一钟刷新一次时间。 3. 在 `Timer` 组件的 `ActionListener` 中更新 `JLabel` 组件的显示内容,即当前时间。 4. 使用 Java 的日期时间类 `java.time.LocalDateTime` 获取系统当前时间。 下面是一个简单的 Java 时钟实现示例: ```java import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.Timer; public class Clock extends JFrame implements ActionListener { private JLabel timeLabel; private Timer timer; public Clock() { // 创建窗口 setTitle("Clock"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); setSize(200, 100); // 创建时间标签 timeLabel = new JLabel(); timeLabel.setFont(new Font("Dialog", Font.PLAIN, 24)); add(timeLabel); // 创建定时器 timer = new Timer(1000, this); timer.start(); } @Override public void actionPerformed(ActionEvent e) { // 更新时间标签 LocalDateTime now = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); timeLabel.setText(formatter.format(now)); } public static void main(String[] args) { Clock clock = new Clock(); clock.setVisible(true); } } ``` 这个时钟会在窗口中显示当前时间,并且每隔一钟自动更新
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值