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))); 
} 
} 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/10553.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论