java实现时钟表盘

 更新时间:2022年09月11日 08:33:59   作者:Jiafu_Liu  
这篇文章主要为大家详细介绍了java实现时钟表盘,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java实现时钟表盘的具体代码,供大家参考,具体内容如下

设计并实现一个模拟时钟功能的应用程序。程序中应显示时针、分针和秒针,并同时以数字形式显示当前时间。

实现结果:

源代码如下:

//ClockPanel.java
import javax.swing.*;
import java.util.Calendar;  
import java.util.GregorianCalendar;  
import java.util.Timer;  
import java.util.TimerTask;  
import java.text.SimpleDateFormat;
import java.util.Locale;  
import java.awt.*;
import java.awt.event.*;

public class ClockPanel extends JPanel{
    private GregorianCalendar calendar;
    private JButton btn;
    private JButton btn2;
    private int currentState=8; 
    private String zone;
    private int hourTemp;
    final int X=320, Y=240, R=120;   // 圆心坐标,半径
    private int xPos,yPos;
    private int hour,minute,second; 
    private int xHour,yHour,xMinute,yMinute,xSecond,ySecond;//表针位置(大端)
    private int xHour1,yHour1,xMinute1,yMinute1,xSecond1,ySecond1;//表针位置(小端)
    private double a_sec,a_min ,a_hour;//角度

    ClockPanel() {   // 创建定时器对象
        Timer t = new Timer();  
        Task task = new Task();  
        t.schedule(task, 0, 1000); 
        setLayout(new BorderLayout(10,20)); 
        btn=new JButton("时区  上");
        btn2=new JButton("时区 下");
        btn.setBorder(BorderFactory.createRaisedBevelBorder());
        btn2.setBorder(BorderFactory.createRaisedBevelBorder());
        btn.setBackground(Color.green);
        btn2.setBackground(Color.green);
        btn.addActionListener(new ButtonListener()); 
        btn2.addActionListener(new ButtonListener()); 
        add(btn,BorderLayout.WEST); 
        add(btn2,BorderLayout.EAST); 
    }
    //相关事件处理
    private class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {    

            if (event.getSource()==btn)
                currentState++;
            if (event.getSource()==btn2)
                currentState--;
        }

    }

    public void paintComponent(Graphics g){ 
        super.paintComponent(g); 
        double alfa;    //所画点对应的角度
        Graphics2D g2d=(Graphics2D)g;
        BasicStroke bstroke=new BasicStroke(1.0f);
        BasicStroke bstroke2=new BasicStroke(2.0f);
        BasicStroke bstroke3=new BasicStroke(3.0f);

        g2d.setStroke(bstroke2);
        for(int i=0;i<=360;i+=6)  {
            alfa=Math.toRadians(i);  //角度用弧度表示
            xPos=X+(int)(R*Math.cos(alfa));   // x坐标
            yPos=Y-(int)(R*Math.sin(alfa));   // y坐标
            int xBegin=320+(int)(144*Math.sin(alfa));
            int yBegin=240-(int)(144*Math.cos(alfa));
            int xEnd=320+(int)(159*Math.sin(alfa));
            int yEnd=240-(int)(159*Math.cos(alfa));

            g2d.setColor(Color.BLACK);
            g2d.drawLine(xBegin,yBegin,xEnd,yEnd);

            g2d.setColor(Color.RED);
            switch(i){  // 写时钟数字刻度
                case 0: g2d.drawString("3", xPos,yPos);
                        break;
                case 90: g2d.drawString("12", xPos,yPos);
                        break;
                case 180: g2d.drawString("9", xPos,yPos);
                        break;
                case 270: g2d.drawString("6",xPos,yPos);
                        break;      
            }

            if(i%30==0){
                g2d.drawLine(xBegin,yBegin,xEnd,yEnd);
            }

        }


        g2d.setColor(Color.BLACK);
        g2d.setStroke(bstroke3);
        g2d.drawLine(X, Y, xHour,yHour);    // 画时针
        g2d.drawLine(X, Y, xHour1,yHour1);  
        g2d.setColor(Color.BLUE);
        g2d.setStroke(bstroke2);
        g2d.drawLine(X, Y, xMinute,yMinute);    // 画分针
        g2d.drawLine(X, Y, xMinute1,yMinute1);
        g2d.setColor(Color.RED);
        g2d.setStroke(bstroke);
        g2d.drawLine(X, Y, xSecond,ySecond);    // 画秒针
        g2d.drawLine(X, Y, xSecond1,ySecond1);
        //表盘中心点1
        g2d.drawOval(317,237,6,6); 
        g2d.fillOval(317,237,6,6);
        //表盘中心点2
        g2d.setColor(Color.BLACK);
        g2d.drawOval(319,238,4,4); 
        g2d.fillOval(319,238,4,4);
        //表盘中心圆环
        g2d.setColor(Color.ORANGE);
        g2d.drawOval(300,220,40,40); 
        g2d.setColor(Color.black);
        g2d.drawString("15010140079",290,376); 

        GregorianCalendar gre=new GregorianCalendar(); 
        SimpleDateFormat dateforamt1=new SimpleDateFormat("yyyy年MM月dd日E");
        //SimpleDateFormat dateforamt2=new SimpleDateFormat("H时m分s秒");
        g2d.setColor(Color.black);
        g2d.setFont(new Font("SAN_SERIF",Font.BOLD,20)); 
        g2d.drawString(dateforamt1.format(gre.getTime()),250,50);

        g2d.drawString(hour+"时"+minute+"分"+second+"秒",260,430);
        //时区判断
        if(currentState>12){
            currentState=-11;
        }
        else if(currentState<-11){
            currentState=12;
        }
        if(currentState<=12&&currentState>=1)
            zone="东"+currentState+"区";
        else
            zone="西"+(1-currentState)+"区";
        g2d.drawString(zone,170,50); 

    }

    class Task extends TimerTask {  
        public void run() {  
            calendar = new GregorianCalendar();  
            hourTemp=currentState>0?(currentState-8):(currentState-1);
            hour = calendar.get(Calendar.HOUR)+hourTemp;  
            minute = calendar.get(Calendar.MINUTE);  
            second = calendar.get(Calendar.SECOND); 

            a_sec = second * 2 * Math.PI / 60;
            a_min = minute * 2 * Math.PI / 60 + a_sec / 60;
            a_hour = hour * 2 * Math.PI / 12 + a_min / 12;
            // 计算时、分、秒针的末端位置
            xSecond=320+(int)(110*Math.sin(a_sec));
            ySecond=240-(int)(110*Math.cos(a_sec));
            xMinute=320+(int)(90 *Math.sin(a_min));
            yMinute=240-(int)(90 *Math.cos(a_min));
            xHour=  320+(int)(70 *Math.sin(a_hour));
            yHour=  240-(int)(70 *Math.cos(a_hour));
            xSecond1=320-(int)(22*Math.sin(a_sec));
            ySecond1=240+(int)(22*Math.cos(a_sec));
            xMinute1=320-(int)(15*Math.sin(a_min));
            yMinute1=240+(int)(15*Math.cos(a_min));
            xHour1  =320-(int)(5 *Math.sin(a_hour));
            yHour1  =240+(int)(5 *Math.cos(a_hour));

            repaint();  
        }  
    }  
}


//
//Clock.java
import javax.swing.*;

public class Clock{ 
    public static void main(String[] args) {
         JFrame frame=new JFrame("Clock");    //创建图文框
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.getContentPane().add(new ClockPanel()); //添加面板
         frame.setVisible(true);
         frame.setSize(640,480);         
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Spring @Transactional事务失效的原因分析

    Spring @Transactional事务失效的原因分析

    一个程序中不可能没有事务,Spring中,事务的实现方式分为两种:编程式事务和声明式事务。日常项目中,我们都会使用声明式事务 @Transactional来实现事务,本文来和大家聊聊什么情况会导致@Transactional事务失效
    2022-09-09
  • Java多线程中synchronized的工作原理

    Java多线程中synchronized的工作原理

    这篇文章主要介绍了Java多线程中synchronized的工作原理,本期讲解 synchronized 工作的原理以及常见的锁优化机制,相信大家在看完这篇博文后对 synchronized 工作流程有一定的理解,需要的朋友可以参考下
    2023-07-07
  • Java emoji持久化mysql过程详解

    Java emoji持久化mysql过程详解

    这篇文章主要介绍了Java emoji持久化mysql过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • 新版idea创建spring boot项目的详细教程

    新版idea创建spring boot项目的详细教程

    这篇文章给大家介绍了新版idea创建spring boot项目的详细教程,本教程对新手小白友好,若根据教程创建出现问题导致失败可下载我提供的源码,在文章最后,本教程较新,文中通过图文给大家介绍的非常详细,感兴趣的朋友可以参考下
    2024-01-01
  • Java 垃圾回收机制详解及实例代码

    Java 垃圾回收机制详解及实例代码

    这篇文章主要介绍了 Java 垃圾回收机制详解及实例代码的相关资料,需要的朋友可以参考下
    2017-02-02
  • Java 全面系统介绍反射的运用

    Java 全面系统介绍反射的运用

    准备入手学习java的安全了,感觉这也是一个大的趋势,想着尽早进入到java安全的探索中,在反序列化链的学习之前,需要先学习反射,不多说了,开干吧
    2022-03-03
  • Java并发编程数据库与缓存数据一致性方案解析

    Java并发编程数据库与缓存数据一致性方案解析

    这篇文章主要为大家介绍了Java并发编程中数据库与缓存数据一致性解决方案,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪
    2022-04-04
  • Spring Boot将@RestController误用于视图跳转问题解决

    Spring Boot将@RestController误用于视图跳转问题解决

    这篇文章主要为大家介绍了Spring Boot将@RestController误用于视图跳转问题解决方案详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-06-06
  • java 压缩和解压缩Zip、Jar、Gzip文件实例代码

    java 压缩和解压缩Zip、Jar、Gzip文件实例代码

    本文主要介绍java压缩和解压缩Zip、Jar、Gzip文件的知识,这里整理了相关资料,并附示例代码有兴趣的小伙伴可以参考下
    2016-09-09
  • Spring Boot系列教程之日志配置

    Spring Boot系列教程之日志配置

    这篇文章主要给大家介绍了关于Spring Boot系列教程之日志配置的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-11-11

最新评论