JAVA时钟代码

6年前,课程设计,用C语言写了个小钟。6年后,一时兴起,用Java写一个小钟。

6年前,用了一个星期;6年后,用了半个小时。

6年前,书生意气,挥斥方遒;6年后,人未老心已老,未语泪先流。

不得不说,时间是把杀猪刀。

package com.JavaClock.UI;
import java.awt.*;
import java.awt.event.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.*;
import java.util.Timer;
import java.util.TimerTask;
import java.util.*;
public class MainFrame extends JFrame implements Runnable{

	private int width = 500;
	private int height = 500;
	private int hour = 1;
	private int minute = 1;
	private int second = 1;
	//半径
	private double R = (width-100)/2.0;
	private double P =  Math.PI/6;
	private boolean tag = true;
	private Map<Integer,Double> xMap = new HashMap<Integer,Double>();
	private Map<Integer,Double> yMap = new HashMap<Integer,Double>();
	
	public MainFrame() {
		
		this.setTitle("我的小钟");
		this.setSize(new Dimension(width,height));
		this.setLocation(200,200);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.setVisible(true);
        for(int i=1;i<=12;i++) {
			
			double e = P*i;
			double x = Math.sin(e)*R + R + 50;
			double y = R + 50 - Math.cos(e)*R ;
			
			xMap.put(i, x);
			yMap.put(i, y);
		}
		
		new Thread(this).start();
	}
	
	public int calX(double P,int i,int R1){
		
		double e = P*i;
		double x = Math.sin(e)*R1 + R + 50;
		return (int)x;
	}
	
	public int calY(double P,int i,int R1){
		
		double e = P*i;
		double y = R + 50 - Math.cos(e)*R1 ;
		return (int)y;
	}
	public void paint(Graphics g){
		
		Graphics2D g2 = (Graphics2D) g;
	
		//钟的背景和钟盘只绘制一次
		g2.setColor(Color.BLACK);
		g2.fillRect(0,0,width,height);
		
		g2.setColor(Color.BLUE);
		g2.drawOval(35,35,width-70,height-70);
		
		double x = 0, y = 0;
		for(int i=1;i<=12;i++) {
			
			double e = P*i;
			x = xMap.get(i);
			y = yMap.get(i) ;
			
			g2.drawString(String.valueOf(i),(int)x,(int) y);
		}
		tag = false;	
		
		//绘制钟心
		g2.fillRect(30+(width-70)/2,30+(height-70)/2,10,10);
		
		//绘制数字时钟
		g2.drawString(hour+":"+minute+":"+second,20 + (width-75)/2,100);
		//绘制时针
		g2.setColor(Color.RED);
		if(hour>12){
			
			hour -= 12;
		}
		g2.drawLine(35+(width-70)/2,35+(width-70)/2,calX(Math.PI/6+Math.PI/120,hour,60), calY(Math.PI/6+Math.PI/120,hour,60));
		//绘制分针
		g2.setColor(Color.YELLOW);
		g2.drawLine(35+(width-70)/2,35+(width-70)/2,calX(Math.PI/30,minute,100), calY(Math.PI/30,minute,100));
		//绘制秒针
		g2.setColor(Color.GREEN);
		g2.drawLine(35+(width-70)/2,35+(width-70)/2,calX(Math.PI/30,second,150), calY(Math.PI/30,second,150));
		
		g2.dispose();
	}
	
	public void run() {
		
		Timer timer = new Timer();
		timer.schedule(new TimerTask(){
			
			public void run() {
				
				hour = new java.util.Date().getHours();
				minute = new java.util.Date().getMinutes();
				second = new java.util.Date().getSeconds();
				
				repaint();
			}
		},0,1000);
	}
	
	public static void main(String[] args){
		
		MainFrame mainf = new MainFrame();
	}
}

代码写得粗糙了点,仅为祭奠我的青春,我的18岁。

  • 10
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值