欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java实现时钟

程序员文章站 2022-03-09 09:04:36
...

在理解吸收了http://blog.csdn.net/fengsigaoju/article/details/51327180这位朋友写的代码之后做了一定的美观性的修改。

增加了两个线程,分别用来操作电子风格表盘和日期显示。

package drawClock;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JPanel;
import javax.swing.text.Highlighter.Highlight;

public class Clock extends JPanel {
	protected Thread thread_dial = null; // 机械型时钟所用线程
	protected Thread thread_electric = null; // 电子型时钟所用线程
	protected Thread thread_date = null; // 日期显示所用线程
	protected static int x_origin = 250, y_origin = 250; // 坐标原点

	protected static int r_hour = 50; // 时针半径
	protected static int r_minute = 75; // 分针半径
	protected static int r_second = 100; // 秒针半径

	double x_hour, y_hour; // 时针所在位置
	double x_minute, y_minute; // 分针所在位置
	double x_second, y_second; // 秒针所在位置

	protected String hour = ""; // 小时读数
	protected String minute = ""; // 分钟读数
	protected String second = ""; // 秒读数
	protected String dot = ""; // 电子式时钟间隔闪烁点

	protected String year = ""; // 年读数
	protected String month = ""; // 月读数
	protected String dayInMonth = ""; // 月中日读数
	protected String dayInWeekCHN = ""; // 周中日汉语
	protected String dash = ""; // 日期中间的破折号

	Color colorOfDay = null;

	public Clock() {
		/* 机械型时钟线程操作 */
		thread_dial = new Thread(new Runnable() {

			@Override
			public void run() {
				while (true) {

					Date date = new Date();
					/* 获取当前时分秒,存入临时变量temp_XX */
					hour = String.format("%tI", date);
					int temp_hour = Integer.parseInt(hour);
					minute = String.format("%tM", date);
					int temp_minute = Integer.parseInt(minute);
					second = String.format("%tS", date);
					int temp_second = Integer.parseInt(second);

					double totalSeconds = 3600 * temp_hour + 60 * temp_minute + 1 * temp_second; // 获得当前时间一共多少秒
					double angle_hour = totalSeconds / 120; // 时针1秒走过1/120°
					angle_hour = Math.toRadians(angle_hour);
					totalSeconds = 60 * temp_minute + temp_second;
					double angle_minute = totalSeconds * 0.1; // 分针1秒走过0.1°
					angle_minute = Math.toRadians(angle_minute);
					double angle_second = temp_second * 6; // 秒针一秒走过6°
					angle_second = Math.toRadians(angle_second);

					/* 根据半径和角度计算指针所在位置 */
					x_hour = r_hour * Math.sin(angle_hour) + x_origin;
					y_hour = r_hour * Math.cos(angle_hour + Math.PI) + y_origin;
					x_minute = r_minute * Math.sin(angle_minute) + x_origin;
					y_minute = r_minute * Math.cos(angle_minute + Math.PI) + y_origin;
					x_second = r_second * Math.sin(angle_second) + x_origin;
					y_second = r_second * Math.cos(angle_second + Math.PI) + y_origin;

					repaint(); // 再次打印界面,相当于每隔1秒刷新一次界面

					try {
						Thread.sleep(1000); // 线程休眠1秒
					} catch (InterruptedException e) {
						e.printStackTrace();
					} // try-catch

				} // while

			} // run()

		});

		/* 电子型时钟线程操作 */
		thread_electric = new Thread(new Runnable() {

			@Override
			public void run() {
				while (true) {
					Date date = new Date();
					/* 获取当前时分秒 */
					hour = String.format("%tI", date);
					minute = String.format("%tM", date);
					second = String.format("%tS", date);

					if (Integer.parseInt(second) % 2 == 0) {
						dot = "";
					} else {
						dot = ":";
					}

					repaint(); // 再次打印界面,相当于每隔1秒刷新一次界面

					try {
						Thread.sleep(1000); // 线程休眠1秒
					} catch (InterruptedException e) {
						e.printStackTrace();
					} // try-catch

				} // while

			} // run()
		});

		/* 日期线程操作 */
		thread_date = new Thread(new Runnable() {

			@Override
			public void run() {
				while (true) {
					Date date = new Date();
					/* 获取当前年月日 */
					year = String.format("%tY", date);
					month = String.format("%tm", date);
					dayInMonth = String.format("%td", date);
					second = String.format("%tS", date);

					String temp = String.format("%ta", date).substring(2, 3);
					switch (temp) {
					case "一":
						dayInWeekCHN = "月";
						colorOfDay = new Color(255, 248, 220);
						break;

					case "二":
						dayInWeekCHN = "火";
						colorOfDay = new Color(255, 69, 0);
						break;

					case "三":
						dayInWeekCHN = "水";
						colorOfDay = new Color(72, 118, 255);
						break;

					case "四":
						dayInWeekCHN = "木";
						colorOfDay = new Color(205, 170, 125);
						break;

					case "五":
						dayInWeekCHN = "金";
						colorOfDay = new Color(255, 215, 0);
						break;

					case "六":
						dayInWeekCHN = "土";
						colorOfDay = new Color(205, 102, 29);
						break;

					case "日":
						dayInWeekCHN = "日";
						colorOfDay = new Color(238, 238, 0);
						break;
					}

					if (Integer.parseInt(second) % 2 == 0) {
						dash = "";
					} else {
						dash = "-";
					}

					repaint(); // 再次打印界面,相当于每隔1秒刷新一次界面

					try {
						Thread.sleep(1000); // 线程休眠1秒
					} catch (InterruptedException e) {
						e.printStackTrace();
					} // try-catch

				} // while

			} // run()
		});

		/* 启动线程 */
		thread_dial.start();
		thread_electric.start();
		thread_date.start();

	} // Clock()

	@Override
	public void paint(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 打开反锯齿开关

		int radius = 250; // 表盘半径

		/* 背景设定 */
		g2d.setColor(Color.BLACK); // 背景色设定
		g2d.fillRect(0, 0, 550, 550);

		/* 画电子表显示区 */
		g2d.setStroke(new BasicStroke(5.0f)); // 设定显示框粗细
		g2d.setColor(new Color(132, 112, 255)); // 设定显示框颜色
		g2d.drawRoundRect(95, 30, 300, 60, 30, 30);
		g2d.setColor(new Color(255, 250, 205)); // 设定显示数字颜色
		g2d.setFont(new Font("Highlight LET", Font.PLAIN, 50)); // 显示数字字体设定
		g2d.drawString(hour, 120, 75);
		g2d.drawString(minute, 220, 75);
		g2d.drawString(second, 320, 75);
		g2d.setColor(new Color(255, 250, 205)); // 设置间隔闪烁点的颜色
		g2d.drawString(dot, 200, 70);
		g2d.drawString(dot, 290, 70);

		/* 画日期显示区 */
		g2d.setStroke(new BasicStroke(5.0f)); // 设定显示框粗细
		g2d.setColor(new Color(145, 238, 145)); // 设定显示框颜色
		g2d.drawRoundRect(95, 415, 300, 60, 30, 30);
		g2d.setColor(new Color(255, 165, 79)); // 设定显示数字颜色
		g2d.setFont(new Font("Highlight LET", Font.PLAIN, 50)); // 显示数字字体设定;
		g2d.drawString(year, 120, 460);
		g2d.drawString(month, 255, 460);
		g2d.drawString(dayInMonth, 330, 460);
		g2d.setColor(new Color(255, 165, 79)); // 设置间隔闪烁线的颜色
		g2d.drawString(dash, 227, 460);
		g2d.drawString(dash, 300, 460);

		/* 画表盘刻度 */
		g2d.setColor(new Color(255, 250, 250)); // 设定刻度颜色
		g2d.setFont(new Font("Highlight LET", Font.PLAIN, 23)); // 刻度数字字体设定
		g2d.drawString("12", 245, 145);
		g2d.drawString("3", 358, 255);
		g2d.drawString("6", 250, 370);
		g2d.drawString("9", 130, 255);

		/* 画表面 */
		g2d.setStroke(new BasicStroke(5.0f)); // 设定表盘粗细
		g2d.setColor(new Color(135, 206, 250)); // 设定表盘颜色
		g2d.drawOval(125, 125, radius, radius);

		/* 画指针 */
		Stroke stroke_line = new BasicStroke(4.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); // 设定指针粗细
		g2d.setStroke(stroke_line);
		g2d.setColor(new Color(144, 238, 144)); // 设定时针颜色
		g2d.drawLine(x_origin, y_origin, (int) x_hour, (int) y_hour);
		g2d.setColor(new Color(255, 250, 205)); // 设定分针颜色
		g2d.drawLine(x_origin, y_origin, (int) x_minute, (int) y_minute);
		g2d.setColor(new Color(255, 99, 71)); // 设定秒针颜色
		g2d.drawLine(x_origin, y_origin, (int) x_second, (int) y_second);

		/* 画曜日左显示区 */
		g2d.setStroke(new BasicStroke(5.0f)); // 设定显示框粗细
		g2d.setColor(colorOfDay); // 设定显示框颜色
		g2d.drawRoundRect(20, 210, 80, 80, 30, 30);
		g2d.setColor(colorOfDay); // 设定显示字体颜色
		g2d.setFont(new Font("华文彩云", Font.BOLD, 50)); // 显示字体设定;
		g2d.drawString(dayInWeekCHN, 35, 265);

		/* 画曜日右显示区 */
		g2d.setStroke(new BasicStroke(5.0f)); // 设定显示框粗细
		g2d.setColor(colorOfDay); // 设定显示框颜色
		g2d.drawRoundRect(400, 210, 80, 80, 30, 30);
		g2d.setColor(colorOfDay); // 设定显示字体颜色
		g2d.setFont(new Font("华文彩云", Font.BOLD, 50)); // 显示字体设定;
		g2d.drawString(dayInWeekCHN, 415, 265);

		/* 画表心 */
		g2d.setColor(new Color(218, 112, 214)); // 设定内圈颜色
		g2d.fillOval(246, 245, 10, 10);
		g2d.setColor(new Color(135, 206, 250)); // 设定外圈颜色
		g2d.setStroke(new BasicStroke(3.0f)); // 设定外圈粗细
		g2d.drawOval(246, 245, 10, 10);

	} // paintClock

} // class Clock


package drawClock;

import javax.swing.JFrame;

public class Operate extends JFrame {

	public Operate() {
		setTitle("黑色风格时钟");
		setVisible(true);
		setLayout(null);
		setResizable(false);
		setBounds(200, 200, 510, 550);

		Clock clock = new Clock();
		clock.setBounds(0, 0, 510, 550);
		getContentPane().add(clock);
	} // Operate()

	public static void main(String[] args) {
		Operate run = new Operate();
		

	} // main()

}


最终运行效果如图所示:

Java实现时钟


相关标签: java 线程