java编写小时钟_JAVA编写的时钟 - 笨瓜瓜的个人空间 - 51Testing软件测试网 51Testing软件测试网-软件测试人的精神家园...

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Toolkit;

import java.util.Date;

import javax.swing.JFrame;

public class Clock extends JFrame. implements Runnable

{

public Clock()

{

super("时钟");

init();

}

public void run()

{

while (true)

{

repaint();

try

{

Thread.sleep(1000);

}

catch (Exception e)

{

}

}

}

private void init()

{

this.setSize(300, 300);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setResizable(false);

this.setVisible(true);

Dimension pm = Toolkit.getDefaultToolkit().getScreenSize();

Dimension fr = this.getSize();

this.setLocation((pm.width - fr.width) / 2,

(pm.height - fr.height) / 2);

}

public void initDraw(Graphics g)

{

// 绘制边缘蓝色圆圈

g.setColor(Color.blue);

g.drawOval(49, 49, 201, 201);

g.drawOval(48, 48, 203, 203);

// 绘制黄色圆盘

g.setColor(Color.yellow);

g.fillOval(50, 50, 200, 200);

// 绘制中间红色圆点

g.setColor(Color.red);

g.fillOval(146, 146, 8, 8);

// 绘制周围4个数字

g.setColor(Color.blue);

g.drawString("12", 144, 70);

g.drawString("3", 235, 154);

g.drawString("6", 147, 240);

g.drawString("9", 60, 154);

// 循环绘制周围60个短线刻度

for (int i = 0; i < 60; i++)

{

g.setColor(Color.black);

double hu = Math.PI * i / 30;

Tool to1 = new Tool(hu, 100);

Tool to2 = new Tool(hu, 90);

if (i % 5 == 0)

{

g.setColor(Color.red);

}

g.drawLine(to1.getX() + 150, to1.getY() + 150, to2.getX() + 150,

to2.getY() + 150);

}

}

public void paint(Graphics g)

{

super.paint(g);

initDraw(getGraphics());

drawLine(g);

}

private void drawLine(Graphics g)

{

Date now = new Date();

int h = now.getHours() % 12;

int m = now.getMinutes();

int s = now.getSeconds();

Font font = new Font("宋体", 1, 15);

g.setFont(font);

g.drawString(now.toLocaleString(), 72, 280);

int x, y, len;

Tool tool;

double hu;

// 画秒针

len = 80;

hu = Math.PI * s / 30;

tool = new Tool(hu, len);

x = tool.getX() + 150;

y = 150 - tool.getY();

g.setColor(Color.cyan);

g.drawLine(150, 150, x, y);

// 秒针末端的灰色小点

g.setColor(Color.gray);

g.fillOval(x - 1, y - 1, 4, 4);

// 画分针

len = 60;

hu = Math.PI * (m + s / 60.0) / 30;

tool = new Tool(hu, len);

x = tool.getX() + 150;

y = 150 - tool.getY();

g.setColor(Color.DARK_GRAY);

g.drawLine(150, 150, x, y);

// 画时针

len = 40;

hu = Math.PI * (h + m / 60.0) / 6;

tool = new Tool(hu, len);

x = tool.getX() + 150;

y = 150 - tool.getY();

g.setColor(Color.BLUE);

g.drawLine(150, 150, x, y);

g.dispose();

}

public static void main(String [] args)

{

new Thread(new Clock()).start();

}

}

class Tool

{

private int x, y;

// 根据传入的弧度和指针的长度两个参数,计算指针末端相对于圆点的x坐标和y坐标

public Tool(double hu, int len)

{

x = (int) (Math.sin(hu) * len);

y = (int) (Math.cos(hu) * len);

}

public int getX()

{

return x;

}

public int getY()

{

return y;

}

}

转自:http://lupingzi.iteye.com/blog/508908

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值