图形化界面钟表(一)

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.swing.JFrame;

public class ClockGUI extends JFrame implements Runnable {
	Thread clock;

	final int Xpoint = 180;
	final int Ypoint = 180;
	final int R = 90; // 表盘半径
	int xHour = 0, yHour = 0; // 时针顶点坐标
	int xMinute = 0, yMinute = 0;// 分针顶点坐标
	int xSecond = 0, ySecond = 0;// 秒针顶点坐标

	public ClockGUI() {
		setTitle("图形化钟表V1.0");
		setSize(360, 360);
		setBackground(Color.yellow);
		start();
		repaint();
		setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 关闭窗口时退出程序
	}

	public void start() {
		if (clock == null) {

			clock = new Thread(this);
			clock.start();
		}
	}

	public void run() {
		while (clock != null) {

			repaint();
			try {
				Thread.sleep(1000);
			} catch (InterruptedException ex) {
				ex.printStackTrace();
			}
		}
	}

	public void stop() {
		clock = null;
	}

	public void paint(Graphics g) {
		g.setColor(Color.yellow);
		g.fillRect(0, 0, 360, 360);

		Calendar time = new GregorianCalendar();
		time.setTime(new Date());
		int hour = time.get(Calendar.HOUR_OF_DAY);
		int minute = time.get(Calendar.MINUTE);
		int second = time.get(Calendar.SECOND);

		DecimalFormat twoDigits = new DecimalFormat("00");
		String timeOutput = "北京时间:" + twoDigits.format(hour) + ":"
				+ twoDigits.format(minute) + ":" + twoDigits.format(second);

		g.setFont(new Font("宋体", Font.BOLD, 20));
		g.setColor(Color.red);
		g.drawString(timeOutput, 100, 340);

		g.setColor(Color.black);
		DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);
		g.drawString(dateFormat.format(time.getTime()), 20, 60);

		g.setFont(new Font("SAN_SERIF", Font.BOLD, 20));
		for (int i = 0, num = 1; i < 360; i += 6) {
			double radian = Math.toRadians(i);
			int xPos = Xpoint + (int) (R * Math.sin(radian));// 注意此句+
			int yPos = Ypoint - (int) (R * Math.cos(radian));// 注意此句-
			if (i == 0) {
				g.setColor(Color.red);
				g.drawString("" + 12, xPos - 6, yPos + 2);
			}
			if (i > 1) {
				if (i % 5 == 0) {
					if (i % 90 == 0) {
						g.setColor(Color.red);
						g.drawString("" + num, xPos - 4, yPos + 2);
					} else {
						g.setColor(Color.black);
						g.drawString("" + num, xPos - 4, yPos + 2);
					}
					num += 1;
					continue;
				}
			}
			g.setColor(Color.black);
			g.drawString(".", xPos, yPos);
		}

		// 画圆
		g.setColor(Color.black);
		g.drawOval(Xpoint - 88, Ypoint - 92, 180, 180);
		// 画圆心
		g.setFont(new Font("SAN_SERIF", Font.BOLD, 40));
		g.drawString(".", Xpoint - 4, Ypoint + 4);

		// 画秒针
		xSecond = (int) (Xpoint + (R - 10)
				* Math.sin(second * (2 * Math.PI / 60)));
		ySecond = (int) (Ypoint - (R - 10)
				* Math.cos(second * (2 * Math.PI / 60)));
		g.setColor(Color.red);
		g.drawLine(Xpoint, Ypoint, xSecond, ySecond);
		// 画分针
		xMinute = (int) (Xpoint + (R - 20)
				* Math.sin((minute + second / 60) * (2 * Math.PI / 60)));
		yMinute = (int) (Ypoint - (R - 20)
				* Math.cos((minute + second / 60) * (2 * Math.PI / 60)));
		g.setColor(Color.black);
		g.drawLine(Xpoint, Ypoint, xMinute, yMinute);
		// 画时针
		xHour = (int) (Xpoint + (R - 30)
				* Math.sin((hour + minute / 60 + second / 60 / 60)
						* (2 * Math.PI / 12)));
		yHour = (int) (Ypoint - (R - 30)
				* Math.cos((hour + minute / 60 + second / 60 / 60)
						* (2 * Math.PI / 12)));
		g.setColor(Color.black);
		g.drawLine(Xpoint, Ypoint, xHour, yHour);

	}

	public static void main(String args[]) {
		ClockGUI clock = new ClockGUI();
	}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值