JAVA时钟
效果图如下:

//简单动态时钟程序,以图形和数字两种方式来显示当前时间
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Calendar;
import java.util.GregorianCalendar; public class Clock extends JFrame implements ActionListener
{
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;
final double RAD = Math.PI/180; //度数转化成弧度的比例 //构造函数创建一个窗体
public Clock()
{
super("ShuBing时钟");
setDefaultCloseOperation(3);
//Image image = getToolkit().getImage("clock.gif");
//setIconImage(image);
setSize(200,200);
setBackground(Color.BLACK);
setLocation(300,150);
setResizable(false);
show();
int delay = 1000;
//创造一个监听事件
ActionListener drawClock = new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
repaint();
}
};
//创造一个时间计数器,每一秒触发一次
new Timer(delay,drawClock).start();
} //实现ActionListener接口必须实现的方法
public void actionPerformed(ActionEvent arg0) {
}
//绘制图形
public void paint(Graphics g)
{
Graphics2D g2D = (Graphics2D) g;
Insets insets = getInsets();
int L = insets.left/2, T=insets.top/2;
h = getSize().height;
g.setColor(Color.white);
//画圆
g2D.setStroke(new BasicStroke(4.0f));
g.drawOval(L+40, T+40, h-80, h-80);
r = h/2-40;
x0 = 40+r-5+L;
y0 = 40+r-5-T;
ang = 60;
//绘制时钟上的12个数字
for(int i=1; i<=12; i++)
{
x=(int)((r+10)*Math.cos(RAD*ang)+x0);
y=(int)((r+10)*Math.sin(RAD*ang)+y0);
g.drawString(""+i, x, h-y);
ang -= 30;
}
//获得现在时间
Calendar now = new GregorianCalendar();
int nowh = now.get(Calendar.HOUR_OF_DAY);
int nowm = now.get(Calendar.MINUTE);
int nows = now.get(Calendar.SECOND);
String st;
if(nowh<10) st = "0"+nowh; else st = ""+nowh;
if(nowm<10) st += ":0"+nowm; else st += ":"+nowm;
if(nows<10) st += ":0"+nows; else st += ":"+nows;
//在窗体上显示时间
g.setColor(Color.pink);
g.fillRect(L, T, 50, 28);
g.setColor(Color.BLUE);
g.drawString(st,L+2,T+26);
//计算时间与度数的关系
ss = 90 - nows*6;
mm=90 - nowm*6;
hh = 90 - nowh*30-nowm/2;
x0 = r+40+L;
y0 = r+40+T;
g2D.setStroke(new BasicStroke(1.2f));
//擦除秒针
if(olds_x>0)
{
g.setColor(getBackground());
g.drawLine(x0, y0, olds_x, h-olds_y);
}
else
{
old_m = mm;
old_h = hh;
}
//绘制秒针
x=(int)(r*0.9*Math.cos(RAD*ss))+x0;
y=(int)(r*0.9*Math.sin(RAD*ss))+y0-2*T;
g.setColor(Color.yellow);
g.drawLine(x0, y0, x, h-y);
olds_x = x;
olds_y = y;
g2D.setStroke(new BasicStroke(2.2f));
//擦除分针
if(old_m != mm)
{
g.setColor(getBackground());
g.drawLine(x0, y0, oldm_x, h-oldm_y);
}
//绘制分针
x=(int)(r*0.7*Math.cos(RAD*mm))+x0;
y=(int)(r*0.7*Math.sin(RAD*mm))+y0-2*T;
g.setColor(Color.green);
g.drawLine(x0, y0, x, h-y);
oldm_x = x;
oldm_y = y;
old_m = mm;
g2D.setStroke(new BasicStroke(3.4f));
//擦除时针
if(old_h != hh)
{
g.setColor(getBackground());
g.drawLine(x0, y0, oldh_x, h-oldh_y);
}
//绘制时针
x=(int)(r*0.5*Math.cos(RAD*hh))+x0;
y=(int)(r*0.5*Math.sin(RAD*hh))+y0-2*T;
g.setColor(Color.red);
g.drawLine(x0, y0, x, h-y);
oldh_x = x;
oldh_y = y;
old_h = hh;
}
public static void main(String[] args)
{
new Clock();
}
}
JAVA时钟的更多相关文章
- Java 时钟
<!DOCTYPE html><html lang="zh"><head> <meta charset="UTF-8" ...
- 【Matlab编程】Matlab及Java小时钟
一年前曾经用matlab的gui做了一个时钟,由于是直接用GUIDE和ActiveX控件写的,程序虽说有许多行,大多数都是自动生成的,自己写的只有十几行而已.闲着没事,就耗费了下午的时间用matlab ...
- 【Qt编程】基于Qt的词典开发系列--后序
从去年八月份到现在,总算完成了词典的编写以及相关技术文档的编辑工作.从整个过程来说,文档的编写比程序的实现耗费的时间更多.基于Qt的词典开发系列文章,大致包含了在编写词典软件过程中遇到的技术重点与难点 ...
- 物联网时代-跟着Thingsboard学IOT架构-HTTP设备协议及API相关限制
thingsboard官网: https://thingsboard.io/ thingsboard GitHub: https://github.com/thingsboard/thingsboar ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- 理解Java对象的交互:时钟显示程序
实现: 结构: 对象:时钟 - 对象:小时 - 对象:分钟 小时和分钟具有相同属性(值,上限),可以用一个类Display来定义这两个对象: 但是两者之间又具有联系( ...
- java多线程并发编程与CPU时钟分配小议
我们先来研究下JAVA的多线程的并发编程和CPU时钟振荡的关系吧 老规矩,先科普 我们的操作系统在DOS以前都是单任务的 什么是单任务呢?就是一次只能做一件事 你复制文件的时候,就不能重命名了 那么现 ...
- Java多线程之sleep方法阻塞线程-模拟时钟
package org.study2.javabase.ThreadsDemo.status; import java.text.SimpleDateFormat; import java.util. ...
- Java实现时钟小程序【代码】
哎,好久没上博客园发东西了,上一次还是两个月前的五一写的一篇计算器博客,不过意外的是那个程序成了这学期的Java大作业,所以后来稍微改了一下那个程序就交了上去,这还是美滋滋.然后五月中旬的时候写了一个 ...
随机推荐
- pyinstaller打包第一个wxPython程序HelloWorld
pyinstaller 打包hello 7Mb ================= www.pyinstaller.org pip install pypiwin32 pip install pyin ...
- css中文字体unicode对照表
为什么要使用Unicode编码代替中文字体 在CSS中使用中文字体通常直接设置字体名称,比如设置字体为宋体:font-family:’宋体’:但因此产生的一个问题是,如果默认编码并不是UTF-8,这会 ...
- iOS 正则表达式小结
#pragma mark - 正则第一种表示方式-利用NSPredicate(谓词)匹配// NSString *email = @"15078357696@163.com" ...
- Using breakpad in cocos2d-x 3.2,dump信息收集
作者:HU 转载请注明,原文链接:http://www.cnblogs.com/xioapingguo/p/4037268.html 一.基本步骤 1.生成转换工具 2.把breakpad加入到项目 ...
- -join 和 -split 用法
具体可参考 PowerShell_ISE的帮助文件: -Join(一元联接运算符): 一元联接运算符 (-join <string[]>) 的优先级高于逗号.因此,如果向一元联接运算符提交 ...
- C++中的new与delete总结
1. operator new.operator delete与new.delete操作符的区别: operator new的作用类似于malloc,负责分配内存:operator delete的作用 ...
- [React Fundamentals] Owner Ownee Relationship
The owner-ownee relationship is used to designate a parent-child relationship with React components ...
- Asp.Net转换Html加号显示为空格的字符!(自已备用)
Asp.Net转换Html显示为空格的字符!(自已备用) 显示+(加号),须要替换一下!
- java_利用session校验图片认证码
RegisterServlet:检验server,client验证码是否一致 ImageServlet: 产生验证码 <!DOCTYPE html> <html> <he ...
- HDU 4259 - Double Dealing(求循环节)
首先将扑克牌进行一次置换,然后分解出所有的循环节,所有循环节的扑克牌个数的最小公倍数即为答案 #include <stdio.h> #include <string.h> #i ...