时钟表盘代码

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);
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,我是一个语言模型AI,没有办法直接为您编写代码。不过,以下是一个使用Python语言绘制RTC时钟表盘的示例代码,供您参考: ```python import turtle import math # 设置窗口大小 turtle.setup(800, 800) turtle.title("RTC时钟表盘") # 绘制表盘外框 turtle.penup() turtle.goto(0, -200) turtle.pendown() turtle.circle(200) # 绘制小时刻度 for i in range(12): turtle.penup() turtle.goto(0, 170) turtle.right(i * 30) turtle.pendown() turtle.forward(20) turtle.penup() turtle.goto(0, 150) turtle.write(str(i+1), align="center", font=("Arial", 12, "normal")) turtle.goto(0, 0) # 绘制分钟刻度 for i in range(60): turtle.penup() turtle.goto(0, 190) turtle.right(i * 6) turtle.pendown() if i % 5 == 0: turtle.forward(10) else: turtle.dot(2) turtle.goto(0, 0) # 绘制时针、分针和秒针 hour_hand = turtle.Turtle() minute_hand = turtle.Turtle() second_hand = turtle.Turtle() hour_hand.penup() minute_hand.penup() second_hand.penup() hour_hand.goto(0, 0) minute_hand.goto(0, 0) second_hand.goto(0, 0) hour_hand.right(90) minute_hand.right(90) second_hand.right(90) hour_hand.pensize(5) minute_hand.pensize(3) second_hand.pensize(1) while True: # 获取当前时间 now = turtle.datetime.datetime.now() # 计算时针、分针和秒针的角度 hour_angle = (now.hour % 12) * 30 + now.minute / 2 minute_angle = now.minute * 6 second_angle = now.second * 6 # 旋转时针、分针和秒针 hour_hand.setheading(hour_angle) minute_hand.setheading(minute_angle) second_hand.setheading(second_angle) # 绘制时针、分针和秒针 hour_hand.pendown() hour_hand.forward(60) hour_hand.penup() minute_hand.pendown() minute_hand.forward(90) minute_hand.penup() second_hand.pendown() second_hand.forward(120) second_hand.penup() # 暂停1秒钟 turtle.delay(1000) # 清除时针、分针和秒针 hour_hand.clear() minute_hand.clear() second_hand.clear() turtle.done() ``` 该代码使用turtle库绘制RTC时钟表盘和时针、分针、秒针,并通过获取当前时间来控制时针、分针、秒针的旋转。您可以根据自己的需求进行修改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值