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大作业,所以后来稍微改了一下那个程序就交了上去,这还是美滋滋.然后五月中旬的时候写了一个 ...
随机推荐
- SQLite使用教程8 Insert 语句
http://www.runoob.com/sqlite/sqlite-insert.html SQLite Insert 语句 SQLite 的 INSERT INTO 语句用于向数据库的某个表中添 ...
- BZOJ 1199: [HNOI2005]汤姆的游戏 计算几何暴力
1199: [HNOI2005]汤姆的游戏 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...
- jquery html标签的链式语法
<div id="ProductNet"> <table border='0' cellspacing='2' cellpadding='0' style='te ...
- [010]Try块和异常处理
Throw表达式和try块的定义如下: 1. throw 表达式,错误检测部分使用这种表达式来说明遇到了不可处理的错误.可以说,throw 引发了异常条件. 2. try 块,错误处理部分使用它来处理 ...
- PAT 1014
1014. Waiting in Line (30) Suppose a bank has N windows open for service. There is a yellow line in ...
- Migration from Zend Framework v2 to v3
Migration from Zend Framework v2 to v3 Zend Framework v2 to v3 has been intended as an incremental u ...
- android studio的里的 content_XXX_xml问题
遇到这个问题就是androidStudio的版本问题,androidStudio会出现这个问题是在androidStudio1.4以上版本当遇到这个问题的时候建Activity的时候选择EmptyAc ...
- 通过scrollerview自定义collectionview(常用于推荐关注界面)
最近项目上要实现一个效果,先把效果图摆上来吧: 刚看到效果图的时候 我觉得很简单 用UICollectionview就可以了 但是后来发现collectionview只有两种布局方式 比较单一 ...
- Linux上安装Redmine
安装基本的软件环境 # yum install zip unzip libyaml-devel zlib-devel curl-devel openssl-devel httpd-devel apr- ...
- CentOS7安装RabbitMQ集群
实验环境 RabbitMQ 集群 server1.example.com IP: 10.10.10.11 Node: diskserver2.example.com IP: 10.1 ...