Java实现动态模拟时钟

作者:不能吃的坚果 时间:2022-07-25 17:35:25 

本文实例为大家分享了java动态模拟时钟的具体代码,供大家参考,具体内容如下

应用名称:java动态模拟时钟

用到的知识:javaGUI,java 绘图

开发环境:win10+eclipse+jdk1.8

功能说明:通过java绘图画出一个虚拟的动态时钟

效果图:

Java实现动态模拟时钟

源代码:


import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.lang.Thread;
import java.text.DecimalFormat;

public class StillClock extends JPanel {

/**
 * @param args
 */
private int hour;
private int minute;
private int second;

//构造函数
public StillClock() {
 setCurrentTime();
}

//返回小时
public int getHour() {
 return hour;
}

public int getMinute() {
 return minute;
}

public int getSecond() {
 return second;
}

//绘制时钟
protected void paintComponent(Graphics g) {

super.paintComponent(g);
 //初始化
 int clockRadius = (int)(Math.min(getWidth(), getHeight()) * 0.8 * 0.5);
 int xCenter = getWidth() / 2;
 int yCenter = getHeight() / 2;
 //画圆
 g.setColor(Color.black);
 g.drawOval(xCenter - clockRadius, yCenter - clockRadius, 2 * clockRadius, 2 * clockRadius);
 g.drawString("12", xCenter - 5, yCenter - clockRadius + 15);
 g.drawString("9", xCenter - clockRadius + 3, yCenter + 5);
 g.drawString("3", xCenter + clockRadius - 10, yCenter + 3);
 g.drawString("6", xCenter - 3, yCenter + clockRadius - 3);
 //画秒针
 int sLength = (int)(clockRadius * 0.8);
 int xSecond = (int)(xCenter + sLength * Math.sin(second * (2 * Math.PI / 60)));
 int ySecond = (int)(yCenter - sLength * Math.cos(second * (2 * Math.PI / 60)));

g.setColor(Color.red);
 g.drawLine(xCenter, yCenter, xSecond, ySecond);

//画分针
 int mLenth = (int)(clockRadius * 0.65);
 int xMinute = (int)(xCenter + mLenth * Math.sin(minute * (2 * Math.PI / 60)));
 int yMinute = (int)(xCenter - mLenth * Math.cos(minute * (2 * Math.PI / 60)));
 g.setColor(Color.blue);
 g.drawLine(xCenter, yCenter, xMinute, yMinute);

//画时针
 int hLength = (int)(clockRadius * 0.5);
 int xHour = (int)(xCenter + hLength * Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));
 int yHour = (int)(yCenter - hLength * Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));
 g.setColor(Color.green);
 g.drawLine(xCenter, yCenter, xHour, yHour);

//画数字时钟
 g.setColor(Color.black);
 DecimalFormat s=new DecimalFormat("00");
 g.drawString(s.format(getHour()) + ":" + s.format(getMinute()) + ":" + s.format(getSecond()), xCenter - 22, yCenter - clockRadius - 15);

}

public void setCurrentTime() {
 Calendar calendar = new GregorianCalendar();

this.hour = calendar.get(Calendar.HOUR_OF_DAY);
 this.minute = calendar.get(Calendar.MINUTE);
 this.second = calendar.get(Calendar.SECOND);
}

public static void main(String[] args) {
 // TODO Auto-generated method stub
 JFrame frame = new JFrame("DiaplayClock");
   frame.setResizable(false);
 frame.setTitle("DiaplayClock");
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(300,350);
 frame.setVisible(true);

while(true) {
  StillClock clock = new StillClock();
  frame.getContentPane().add(clock);
  clock.setVisible(true);
  frame.validate();
  try {
   Thread.sleep(1000);

}
  catch (InterruptedException e) {
   e.printStackTrace();
  }
  clock.setVisible(false);
  frame.remove(clock);
  clock = null;
  frame.validate();

}

}
}

来源:https://blog.csdn.net/C_jian/article/details/50527895

标签:java,时钟
0
投稿

猜你喜欢

  • android仿微信联系人索引列表功能

    2023-06-22 17:33:30
  • Java通过反射将 Excel 解析成对象集合实例

    2023-05-22 18:49:02
  • Spring Bean生命周期之BeanDefinition的合并过程详解

    2023-11-29 02:50:35
  • C++实现约瑟夫环的循环单链表

    2022-11-12 19:34:29
  • java开发工作中对InheritableThreadLocal使用思考

    2023-11-24 21:46:44
  • Spring Cloud Ribbon客户端详细介绍

    2023-11-27 21:36:22
  • java读取resources文件详解及实现代码

    2022-07-15 15:11:30
  • C# 三种序列化方法分享

    2022-09-21 03:43:05
  • 详解Android的四大应用程序组件

    2023-05-23 02:00:32
  • springcloud中RabbitMQ死信队列与延迟交换机实现方法

    2023-05-04 04:25:54
  • c# SendMail发送邮件实例代码

    2023-03-13 11:17:21
  • java 数据结构单链表的实现

    2022-07-24 09:45:33
  • edittext + listview 实现搜索listview中的内容方法(推荐)

    2022-03-11 21:41:57
  • Android架构组件Room的使用详解

    2022-04-30 14:59:24
  • Android崩溃异常捕获方法

    2022-08-24 08:26:12
  • C#异步编程之async/await详解

    2022-10-08 02:50:07
  • C# PropertyInfo类案例详解

    2021-12-11 16:47:45
  • 面试题:Java 实现查找旋转数组的最小数字

    2023-05-30 22:14:05
  • C#/VB.NET实现将XML转为PDF

    2023-02-11 11:21:21
  • easyexcel读取excel合并单元格数据的操作代码

    2022-08-26 16:26:21
  • asp之家 软件编程 m.aspxhome.com