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大作业,所以后来稍微改了一下那个程序就交了上去,这还是美滋滋.然后五月中旬的时候写了一个 ...
随机推荐
- Packetbeat协议扩展开发教程(3)
原文链接:http://elasticsearch.cn/article/54 书接上回:http://elasticsearch.cn/article/53 前面介绍了Packetbeat的项目结构 ...
- java异步任务处理
1.场景 最近做项目的时候遇到了一个小问题:从前台提交到服务端A,A调用服务端B处理超时,原因是前端一次请求往db插1万数据,插完之后会去清理缓存.发送消息. 服务端的有三个操作 a.插DB b.清理 ...
- Session笔记
一.Session机制 session机制採用的是在server端保持 HTTP 状态信息的方案 . server使用一种类似于散列表的结构(也可能就是使用散列表)来保存信息. 当程序须要为某个cli ...
- tomcat的几种部署方法
原文:http://blog.163.com/pg_roger/blog/static/185884389201172921211937/ 1.将应用文件夹或war文件(均指编译好的文件)塞到tomc ...
- [VirtualBox] Install Ubuntu 14.10 error 5 Input/output error
After you download the VirtualBox install package and install it (just defualt setting). Then you sh ...
- javascript 递归之阶乘
阶乘,即5! = 5*4*3*2*1, 先看传统的做法,利用while循环实现: function factorial(num){ var result = num; if(num<0){ re ...
- C#_datatable_读取
private void button5_Click(object sender, EventArgs e) { string 价格编号 = txtnum.Text; if (价格编号!= " ...
- Upgrading Applications
Upgrading Applications If you have an existing Zend Framework v2 application, and want to update it ...
- Forms and actions
Forms and actions Adding new albums We can now code up the functionality to add new albums. There ar ...
- DEM渲染洼地淹没图(转)
http://blog.csdn.net/kikitamoon/article/details/18700555 首先要准备基础数据,一张DEM栅格图. 然后将掩膜水位以下的数据提取出来以备后用.这里 ...