时钟表盘代码

3 篇文章 0 订阅
3 篇文章 0 订阅
 

package timer.com.cn;

/*
 *时钟
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class TimeWindow extends JPanel implements Runnable {
 public static void main(String[] args) {
  TimeWindow xx=new TimeWindow();
 }
 Thread th = new Thread(this);
 int second;
 int minutes;
 int hour;
 int day;
 int mouth;
 int year;
 Date data;
 TimeWindow() {
  th.start();
  data = new Date();
  second = data.getSeconds();
  minutes = data.getMinutes();
  hour = data.getHours();
  day = data.getDay();
  mouth = data.getMonth();
  year = data.getYear();
  this.setBounds(0, 0, 300, 300);
 }
 public void run() {

  try {
   while (true) {
    data = new Date();
    // 获得当前日期和时间
    second = data.getSeconds();
    minutes = data.getMinutes();
    hour = data.getHours();
    day = data.getDay();
    mouth = data.getMonth();
    year = data.getYear();
    this.repaint();
    th.sleep(1000);
   }
  } catch (Exception e) {
  }
 }
 public void paintComponent(Graphics g) {
  super.paintComponent(g);
  this.setBackground(Color.gray);
  int x = this.getWidth() / 2;
  int y = this.getHeight() / 2;
  double z[] = {30, 15, 10, 7.5, 6, 5, 4.29, 3.75, 3.33, 3, 2.72, 2.50,
    2.30, 2.14};
  // 画表盘
  g.drawString("1 ", (int) (x - 5 + Math.sin(Math.PI / z[4]) * 110),
    (int) (y + 20 - Math.cos(Math.PI / z[4]) * 110));
  g.drawString("2 ", (int) (x - 5 + Math.sin(Math.PI / z[8]) * 110),
    (int) (y + 20 - Math.cos(Math.PI / z[8]) * 110));
  g.drawString("3 ", x + 90, y + 5);
  g.drawString("4 ", (int) (x - 10 + Math.cos(Math.PI / z[4]) * 110),
    (int) (y + 5 + Math.sin(Math.PI / z[4]) * 110));
  g.drawString("5 ", (int) (x - 10 + Math.cos(Math.PI / z[8]) * 110),
    (int) (y + Math.sin(Math.PI / z[8]) * 110));
  g.drawString("6 ", x - 5, y + 100);
  g.drawString("7 ", (int) (x - Math.sin(Math.PI / z[4]) * 110),
    (int) (y - 5 + Math.cos(Math.PI / z[4]) * 110));
  g.drawString("8 ", (int) (x - 5 - Math.sin(Math.PI / z[8]) * 110),
    (int) (y - 10 + Math.cos(Math.PI / z[8]) * 110));
  g.drawString("9 ", x - 100, y + 5);
  g.drawString("10 ", (int) (x + 5 - Math.cos(Math.PI / z[4]) * 110),
    (int) (y + 10 - Math.sin(Math.PI / z[4]) * 110));
  g.drawString("11 ", (int) (x + 10 - Math.cos(Math.PI / z[8]) * 110),
    (int) (y + 10 - Math.sin(Math.PI / z[8]) * 110));
  g.drawString("12 ", x - 5, y - 90);
  g.setColor(Color.blue);
  g.fillOval(x - 5, y - 115, 10, 10);
  g.fillOval(x + 105, y - 5, 10, 10);
  g.fillOval(x - 5, y + 105, 10, 10);
  g.fillOval(x - 115, y - 5, 10, 10);
  for (int i = 0; i < 14; i++) {
   if (i == 9 | i == 4) {
    g.setColor(Color.green);
    g.fillOval((int) (x + Math.sin(Math.PI / z[i]) * 110),
      (int) (y - Math.cos(Math.PI / z[i]) * 110), 6, 6);
    g.fillOval((int) (x + Math.cos(Math.PI / z[i]) * 110),
      (int) (y + Math.sin(Math.PI / z[i]) * 110), 6, 6);
    g.fillOval((int) (x - Math.sin(Math.PI / z[i]) * 110),
      (int) (y + Math.cos(Math.PI / z[i]) * 110), 6, 6);
    g.fillOval((int) (x - Math.cos(Math.PI / z[i]) * 110),
      (int) (y - Math.sin(Math.PI / z[i]) * 110), 6, 6);
    continue;
   }
   g.setColor(Color.red);
   g.fillOval((int) (x + Math.sin(Math.PI / z[i]) * 110),
     (int) (y - Math.cos(Math.PI / z[i]) * 110), 2, 2);
   g.fillOval((int) (x + Math.cos(Math.PI / z[i]) * 110),
     (int) (y + Math.sin(Math.PI / z[i]) * 110), 2, 2);
   g.fillOval((int) (x - Math.sin(Math.PI / z[i]) * 110),
     (int) (y + Math.cos(Math.PI / z[i]) * 110), 2, 2);
   g.fillOval((int) (x - Math.cos(Math.PI / z[i]) * 110),
     (int) (y - Math.sin(Math.PI / z[i]) * 110), 2, 2);
  }
  // 显示日期
  g.drawString("Switzerland ", x - 35, y + 50);
  g.setFont(new Font("魏体 ", 1, 20));
  g.drawString("FIYATA ", x - 35, y - 50);
  // 秒针
  g.setColor(Color.red);
  if (second == 0)
   g.drawLine(x, y, x, y - 90);
  if (second > 0 & second < 15) {
   int i = second - 1;
   g.drawLine(x, y, (int) (x + Math.sin(Math.PI / z[i]) * 90),
     (int) (y - Math.cos(Math.PI / z[i]) * 90));
  }
  if (second == 15)
   g.drawLine(x, y, x + 90, y);
  if (second > 15 & second < 30) {
   int i = second - 16;
   g.drawLine(x, y, (int) (x + Math.cos(Math.PI / z[i]) * 90),
     (int) (y + Math.sin(Math.PI / z[i]) * 90));
  }
  if (second == 30)
   g.drawLine(x, y, x, y + 90);
  if (second > 30 & second < 45) {
   int i = second - 31;
   g.drawLine(x, y, (int) (x - Math.sin(Math.PI / z[i]) * 90),
     (int) (y + Math.cos(Math.PI / z[i]) * 90));
  }
  if (second == 45)
   g.drawLine(x, y, x - 90, y);
  if (second > 45 & second < 60) {
   int i = second - 46;
   g.drawLine(x, y, (int) (x - Math.cos(Math.PI / z[i]) * 90),
     (int) (y - Math.sin(Math.PI / z[i]) * 90));
  }
  // 分针
  g.setColor(Color.green);
  if (hour >= 12)
   hour = (hour - 12) * 5;
  else
   hour = hour * 5;
  if (minutes >= 12 & minutes < 24)
   hour = hour + 1;
  if (minutes >= 24 & minutes < 36)
   hour = hour + 2;
  if (minutes >= 36 & minutes < 48)
   hour = hour + 3;
  if (minutes >= 48 & minutes < 60)
   hour = hour + 4;
  if (minutes == 0)
   g.drawLine(x, y, x, y - 65);
  if (minutes > 0 & minutes < 15) {
   int i = minutes - 1;
   g.drawLine(x, y, (int) (x + Math.sin(Math.PI / z[i]) * 65),
     (int) (y - Math.cos(Math.PI / z[i]) * 65));
  }
  if (minutes == 15)
   g.drawLine(x, y, x + 65, y);
  if (minutes > 15 & minutes < 30) {
   int i = minutes - 16;
   g.drawLine(x, y, (int) (x + Math.cos(Math.PI / z[i]) * 65),
     (int) (y + Math.sin(Math.PI / z[i]) * 65));
  }
  if (minutes == 30)
   g.drawLine(x, y, x, y + 65);
  if (minutes > 30 & minutes < 45) {
   int i = minutes - 31;
   g.drawLine(x, y, (int) (x - Math.sin(Math.PI / z[i]) * 65),
     (int) (y + Math.cos(Math.PI / z[i]) * 65));
  }
  if (minutes == 45)
   g.drawLine(x, y, x - 65, y);
  if (minutes > 45 & minutes < 60) {
   int i = minutes - 46;
   g.drawLine(x, y, (int) (x - Math.cos(Math.PI / z[i]) * 65),
     (int) (y - Math.sin(Math.PI / z[i]) * 65));
  }
  // 时针
  g.setColor(Color.blue);
  if (hour == 0)
   g.drawLine(x, y, x, y - 40);
  if (hour > 0 & hour < 15) {
   int i = hour - 1;
   g.drawLine(x, y, (int) (x + Math.sin(Math.PI / z[i]) * 40),
     (int) (y - Math.cos(Math.PI / z[i]) * 40));
  }
  if (hour == 15)
   g.drawLine(x, y, x + 40, y);
  if (hour > 15 & hour < 30) {
   int i = hour - 16;
   g.drawLine(x, y, (int) (x + Math.cos(Math.PI / z[i]) * 40),
     (int) (y + Math.sin(Math.PI / z[i]) * 40));
  }
  if (hour == 30)
   g.drawLine(x, y, x, y + 40);
  if (hour > 30 & hour < 45) {
   int i = hour - 31;
   g.drawLine(x, y, (int) (x - Math.sin(Math.PI / z[i]) * 40),
     (int) (y + Math.cos(Math.PI / z[i]) * 40));
  }
  if (hour == 45)
   g.drawLine(x, y, x - 40, y);
  if (hour > 45 & hour < 60) {
   int i = hour - 46;
   g.drawLine(x, y, (int) (x - Math.cos(Math.PI / z[i]) * 40),
     (int) (y - Math.sin(Math.PI / z[i]) * 40));
  }
  g.fillOval(x - 5, y - 5, 10, 10);
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值