Java显示时钟

显示时钟。编写程序显示一个时钟。示例输出如下图所示。

package priv.lhw.show.clock;

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

public class MyFrame extends JFrame {

    public MyFrame(){
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension dimension = toolkit.getScreenSize();
        setSize(400, 400);
        setLocation((dimension.width - getWidth()) / 2, (dimension.height - getHeight()) / 2);
        setTitle("显示时钟");
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Clock clock = new Clock();
        add(clock);
    }
}
package priv.lhw.show.clock;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.time.LocalTime;

public class Clock extends JPanel {
    @Override
    public void paintComponent(Graphics g){
        Graphics2D graphics2D = (Graphics2D) g;
        //反锯齿开关开
        graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        //绘制表盘
        Ellipse2D clockDial = new Ellipse2D.Double(getWidth() / 4.0, getHeight() / 4.0, getWidth() / 2.0, getWidth() / 2.0);
        graphics2D.draw(clockDial);
        graphics2D.drawString("3", 295, 190);
        graphics2D.drawString("6", 190, 295);
        graphics2D.drawString("9", 80, 190);
        graphics2D.drawString("12", 185, 80);

        //绘制表盘刻度线
        double radius = getWidth() / 4.0;
        for (int i = 30; i > -30; i--){
            int length = i % 5 == 0 ? 10 : 5;
            /*12  w / 2 , h / 4;
            3   w / 4 * 3, h / 2;
            6   w / 2; h / 4 * 3;
            9   w / 4, h / 2;*/
            double radians = Math.toRadians(i * 6);
            Line2D tickMark = new Line2D.Double(clockDial.getCenterX() + radius * Math.sin(radians),
                    clockDial.getCenterY() + radius * Math.cos(radians),
                    clockDial.getCenterX() + (radius - length) * Math.sin(radians),
                    clockDial.getCenterY() + (radius - length) * Math.cos(radians));
            graphics2D.setColor(Color.red);
            graphics2D.draw(tickMark);
        }

        //获取当前时间,计算角度时需要注意时针和分针的偏移量
        LocalTime localTime = LocalTime.now();
        double curHour = Math.toRadians(180 - (localTime.getHour() * 30 + localTime.getMinute() / 2.0));
        double curMinute = Math.toRadians(180 - (localTime.getMinute() * 6 + localTime.getSecond() / 10.0));
        double curSecond = Math.toRadians(180 - localTime.getSecond() * 6);

        //绘制时针
        graphics2D.setColor(Color.green);
        Line2D hourHand = new Line2D.Double(clockDial.getCenterX(), clockDial.getCenterY(),
                clockDial.getCenterX() + radius / 2 * Math.sin(curHour),
                clockDial.getCenterY() + radius / 2 * Math.cos(curHour));
        graphics2D.draw(hourHand);

        //绘制分针
        graphics2D.setColor(Color.blue);
        Line2D minuteHand = new Line2D.Double(clockDial.getCenterX(), clockDial.getCenterY(),
                clockDial.getCenterX() + radius / 1.5 * Math.sin(curMinute),
                clockDial.getCenterY() + radius / 1.5 * Math.cos(curMinute));
        graphics2D.draw(minuteHand);;

        //绘制秒针
        graphics2D.setColor(Color.red);
        Line2D secondHand = new Line2D.Double(clockDial.getCenterX(), clockDial.getCenterY(),
                clockDial.getCenterX() + radius / 1.2 * Math.sin(curSecond),
                clockDial.getCenterY() + radius / 1.2 * Math.cos(curSecond));
        graphics2D.draw(secondHand);

        //绘制当前时间
        graphics2D.drawString(String.format("Hour: %2d Minute: %2d Second: %2d", localTime.getHour(), localTime.getMinute(), localTime.getSecond()), getWidth() / 7 * 2, getHeight() / 8 * 7);
    }
}

package priv.lhw.show.clock;

public class Main {

    public static void main(String[] args) {
	// write your code here
        MyFrame myFrame = new MyFrame();
        new Thread(() -> {
            while (true){
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                myFrame.repaint();
            }
        }).start();
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值