Java数字时钟实现代码,java数字时钟,这是一个数字钟表程序,主
Java数字时钟实现代码,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)(2R),(int)(2R));g.setColor(Color.black);g.drawOval((int)(cx-R), (int)(cy-R),(int)(2R),(int)(2R));//画刻度(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+RMath.cos((s6-90)PI);y1=y+RMath.sin((s6-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/43)Math.cos((m6-90)PI)), (int)(y+(R/43)Math.sin((m6-90)PI)));//画时针g.setColor(Color.red);g.drawLine((int)x, (int)y, (int)(x+(R/2)Math.cos((h30+m6/12-90)PI)), (int)(y+(R/2)Math.sin((h30+m6/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(30PI)),(int)(y+(R-5)Math.sin(30PI)),(int)(x+RMath.cos(30PI)),(int)(y+RMath.sin(30PI)));g.drawLine((int)(x+(R-5)Math.cos(60PI)),(int)(y+(R-5)Math.sin(60PI)),(int)(x+RMath.cos(60PI)),(int)(y+RMath.sin(60PI)));g.drawLine((int)(x+(R-5)Math.cos(120PI)),(int)(y+(R-5)Math.sin(120PI)),(int)(x+RMath.cos(120PI)),(int)(y+RMath.sin(120PI)));g.drawLine((int)(x+(R-5)Math.cos(150PI)),(int)(y+(R-5)Math.sin(150PI)),(int)(x+RMath.cos(150PI)),(int)(y+RMath.sin(150PI)));g.drawLine((int)(x+(R-5)Math.cos(210PI)),(int)(y+(R-5)Math.sin(210PI)),(int)(x+RMath.cos(210PI)),(int)(y+RMath.sin(210PI)));g.drawLine((int)(x+(R-5)Math.cos(240PI)),(int)(y+(R-5)Math.sin(240PI)),(int)(x+RMath.cos(240PI)),(int)(y+RMath.sin(240PI)));g.drawLine((int)(x+(R-5)Math.cos(300PI)),(int)(y+(R-5)Math.sin(300PI)),(int)(x+RMath.cos(300PI)),(int)(y+RMath.sin(300PI)));g.drawLine((int)(x+(R-5)Math.cos(330PI)),(int)(y+(R-5)Math.sin(330PI)),(int)(x+RMath.cos(330PI)),(int)(y+RMath.sin(330*PI)));}}
```
用户点评