效果图如下:

  1. //简单动态时钟程序,以图形和数字两种方式来显示当前时间
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. import java.awt.*;
  5. import java.util.Calendar;
  6. import java.util.GregorianCalendar;
  7.  
  8. public class Clock extends JFrame implements ActionListener
  9. {
  10. int x,y,x0,y0,r,h,olds_x,olds_y,oldm_x,oldm_y,oldh_x,oldh_y,ss,mm,hh,old_m,old_h,ang;
  11. final double RAD = Math.PI/180; //度数转化成弧度的比例
  12.  
  13. //构造函数创建一个窗体
  14. public Clock()
  15. {
  16. super("ShuBing时钟");
  17. setDefaultCloseOperation(3);
  18. //Image image = getToolkit().getImage("clock.gif");
  19. //setIconImage(image);
  20. setSize(200,200);
  21. setBackground(Color.BLACK);
  22. setLocation(300,150);
  23. setResizable(false);
  24. show();
  25. int delay = 1000;
  26. //创造一个监听事件
  27. ActionListener drawClock = new ActionListener()
  28. {
  29. public void actionPerformed(ActionEvent evt)
  30. {
  31. repaint();
  32. }
  33. };
  34. //创造一个时间计数器,每一秒触发一次
  35. new Timer(delay,drawClock).start();
  36. }
  37.  
  38. //实现ActionListener接口必须实现的方法
  39. public void actionPerformed(ActionEvent arg0) {
  40. }
  41. //绘制图形
  42. public void paint(Graphics g)
  43. {
  44. Graphics2D g2D = (Graphics2D) g;
  45. Insets insets = getInsets();
  46. int L = insets.left/2, T=insets.top/2;
  47. h = getSize().height;
  48. g.setColor(Color.white);
  49. //画圆
  50. g2D.setStroke(new BasicStroke(4.0f));
  51. g.drawOval(L+40, T+40, h-80, h-80);
  52. r = h/2-40;
  53. x0 = 40+r-5+L;
  54. y0 = 40+r-5-T;
  55. ang = 60;
  56. //绘制时钟上的12个数字
  57. for(int i=1; i<=12; i++)
  58. {
  59. x=(int)((r+10)*Math.cos(RAD*ang)+x0);
  60. y=(int)((r+10)*Math.sin(RAD*ang)+y0);
  61. g.drawString(""+i, x, h-y);
  62. ang -= 30;
  63. }
  64. //获得现在时间
  65. Calendar now = new GregorianCalendar();
  66. int nowh = now.get(Calendar.HOUR_OF_DAY);
  67. int nowm = now.get(Calendar.MINUTE);
  68. int nows = now.get(Calendar.SECOND);
  69. String st;
  70. if(nowh<10) st = "0"+nowh; else st = ""+nowh;
  71. if(nowm<10) st += ":0"+nowm; else st += ":"+nowm;
  72. if(nows<10) st += ":0"+nows; else st += ":"+nows;
  73. //在窗体上显示时间
  74. g.setColor(Color.pink);
  75. g.fillRect(L, T, 50, 28);
  76. g.setColor(Color.BLUE);
  77. g.drawString(st,L+2,T+26);
  78. //计算时间与度数的关系
  79. ss = 90 - nows*6;
  80. mm=90 - nowm*6;
  81. hh = 90 - nowh*30-nowm/2;
  82. x0 = r+40+L;
  83. y0 = r+40+T;
  84. g2D.setStroke(new BasicStroke(1.2f));
  85. //擦除秒针
  86. if(olds_x>0)
  87. {
  88. g.setColor(getBackground());
  89. g.drawLine(x0, y0, olds_x, h-olds_y);
  90. }
  91. else
  92. {
  93. old_m = mm;
  94. old_h = hh;
  95. }
  96. //绘制秒针
  97. x=(int)(r*0.9*Math.cos(RAD*ss))+x0;
  98. y=(int)(r*0.9*Math.sin(RAD*ss))+y0-2*T;
  99. g.setColor(Color.yellow);
  100. g.drawLine(x0, y0, x, h-y);
  101. olds_x = x;
  102. olds_y = y;
  103. g2D.setStroke(new BasicStroke(2.2f));
  104. //擦除分针
  105. if(old_m != mm)
  106. {
  107. g.setColor(getBackground());
  108. g.drawLine(x0, y0, oldm_x, h-oldm_y);
  109. }
  110. //绘制分针
  111. x=(int)(r*0.7*Math.cos(RAD*mm))+x0;
  112. y=(int)(r*0.7*Math.sin(RAD*mm))+y0-2*T;
  113. g.setColor(Color.green);
  114. g.drawLine(x0, y0, x, h-y);
  115. oldm_x = x;
  116. oldm_y = y;
  117. old_m = mm;
  118. g2D.setStroke(new BasicStroke(3.4f));
  119. //擦除时针
  120. if(old_h != hh)
  121. {
  122. g.setColor(getBackground());
  123. g.drawLine(x0, y0, oldh_x, h-oldh_y);
  124. }
  125. //绘制时针
  126. x=(int)(r*0.5*Math.cos(RAD*hh))+x0;
  127. y=(int)(r*0.5*Math.sin(RAD*hh))+y0-2*T;
  128. g.setColor(Color.red);
  129. g.drawLine(x0, y0, x, h-y);
  130. oldh_x = x;
  131. oldh_y = y;
  132. old_h = hh;
  133. }
  134. public static void main(String[] args)
  135. {
  136. new Clock();
  137. }
  138. }

JAVA时钟的更多相关文章

  1. Java 时钟

    <!DOCTYPE html><html lang="zh"><head> <meta charset="UTF-8" ...

  2. 【Matlab编程】Matlab及Java小时钟

    一年前曾经用matlab的gui做了一个时钟,由于是直接用GUIDE和ActiveX控件写的,程序虽说有许多行,大多数都是自动生成的,自己写的只有十几行而已.闲着没事,就耗费了下午的时间用matlab ...

  3. 【Qt编程】基于Qt的词典开发系列--后序

    从去年八月份到现在,总算完成了词典的编写以及相关技术文档的编辑工作.从整个过程来说,文档的编写比程序的实现耗费的时间更多.基于Qt的词典开发系列文章,大致包含了在编写词典软件过程中遇到的技术重点与难点 ...

  4. 物联网时代-跟着Thingsboard学IOT架构-HTTP设备协议及API相关限制

    thingsboard官网: https://thingsboard.io/ thingsboard GitHub: https://github.com/thingsboard/thingsboar ...

  5. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  6. 理解Java对象的交互:时钟显示程序

    实现: 结构: 对象:时钟  - 对象:小时                 - 对象:分钟 小时和分钟具有相同属性(值,上限),可以用一个类Display来定义这两个对象: 但是两者之间又具有联系( ...

  7. java多线程并发编程与CPU时钟分配小议

    我们先来研究下JAVA的多线程的并发编程和CPU时钟振荡的关系吧 老规矩,先科普 我们的操作系统在DOS以前都是单任务的 什么是单任务呢?就是一次只能做一件事 你复制文件的时候,就不能重命名了 那么现 ...

  8. Java多线程之sleep方法阻塞线程-模拟时钟

    package org.study2.javabase.ThreadsDemo.status; import java.text.SimpleDateFormat; import java.util. ...

  9. Java实现时钟小程序【代码】

    哎,好久没上博客园发东西了,上一次还是两个月前的五一写的一篇计算器博客,不过意外的是那个程序成了这学期的Java大作业,所以后来稍微改了一下那个程序就交了上去,这还是美滋滋.然后五月中旬的时候写了一个 ...

随机推荐

  1. 使用ApplicationLoader中出现报错:The IPA is invalid. It does not inlude a Payload directory

    问题处理方法: 1.将achieve的.app后缀的软件包放在一个payload的文件夹中 2.压缩该文件夹,改变.zip后缀为.ipa 3.使用applicationLoader上传该文件  

  2. 看文章《EAI和SOA的比较》有感(1)

    <EAI和SOA的比较>http://www.cnblogs.com/asdling/archive/2007/11/26/973100.html这篇文章写的很全面,至少自己这么认为,也解 ...

  3. ORM之二:核心接口与扩展操作

    一.数据库提供者接口 /// <summary> /// 数据库提供者 /// </summary> public interface IDbProvider : IDispo ...

  4. 并发与同步 (一) ThreadLocal与Synchronized 用哪一个好

    ThreadLocal是什么? 历史 早在JDK 1.2的版本中就提供java.lang.ThreadLocal,ThreadLocal为解决多线程程序的并发问题提供了一种新的思路.使用这个工具类可以 ...

  5. POJ_1365_Prime_Land

    //懒得解释 #include <iostream> #include <cstring> #include <cmath> #include <cstdio ...

  6. 【源码分享】iOS-OC版五子棋

    五子棋是程序猿比较熟悉的一款小游戏,相信很多人大学时期就用多种语言写过五子棋小游戏,笔者工作闲暇之余,试着用OC实现了一下,在这里给大家分享一下.有不足之处,欢迎大家提供建议和指点! GitHub源码 ...

  7. 兼容:判断 iframe 是否加载完成

    判断 iframe 是否加载完成其实与 判断 JavaScript 文件是否加载完成 采用的方法很类似 var iframe = document.createElement("iframe ...

  8. Apache配置虚拟目录,以及各种操作

    apache配置虚拟目录: 打开并创建虚拟目录的步骤如下: # Virtual hosts # Include conf/extra/httpd-vhosts.conf 去掉conf/http.con ...

  9. Android(java)学习笔记119:继承中父类没有无参构造

    /* 如果父类没有无参构造方法,那么子类的构造方法会出现什么现象呢? 报错. 如何解决呢? A:在父类中加一个无参构造方法 B:通过使用super关键字去显示的调用父类的带参构造方法 C:子类通过th ...

  10. boot2docker安装及使用

    更新homebrew 为了确保有boot2docker的安装脚本 brew update 安装boot2docker brew install boot2docker 初始化 boot2docker ...