时钟类

<pre name="code" class="java">package com.lovo;

import java.util.Calendar;

/**
 * 时钟类
 * @author 李睿
 *
 */
public class Clock {
	private int hour;
	private  int min;
	private int second;

	public Clock(int hour, int min, int second) {
		this.hour = hour;
		this.min = min;
		this.second = second;
	}
	
	public Clock(){
		Calendar cal = Calendar.getInstance();
		hour = cal.get(11);
		min = cal.get(12);
		second = cal.get(13);
	}
	/**
	 * 时钟运行
	 */
	public void run(){
			second ++;
			if (second == 60) {
				min += 1;
				second =0;
				if (min == 60) {
					hour += 1;
					min = 0;
				}
			}
			if (hour == 24 && min == 0 && second == 0){
				hour = 0;
				min = 0;
				second = 0;
			}
		}
	
	/**
	 * 显示时间
	 * @return 
	 */
	public String showTime(){
		String time = "";
		if (hour < 10){
			time += "0";
		}
		time += hour + ":";
		if (min < 10){
			time += "0";
		}
		time += min + ":";
		if (second < 10){
			time += "0";
		}
		time += second;
		return time;
		}
}



<pre name="code" class="java">package com.lovo;
/**
 * 时钟运行显示时间
 * @author 李睿
 *
 */
public class Text03 {
	public static void main(String[] args) throws Exception {
		Clock c1 = new Clock();
		for (; ;) {
			c1.run();
			System.out.println(c1.showTime());
			//停顿1s
			Thread.sleep(1000);
		}
	}
}

 


                
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java基础篇中的StillClock用于画出时钟以方便Java进阶篇P228第二十九章多线程(第八版)ClockWithAudio的调用。 package 多线程练习; import java.awt.*; import javax.swing.*; import java.util.*; public class StillClock extends JPanel { private int hour; private int minute; private int second; /** Construct a default clock with the current time */ 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); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值