java awt 模拟钟表

import java.awt.BasicStroke;
import java.awt.geom.Ellipse2D;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class run extends javax.swing.JFrame {

	// 定义一Graphics变量,用来画分针、秒针、时针等
	private java.awt.Graphics g;

	// 定义一个图片变量来存背景图片
	private java.awt.Image backGround;

	// 定义一个X,Y,分别表示钟表中心的坐标
	// R表示秒针的半径
	private int X = 300, Y = 300, R = 160;
	// 分别定义分针、秒针、时针的X、Y坐标
	private int X_HOUR, X_MINUTE, X_SECOND;
	private int Y_HOUR, Y_MINUTE, Y_SECOND;

	public void loadBackground() {
		this.setTitle("小钟表");
		this.setSize(600, 600);
		// 改其属性为不可变大小
		this.setResizable(false);
		// 设置其为关闭后退出
		this.setDefaultCloseOperation(3);
		// 设置其为可见
		this.setVisible(true);
		// 定义画布对象
		g = this.getGraphics();
		this.setBackground(java.awt.Color.white);
	}

	public void miaoMove(java.util.Date d) {
		// 将g包装成一个graphics2D
		java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
		// 将其颜色设置成为红
		g2.setColor(java.awt.Color.RED);
		// 计算 X_SECOND, Y_SECOND
		X_SECOND = (int) (X + R
				* Math.cos(d.getSeconds() * 2 * Math.PI / 60 - Math.PI / 2));
		Y_SECOND = (int) (Y + R
				* Math.sin(d.getSeconds() * 2 * Math.PI / 60 - Math.PI / 2));
		g2.setStroke(new BasicStroke(1));
		g2.drawLine(X, Y, X_SECOND, Y_SECOND);

	}

	public void fenMove(java.util.Date d) {
		java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
		g2.setColor(java.awt.Color.BLUE);
		X_MINUTE = (int) (X + (R * 0.8)
				* Math.cos(d.getMinutes() * 2 * Math.PI / 60 - Math.PI / 2));
		Y_MINUTE = (int) (Y + (R * 0.8)
				* Math.sin(d.getMinutes() * 2 * Math.PI / 60 - Math.PI / 2));
		g2.setStroke(new BasicStroke(5));
		g2.drawLine(X, Y, X_MINUTE, Y_MINUTE);

	}
	public void shiMove(java.util.Date d) {
		java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
		g2.setColor(java.awt.Color.BLACK);
		X_HOUR = (int) (X + (R * 0.5)
				* Math.cos(d.getHours() * 2 * Math.PI / 12 + d.getMinutes() * 2
						* Math.PI / (60 * 12) - Math.PI / 2));
		Y_HOUR = (int) (Y + (R * 0.5)
				* Math.sin(d.getHours() * 2 * Math.PI / 12 + d.getMinutes() * 2
						* Math.PI / (60 * 12) - Math.PI / 2));
		g2.setStroke(new BasicStroke(5));
		g2.drawLine(X, Y, X_HOUR, Y_HOUR);
	}

	public void miaoMove2(java.util.Date d) {
		java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
		g2.setColor(java.awt.Color.WHITE);
		X_SECOND = (int) (X + R
				* Math.cos(d.getSeconds() * 2 * Math.PI / 60 - Math.PI / 2));
		Y_SECOND = (int) (Y + R
				* Math.sin(d.getSeconds() * 2 * Math.PI / 60 - Math.PI / 2));
		g2.setStroke(new BasicStroke(1));
		g2.drawLine(X, Y, X_SECOND, Y_SECOND);

	}

	public void fenMove2(java.util.Date d) {
		java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
		g2.setColor(java.awt.Color.WHITE);
		X_MINUTE = (int) (X + (R * 0.8)
				* Math.cos(d.getMinutes() * 2 * Math.PI / 60 - Math.PI / 2));
		Y_MINUTE = (int) (Y + (R * 0.8)
				* Math.sin(d.getMinutes() * 2 * Math.PI / 60 - Math.PI / 2));
		g2.setStroke(new BasicStroke(5));
		g2.drawLine(X, Y, X_MINUTE, Y_MINUTE);

	}

	public void shiMove2(java.util.Date d) {
		java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
		g2.setColor(java.awt.Color.WHITE);
		X_HOUR = (int) (X + (R * 0.5)
				* Math.cos(d.getHours() * 2 * Math.PI / 12 + d.getMinutes() * 2
						* Math.PI / (60 * 12) - Math.PI / 2));
		Y_HOUR = (int) (Y + (R * 0.5)
				* Math.sin(d.getHours() * 2 * Math.PI / 12 + d.getMinutes() * 2
						* Math.PI / (60 * 12) - Math.PI / 2));
		g2.setStroke(new BasicStroke(5));
		g2.drawLine(X, Y, X_HOUR, Y_HOUR);
	}
	public void paint(java.awt.Graphics g) {
		super.paint(g);
		g.drawImage(backGround, 0, 0, 600, 600, null);
	}
	public void showData(java.util.Date d) {
		SimpleDateFormat f = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss",
				Locale.CHINA);
		String result = f.format(d);
		java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
		g2.setColor(java.awt.Color.darkGray);
		g2.drawString(result, 220, 580);
	}

	public void showData2(java.util.Date d) {
		SimpleDateFormat f = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss",
				Locale.CHINA);
		String result = f.format(d);
		java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
		g2.setColor(java.awt.Color.white);
		g2.drawString(result, 220, 580);
	}

	public void drawFram() {
		java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
		g2.setColor(java.awt.Color.BLACK);
		g2.setStroke(new BasicStroke(2));
		Ellipse2D circle = new Ellipse2D.Double();
		circle.setFrameFromCenter(X, Y, X + R, Y + R);// 设置椭圆中点及a,b
		g2.draw(circle);
		int r;
		for (int i = 0; i < 61; i += 5) {
			if ((i % 10) == 5) {
				r = 4;
				g2.setColor(java.awt.Color.BLUE);
			} else {
				r = 6;
				g2.setColor(java.awt.Color.RED);
			}
			int X_PIONT = (int) (X + R
					* Math.cos(i * 2 * Math.PI / 60 - Math.PI / 2));
			int Y_PIONT = (int) (Y + R
					* Math.sin(i * 2 * Math.PI / 60 - Math.PI / 2));
			Ellipse2D circle_point = new Ellipse2D.Double();
			circle_point.setFrameFromCenter(X_PIONT, Y_PIONT, X_PIONT + r,
					Y_PIONT + r);
			g2.draw(circle_point);
			g2.fill(circle_point);
		}
	}

	public static void main(String[] args) {
		java.util.Date d = new java.util.Date();
		run biaoRun = new run();
		biaoRun.loadBackground();
		while (true) {
			biaoRun.showData(d);
			biaoRun.shiMove(d);
			biaoRun.fenMove(d);
			biaoRun.miaoMove(d);

			try {
				Thread.sleep(1000);
			} catch (Exception ef) {
				ef.printStackTrace();
			}
			biaoRun.shiMove2(d);
			biaoRun.fenMove2(d);
			biaoRun.miaoMove2(d);
			biaoRun.showData2(d);
			biaoRun.drawFram();
			d = new java.util.Date();
		}

	}
}

 注部分参考别人代码,修改完成。

你可能感兴趣的:(java,thread,swing,F#)