⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clock.java

📁 这是用java语言编的一个小程序
💻 JAVA
字号:
import java.util.*;
import java.awt.*;
import java.applet.*;
import java.text.*;

public class Clock extends Applet implements Runnable{
     private volatile Thread timer;
     private int lastxs,lastys,lastxm,lastym,lastxh,lastyh;
     private SimpleDateFormat formatter;
     private String lastdate;
     private Font clockFaceFont;
     private Date currentDate;
     private Color handColor,numberColor;
     private int x0=80,y0=55;

    public void init(){
        int x,y;
        lastxs=lastys=lastxm=lastym=lastxh=lastyh=0;
        formatter=new SimpleDateFormat("yyyy mm dd hh:mm:ss",Locale.getDefault());
         currentDate=new Date();
         lastdate=formatter.format(currentDate);
         clockFaceFont=new Font("Serif",Font.PLAIN,14);
         handColor=Color.blue;
         numberColor=Color.darkGray;
         setBackground(Color.green);
         handColor=Color.red;
         resize(400,400);
   }
  

    public void start(){
         timer=new Thread(this);
         timer.start(); 
   }

    public void stop(){
        timer=null;
     }

    public void run(){
            Thread me=Thread.currentThread();
             while(timer==me){
                  try{
                      Thread.currentThread().sleep(100);
                  }catch(InterruptedException e){}
                   repaint();
              }
      }
  
                              
    public void paint(Graphics g){
          g.setFont(clockFaceFont);

          g.setColor(handColor);
          g.drawArc(x0-50,y0-50,100,100,0,360);
          g.setColor(numberColor);
          g.drawString("9",x0-45,y0+3);
          g.drawString("3",x0+42,y0+3);
          g.drawString("12",x0-5,y0-37);
          g.drawString("6",x0-3,y0+45);

         g.setColor(numberColor);
         g.drawString(lastdate,15,145);
	//写出当前时间
         g.drawLine(x0,y0,lastxs,lastys);
         g.setColor(handColor);
         g.drawLine(x0,y0-1,lastxm,lastym);
         g.drawLine(x0-1,y0,lastxm,lastym);
         g.drawLine(x0,y0-1,lastxh,lastyh);
         g.drawLine(x0-1,y0,lastxh,lastyh);  
    }

   public void update(Graphics g){
       int xh,yh,xm,ym,xs,ys;
        int s=0,m=10,h=10;
        String today;
        currentDate=new Date();
        formatter.applyPattern("s");
        try{
             s=Integer.parseInt(formatter.format(currentDate));
        }catch(NumberFormatException n){
              s=0;
         }
         formatter.applyPattern("m");
          try{
             m=Integer.parseInt(formatter.format(currentDate));
        }catch(NumberFormatException n){
              m=10;
         }
         formatter.applyPattern("h");
          try{
             h=Integer.parseInt(formatter.format(currentDate));
        }catch(NumberFormatException n){
              h=10;
         }

          xs=(int)(Math.cos(s*Math.PI/30-Math.PI/2)*40+x0);
          ys=(int)(Math.sin(s*Math.PI/30-Math.PI/2)*40+y0);
          xh=(int)(Math.cos(m*Math.PI/30-Math.PI/2)*30+x0);
          yh=(int)(Math.sin(m*Math.PI/30-Math.PI/2)*30+x0);
          xm=(int)(Math.cos((h*30+m/2)*Math.PI/180-Math.PI/2)*30+x0);
         ym=(int)(Math.sin((h*30+m/2)*Math.PI/180-Math.PI/2)*30+x0);

          formatter.applyPattern("yyyy mm dd hh:mm:ss");
          today=formatter.format(currentDate);
          g.setFont(clockFaceFont);
          g.setColor(getBackground());
          if (xs!=lastxs || ys!=lastys){
                g.drawLine(x0,y0,lastxs,lastys);
                g.drawString(lastdate,5,125);
          }
           if (xm!=lastxm || ym!=lastym){
                g.drawLine(x0,y0-1,lastxm,lastym);
                g.drawLine(x0-1,y0,lastxm,lastym);
          }
         if (xh!=lastxh || yh!=lastyh){
                g.drawLine(x0,y0-1,lastxh,lastyh);
                g.drawLine(x0-1,y0,lastxh,lastyh);
          }
 
         g.setColor(numberColor);
          g.drawString(today,5,125);
 
          g.drawLine(x0,y0,xs,ys);
          g.setColor(handColor);
          g.drawLine(x0,y0-1,xm,ym);
          g.drawLine(x0-1,y0,xm,ym);
          g.drawLine(x0,y0-1,xh,yh);
          g.drawLine(x0-1,y0,xh,yh);
          lastxs=xs; lastys=ys;
          lastxm=xm; lastym=ym;
          lastxh=xh; lastyh=yh;
          lastdate=today;
          currentDate=null;
   }
}   
     


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -