package com.a.a;

import java.awt.*;
import javax.swing.*;
import java.util.*;

public class StillClock extends JPanel
{
	private int hour;
	private int minute;
	private int second;

	public StillClock()
	{
		setCurrentTime();
	}

	/** Construct a clock with specified hour, minute, and second */
	public StillClock(int hour, int minute, int second)
	{
		this.hour = hour;
		this.minute = minute;
		this.second = second;
	}

	/** Return hour */
	public int getHour()
	{
		return hour;
	}

	/** Set a new hour */
	public void setHour(int hour)
	{
		this.hour = hour;
		repaint();
	}

	/** Return minute */
	public int getMinute()
	{
		return minute;
	}

	/** Set a new minute */
	public void setMinute(int minute)
	{
		this.minute = minute;
		repaint();
	}

	/** Return second */
	public int getSecond()
	{
		return second;
	}

	/** Set a new second */
	public void setSecond(int second)
	{
		this.second = second;
		repaint();
	}

	/** Draw the clock */
	protected void paintComponent(Graphics g)
	{
		super.paintComponent(g);

		// Initialize clock parameters
		int clockRadius = (int) (Math.min(getWidth(), getHeight()) * 0.8 * 0.5);
		int xCenter = getWidth() / 2;
		int yCenter = getHeight() / 2;

		// Draw circle
		g.setColor(Color.black);
		g.drawOval(xCenter - clockRadius, yCenter - clockRadius,
				2 * clockRadius, 2 * clockRadius);
		g.drawString("12", xCenter - 5, yCenter - clockRadius + 12);
		g.drawString("9", xCenter - clockRadius + 3, yCenter + 5);
		g.drawString("3", xCenter + clockRadius - 10, yCenter + 3);
		g.drawString("6", xCenter - 3, yCenter + clockRadius - 3);

		// Draw second hand
		int sLength = (int) (clockRadius * 0.8);
		int xSecond = (int) (xCenter + sLength
				* Math.sin(second * (2 * Math.PI / 60)));
		int ySecond = (int) (yCenter - sLength
				* Math.cos(second * (2 * Math.PI / 60)));
		g.setColor(Color.red);
		g.drawLine(xCenter, yCenter, xSecond, ySecond);

		// Draw minute hand
		int mLength = (int) (clockRadius * 0.65);
		int xMinute = (int) (xCenter + mLength
				* Math.sin(minute * (2 * Math.PI / 60)));
		int yMinute = (int) (yCenter - mLength
				* Math.cos(minute * (2 * Math.PI / 60)));
		g.setColor(Color.blue);
		g.drawLine(xCenter, yCenter, xMinute, yMinute);

		// Draw hour hand
		int hLength = (int) (clockRadius * 0.5);
		int xHour = (int) (xCenter + hLength
				* Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));
		int yHour = (int) (yCenter - hLength
				* Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));
		g.setColor(Color.BLACK);
		g.drawLine(xCenter, yCenter, xHour, yHour);
	}

	public void setCurrentTime()
	{
		// Construct a calendar for the current date and time
		Calendar calendar = new GregorianCalendar();

		// Set current hour, minute and second
		this.hour = calendar.get(Calendar.HOUR_OF_DAY);
		this.minute = calendar.get(Calendar.MINUTE);
		this.second = calendar.get(Calendar.SECOND);
	}

	public Dimension getPreferredSize()
	{
		return new Dimension(200, 200);
	}
}
最近下载更多
Kunyuu  LV1 2023年12月26日
另类清晨  LV8 2023年5月15日
微信网友_5992582549164032  LV6 2022年6月21日
段朝洪  LV15 2020年11月21日
liangge2115  LV27 2020年10月17日
刘晨娜  LV1 2020年7月24日
15966848095  LV22 2020年1月12日
wenxiaozhong  LV1 2019年12月18日
如风太过分人  LV1 2019年12月17日
yongzheng132  LV17 2019年6月21日
最近浏览更多
Kunyuu  LV1 2023年12月26日
wuyongjian 2023年6月29日
暂无贡献等级
luo110012  LV9 2023年5月15日
另类清晨  LV8 2023年5月15日
想把它染成绿色 2023年3月1日
暂无贡献等级
Willson88888  LV3 2022年12月7日
微信网友_6248713511227392  LV11 2022年12月5日
一起吹过晚风的街  LV1 2022年9月21日
微信网友_5992582549164032  LV6 2022年6月21日
猪皮怪 2022年6月12日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友