Java__applet__Clock

这是一个用Java写的时钟
运行效果如下图:
Java__applet__Clock_第1张图片

代码如下:

import java.awt.*;
import java.uti1655648?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzU1NTA0NDM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)![这里写图片描述](https://img-blog.csdn.net/2016120721l.*;
import javax.swing.*;
import java.text.SimpleDateFormat;

/**
 * @author 行者無疆
 *
 */
public class Clock extends JApplet implements Runnable {
	private Thread timer = null;
	private int lastXh, lastYh, lastXm, lastYm, lastXs, lastYs;
	private SimpleDateFormat sdf;
	private NewPanel panel = new NewPanel();

	public void init() {
		lastXh = lastYh = lastXm = lastYm = lastXs = lastYs = 0;
		setBackground(Color.WHITE);
		add(panel);
		sdf = new SimpleDateFormat("yyyy 年 MM 月 dd 日 a hh 时 mm 分 ss 秒 EEEEE");

	}

	private class NewPanel extends JPanel {
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			int xh, yh, xm, ym, xs, ys, s, m, h, xCenter, yCenter;
			Calendar cal = Calendar.getInstance();
			Date sdate = new Date();
			String today = sdf.format(sdate);
			cal.setTime(sdate);
			s = cal.get(Calendar.SECOND);
			m = cal.get(Calendar.MINUTE);
			h = cal.get(Calendar.HOUR);
			xCenter = getWidth() / 2;
			yCenter = 80;
			xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 45 + xCenter);
			ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 45 + yCenter);
			xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + xCenter);
			ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + yCenter);
			xh = (int) (Math.cos((h * 30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30 + xCenter);
			yh = (int) (Math.sin((h * 30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30 + yCenter);

			g.setFont(new Font("TimesRoman", Font.PLAIN, 14));
			g.setColor(Color.blue);
			g.drawOval(xCenter - 52, yCenter - 52, 104, 104);
			g.setColor(Color.DARK_GRAY);
			g.drawString("9", xCenter - 45, yCenter +5);
			g.drawString("3", xCenter + 40, yCenter + 3);
			g.drawString("12", xCenter - 7, yCenter - 37);
			g.drawString("6", xCenter - 4, yCenter + 45);
			g.setColor(getBackground());
			if (xs != lastXs || ys != lastYs) {
				g.fillOval(lastXs - 5, lastYs - 5, 10, 10);
				g.drawLine(xCenter, yCenter, lastXs, lastYs);
			}
			if (xm != lastXm || ym != lastYm) {
				g.drawLine(xCenter, yCenter - 1, lastXm, lastYm);
				g.drawLine(xCenter - 1, yCenter, lastXm, lastYm);
			}
			if (xh != lastXh || yh != lastYh) {
				g.drawLine(xCenter, yCenter - 1, lastXh, lastYh);
				g.drawLine(xCenter - 1, yCenter, lastXh, lastYh);
			}
			g.setColor(Color.DARK_GRAY);
			g.drawString(today, 30, 180);
			g.setColor(Color.red);
			g.fillOval(xs - 5, ys - 5, 10, 10);
			g.drawLine(xCenter, yCenter - 1, xm, ym);
			g.drawLine(xCenter - 1, yCenter, xm, ym);
			g.drawLine(xCenter, yCenter - 1, xh, yh);
			g.drawLine(xCenter - 1, yCenter, xh, yh);
			lastXs = xs;
			lastYs = ys;
			lastXm = xm;
			lastYm = ym;
			lastXh = xh;
			lastYh = yh;

		}
	}

	public void start()
	{
		if(timer ==null)
		{
			timer  =new Thread(this);
			timer.start();
		}
	}

	public void stop() {
		if (timer != null) {
			timer.interrupt();
			timer = null;
		}
	}

	public void run() {
		while (timer != null) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}
			repaint();
		}
	}

}

HTML如下:



 



#欢迎留言交流

你可能感兴趣的:(java练习)