加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java数字时钟实现代码

发布时间:2020-12-15 03:19:39 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 ? 这是一个数字钟表程序,主要功能是从系统中获取当前的系统时间然后再将其显示在数字时钟上,由于整个的数字时钟都是用函数构成的,所以它可以实现一

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

? 这是一个数字钟表程序,主要功能是从系统中获取当前的系统时间然后再将其显示在数字时钟上,由于整个的数字时钟都是用函数构成的,所以它可以实现一般的数 字时钟所不具有的功能,比如说它可以被鼠标指针拖动到窗口的任意位置,除此之外它还可以实现钟表大小随鼠标滚轮的滚动而变大变小的操作。
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,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)(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,width,height);
drawCircle(g,cx,cy,R,s,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)(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)(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)(x+R),(int)y);
g.drawLine((int)x,(int)(y+R-10),(int)(y+R));
g.drawLine((int)(x-R+10),(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)));
}
}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读