JAVA 因简洁而美丽,因有效而动人 善待JAVA这颗种子的人,必将得到她的福荫

JAVA编写的时钟

上一篇 / 下一篇  2012-09-05 22:00:14 / 个人分类:java

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

TAG: GUI gui java Thread

 

评分:0

我来说两句

Open Toolbar