m0_73865497 2022-09-29 01:21 采纳率: 100%
浏览 49
已结题

有没有搞人帮看一下这个怎么做,后面不会做了。

问题:让我们表示大小的模拟时钟的初始大小。
当客户端的半径为r时,t秒中同心的半径为r*t/60。
将刻度添加到手表中。
设计是自由。
您可以使用形状来标记比例或添加数字。

img

import javax.swing.;
import java.awt.
;
import java.time.*;

public class ClockWriter extends JPanel {

private final int SIZE;

public ClockWriter(int n) {
    SIZE = n;
    JFrame frame = new JFrame();
    frame.setTitle("Clock");
    frame.setSize(SIZE+50, SIZE+150);
    frame.getContentPane().add(this);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public void paintComponent(Graphics g) {
    g.setColor(Color.BLUE);
    g.drawString("TIME IS GOLD", 105, 50);
    g.setColor(Color.LIGHT_GRAY);
    g.fillOval(25, 100, SIZE, SIZE);
    LocalTime now = LocalTime.now();
    int radius = SIZE / 2;
     int x1 = 25 + radius;
     int y1 = 100 + radius;
     radius -= 30;
      double t = now.getSecond();
     double l = t/60;
     int r = (int)(SIZE*(l));
         
     g.setColor(Color.PINK);
     g.fillOval(r,r,r,r);
     double minute_angle = (now.getMinute() - 15) * Math.PI / 30;
     int x2 = x1 + (int)(radius * Math.cos(minute_angle));
     int y2 = y1 + (int)(radius * Math.sin(minute_angle));
     g.setColor(Color.RED);
     g.drawLine(x1, y1, x2, y2);
     radius -= 30;
     double hour_angle = (now.getHour() - 3) * Math.PI / 6 + minute_angle / 12;
     x2 = x1 + (int)(radius * Math.cos(hour_angle));
     y2 = y1 + (int)(radius * Math.sin(hour_angle));
     g.setColor(Color.YELLOW);
     g.drawLine(x1, y1, x2, y2);
}

public static void main(String[] args) {
    new ClockWriter(250);
}

}

  • 写回答

1条回答 默认 最新

  • 一束尘光 2022-09-29 13:32
    关注

    帮你调整了一下参数,分针的角度如果以12点钟方向为基准,比较好计算,顺便加了一下动态刷新和刻度盘。如果对你有帮助,请采纳

    public class ClockWriter extends JPanel{
        private final int SIZE;
    
        public ClockWriter(int n) {
            SIZE = n;
            JFrame frame = new JFrame();
            frame.setTitle("Clock");
            frame.setSize(SIZE+50, SIZE+150);
            frame.getContentPane().add(this);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    
        public void paintComponent(Graphics g) {
            g.setColor(Color.BLUE);
            g.drawString("TIME IS GOLD", 105, 50);
            g.setColor(Color.LIGHT_GRAY);
            g.fillOval(25, 100, SIZE, SIZE);
            LocalTime now = LocalTime.now();
            int radius = SIZE / 2;
            int x1 = 25 + radius;
            int y1 = 100 + radius;
            radius -= 30;
            double t = now.getSecond();
            double l = t/60;
            int r = (int)(SIZE*(l));
    
            // 绘制刻度盘
            paintClockDialog(g);
    
            g.setColor(Color.PINK);
            // 改动行1
            g.fillOval(25 + (SIZE - r) / 2, 100 + (SIZE - r) / 2, r, r);
    
            // 改动行2,12点方向顺时针到分针的夹角
            double minute_angle = 2 * Math.PI * now.getMinute() / 60;
            // 改动行3, 4
            int x2 = x1 + (int)(radius * Math.sin(minute_angle));
            int y2 = y1 - (int)(radius * Math.cos(minute_angle));
            g.setColor(Color.RED);
            g.drawLine(x1, y1, x2, y2);
            radius -= 30;
            double hour_angle = (now.getHour() - 3) * Math.PI / 6 + minute_angle / 12;
            x2 = x1 + (int)(radius * Math.cos(hour_angle));
            y2 = y1 + (int)(radius * Math.sin(hour_angle));
            g.setColor(Color.YELLOW);
            g.drawLine(x1, y1, x2, y2);
        }
    
        private void paintClockDialog(Graphics g) {
            g.setColor(Color.BLACK);
            int radius = SIZE / 2;
            int x = 25 + radius, y = 100 + radius;
            for (int i = 0; i < 12; i++) {
                double angle = 2 * Math.PI * i / 12d;
                int x1 = x + (int)((radius - 9) * Math.sin(angle));
                int y1 = y + (int)((radius - 9) * Math.cos(angle));
                int x2 = x + (int)(radius * Math.sin(angle));
                int y2 = y + (int)(radius * Math.cos(angle));
                g.drawLine(x1, y1, x2, y2);
            }
        }
    
        public static void main(String[] args) {
            // 改动5,1秒钟刷新1次
            ClockWriter clockWriter = new ClockWriter(250);
            while (true) {
                clockWriter.repaint(1000);
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月8日
  • 已采纳回答 9月30日
  • 创建了问题 9月29日

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂