时间:2021-05-19
本文实例为大家分享了java动态模拟时钟的具体代码,供大家参考,具体内容如下
应用名称:java动态模拟时钟
用到的知识:javaGUI,java 绘图
开发环境:win10+eclipse+jdk1.8
功能说明:通过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(); } }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
这篇文章介绍了使用JFrame完成动态模拟时钟,在面板中绘制时钟并提取系统当前时刻,主方法中暂停线程1秒,刷新面板。实现代码如下importjavax.swin
本文实例讲述了javascript实现模拟时钟的方法。分享给大家供大家参考。具体实现方法如下:javascript模拟时钟functionFormat2Len(
本文实例为大家分享了JS实现动态时钟的具体代码,供大家参考,具体内容如下利用setTime()实现动态的时钟效果代码如下正在运行的时钟/*设置样式:无边框的文本
本文实例为大家分享了Java实现动态数字时钟的具体代码,供大家参考,具体内容如下构建:Clock继承JFrame为运行页面ClockText测试类创建Clock
Python实现模拟时钟代码推荐#coding=utf8importsys,pygame,math,randomfrompygame.localsimport*