时钟程序设计java_Java数字时钟实现代码详解
这是一个数字钟表程序,主要功能是从系统中获取当前的系统时间然后再将其显示在数字时钟上,由于整个的数字时钟都是用函数构成的,所以它可以实现一般的数 字时钟所不具有的功能,比如说它可以被鼠标指针拖动到窗口的任意位置,除此之外它还可以实现钟表大小随鼠标滚轮的滚动而变大变小的操作。
package TheClock;
import java.awt.*;
import javax.swing.*;
import java.lang.*;
import java.awt.event.*;
import java.util.*;
public class TheClock extends JApplet implements Runnable,MouseMotionListener,MouseListener,MouseWheelListener{
private Thread t;
boolean dstatus=true;
public static final double PI=Math.PI/180;
Image offScreenImage=null;
Graphics offScreenBuffer=null;
int width=1440;
int height=900;
double R=90;
int cx=250,cy=150;
int x2,y2;
public void init(){
t=new Thread(this);
t.start();
offScreenImage=this.createImage(width,height);
offScreenBuffer=offScreenImage.getGraphics();
addMouseListener(this);
addMouseMotionListener(this);
addMouseWheelListener(this);
}
public void update(Graphics g){
paint(g);
}
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseDragged(MouseEvent e){
x2=e.getX();
y2=e.getY();
cx=x2;
cy=y2;
repaint();
}
public void mouseMoved(MouseEvent e){}
public void mouseWheelMoved(MouseWheelEvent e){
int count=e.getWheelRotation();
if(count>0){
R+=count;
}else if(count<0){
R+=count;
}
}
public void run(){
while(true){
try{
t.sleep(1000);
}catch(Exception e){}
repaint();
}
}
public void paint(Graphics g){
int s,m,h;
//创建时间
Date rightNow=new Date();
//获取系统时间
String today=rightNow.toLocaleString();
s=rightNow.getSeconds();
m=rightNow.getMinutes();
h=rightNow.getHours();
g.drawImage(offScreenImage,0,0,this);
g.setColor(Color.orange);
g.fillOval((int)(cx-R), (int)(cy-R),(int)(2*R),(int)(2*R));
g.setColor(Color.black);
g.drawOval((int)(cx-R), (int)(cy-R),(int)(2*R),(int)(2*R));
//画刻度(1,2,4,5,7,8,10,11)
g.drawString("12", cx-8, (int)(cy-R+18));
g.drawString("3", (int)(cx+R-18), cy+4);
g.drawString("6",cx-4,(int)(cy+R-10));
g.drawString("9", (int)(cx-R+12), cy+4);
g.drawString("我的钟表", cx-20, (int)(cy-R-20));
g.drawString(today,cx-55,(int)(cy+R+30));
g.setFont(new Font("TimesRoman",Font.PLAIN,14));
offScreenBuffer.clearRect(0,0,width,height);
drawCircle(g,cx,cy,R,s,m,h);
}
public void drawCircle(Graphics g,double x,double y,double R,double s,double m,double h){
double x1,y1;
x1=x+R*Math.cos((s*6-90)*PI);
y1=y+R*Math.sin((s*6-90)*PI);
//画秒针
g.setColor(Color.blue);
g.drawLine((int)x, (int)y, (int)x1, (int)y1);
//画分针
g.setColor(Color.green);
g.drawLine((int)x, (int)y, (int)(x+(R/4*3)*Math.cos((m*6-90)*PI)), (int)(y+(R/4*3)*Math.sin((m*6-90)*PI)));
//画时针
g.setColor(Color.red);
g.drawLine((int)x, (int)y, (int)(x+(R/2)*Math.cos((h*30+m*6/12-90)*PI)), (int)(y+(R/2)*Math.sin((h*30+m*6/12-90)*PI)));
//画刻度(3,6,9,12)
g.setColor(Color.black);
g.drawLine((int)x,(int)(y-R+10),(int)x,(int)(y-R));
g.drawLine((int)(x+R-10),(int)y,(int)(x+R),(int)y);
g.drawLine((int)x,(int)(y+R-10),(int)x,(int)(y+R));
g.drawLine((int)(x-R+10),(int)y,(int)(x-R),(int)y);
g.drawLine((int)(x+(R-5)*Math.cos(30*PI)),(int)(y+(R-5)*Math.sin(30*PI)),(int)(x+R*Math.cos(30*PI)),(int)(y+R*Math.sin(30*PI)));
g.drawLine((int)(x+(R-5)*Math.cos(60*PI)),(int)(y+(R-5)*Math.sin(60*PI)),(int)(x+R*Math.cos(60*PI)),(int)(y+R*Math.sin(60*PI)));
g.drawLine((int)(x+(R-5)*Math.cos(120*PI)),(int)(y+(R-5)*Math.sin(120*PI)),(int)(x+R*Math.cos(120*PI)),(int)(y+R*Math.sin(120*PI)));
g.drawLine((int)(x+(R-5)*Math.cos(150*PI)),(int)(y+(R-5)*Math.sin(150*PI)),(int)(x+R*Math.cos(150*PI)),(int)(y+R*Math.sin(150*PI)));
g.drawLine((int)(x+(R-5)*Math.cos(210*PI)),(int)(y+(R-5)*Math.sin(210*PI)),(int)(x+R*Math.cos(210*PI)),(int)(y+R*Math.sin(210*PI)));
g.drawLine((int)(x+(R-5)*Math.cos(240*PI)),(int)(y+(R-5)*Math.sin(240*PI)),(int)(x+R*Math.cos(240*PI)),(int)(y+R*Math.sin(240*PI)));
g.drawLine((int)(x+(R-5)*Math.cos(300*PI)),(int)(y+(R-5)*Math.sin(300*PI)),(int)(x+R*Math.cos(300*PI)),(int)(y+R*Math.sin(300*PI)));
g.drawLine((int)(x+(R-5)*Math.cos(330*PI)),(int)(y+(R-5)*Math.sin(330*PI)),(int)(x+R*Math.cos(330*PI)),(int)(y+R*Math.sin(330*PI)));
}
}
时钟程序设计java_Java数字时钟实现代码详解相关推荐
- 四、用简单神经网络识别手写数字(内含代码详解及订正)
本博客主要内容为图书<神经网络与深度学习>和National Taiwan University (NTU)林轩田老师的<Machine Learning>的学习笔记,因此在全 ...
- 常见字典用法集锦及代码详解
目录 前言 字典的简介 1. 字典对象 1.1 Add 方法 1.2 Exists 方法 1.3 Keys 方法 1.4 Items 方法 1.5 Remove 方法 1.6 RemoveAll 方法 ...
- 【OpenCV/C++】KNN算法识别数字的实现原理与代码详解
KNN算法识别数字 一.KNN原理 1.1 KNN原理介绍 1.2 KNN的关键参数 二.KNN算法识别手写数字 2.1 训练过程代码详解 2.2 预测分类的实现过程 三.KNN算法识别印刷数字 2. ...
- 基于多相滤波器的数字信道化算法详解(Python, Verilog代码已开源)
基于多相滤波器的数字信道化算法详解 推导过程 总结 仿真 本文详细介绍了基于多相滤波器的数字信道化算法的推导过程, 如果您在阅读的过程中发现算法推导过程中有任何错误, 请不吝指出. 此外, 进入我的G ...
- python split函数 空格_最易懂的Python新手教程:从基础语法到代码详解
导读:本文立足基础,讲解Python和PyCharm的安装,及Python最简单的语法基础和爬虫技术中所需的Python语法. 作者:罗攀 蒋仟 如需转载请联系华章科技 本文涉及的主要知识点如下: P ...
- 异步FIFO设计:各个模块的作用及Verilog代码详解
实现原理参考:异步FIFO---Verilog实现_alangaixiaoxiao的博客-CSDN博客_异步fifo 代码参考:IC基础(一):异步FIFO_MaoChuangAn的博客-CSDN博客 ...
- 《STM32从零开始学习历程》——CAN通讯代码详解
<STM32从零开始学习历程>@EnzoReventon CAN通讯代码详解 相关链接: <STM32从零开始学习历程>--CAN通讯协议物理层 CAN-bus规范 V2.0版 ...
- 204B实战应用-LMK04821代码详解(二)
204B实战应用-LMK04821代码详解(二) 一. SPI协议 通过阅读LMK04821数据手册,我们可以从中知道,可以通过SPI协议对LMK04821进行寄存器的配置工作,进而实现我们设计所需要 ...
- FPGA项目开发:204B实战应用-LMK04821代码详解(二)
大侠好,欢迎来到FPGA技术江湖,江湖偌大,相见即是缘分.大侠可以关注FPGA技术江湖,在"闯荡江湖"."行侠仗义"栏里获取其他感兴趣的资源,或者一起煮酒言欢. ...
最新文章
- 依赖注入?依赖注入是如何实现解耦的?
- 微信小程序小结02-- 完整的demo
- .NET 6 使用 string.Create 提升字符串创建和拼接性能
- PHP中的get_defined_funciton和get_defined_vars
- 如何形象地理解 Python 中的 int 类型是任意精度这一概念?
- linux命名管道fifo通信示例
- opencv实际案例(一)银行卡号的识别
- 【PPT模板】甄选100套时间轴精品
- 名言警句 研习补弱 历年珍题
- C++按键模拟/刷屏器
- c3p0-0.9.1.2.jar与c3p0-0.9.5.2.jar
- linux打开db文件,我如何打开. db 文件?
- Collaborative Filtering with Temporal Dynamics
- 计算机cmd卸载软件,在CMD模式下如何卸载软件
- 8.4.1 搭建自己的SSR
- iOS下载APP之后直接跳转到信任界面
- FFmpeg windows 录屏(录像)录音 实测
- 怎么可以修改pr基本图形中的文字_10、Pr中基本图形安装使用,点点就可以应用高级的字幕...
- 在国外期刊发表文章时一定用到的,SCI论文写法攻略
- 【阿里巴巴百川掌中测 限量内测招募啦】来自阿里的移动开发测试洪荒之力