Clock.java
上传用户:tendy5003
上传日期:2022-07-16
资源大小:210k
文件大小:4k
源码类别:

ICQ/即时通讯

开发平台:

Java

  1. import java.util.*;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.applet.*;
  6. import javax.swing.Timer;
  7. public class Clock extends JFrame implements ActionListener
  8.  { 
  9.     Timer timer;
  10.     int x,y,old_X,old_Y, r,x0,y0,w,h,ang; 
  11.     int sdo,mdo,hdo,old_M,old_H; 
  12.     TimeZone tz =TimeZone.getTimeZone("JST");
  13.     final double RAD=Math.PI/180.0; 
  14.     
  15.   
  16.   public Clock()
  17.   {
  18.      super("[HappyChat]时钟"); 
  19.       setSize(300,300);
  20.         setBackground(new Color(0,0,192));
  21.         setResizable(false);    
  22.         Dimension scr=Toolkit.getDefaultToolkit().getScreenSize();//在屏幕居中显示
  23.         Dimension fra=this.getSize();
  24.         if(fra.width>scr.width)
  25.         {
  26.             fra.width=scr.width;
  27.         }
  28.         if(fra.height>scr.height)
  29.         {
  30.             fra.height=scr.height;
  31.         }
  32. this.setLocation((scr.width-fra.width)/2,(scr.height-fra.height)/2);
  33.     show();
  34.      int delay = 1000;
  35.     
  36.      //窗体添加事件监听,监听秒表的触发
  37.      ActionListener taskPerformer = new ActionListener()
  38.      {
  39.        public void actionPerformed(ActionEvent evt) 
  40.        {
  41.           repaint();
  42.        }
  43.      };
  44.      new Timer(delay, taskPerformer).start();
  45.   }
  46.   
  47.   public void actionPerformed(ActionEvent e) 
  48.   {
  49.         timer.restart();
  50.   }
  51.   public void paint( Graphics g ) 
  52.   { 
  53. Insets insets = getInsets();
  54.       int L0 = (insets.left)/2, T0 = (insets.top)/2;
  55.       int hh,mm,ss; 
  56.       String st; 
  57.       h=getSize().height;
  58.       //绘制圆形
  59.       g.setColor(Color.white); 
  60.       g.drawOval(L0+30,T0+30,h-60,h-60);
  61.       g.drawOval(L0+32,T0+32,h-64,h-64);
  62.       r=h/2-30;
  63.       x0=30+r-5+L0;
  64.       y0=30+r-5-T0;
  65.       ang=60;
  66.       for (int i=1; i<=12; i++) 
  67.       { 
  68.          x=(int)((r+10)*Math.cos(RAD*ang)+x0);
  69.          y=(int)((r+10)*Math.sin(RAD*ang)+y0);
  70.          g.drawString(""+i,x,h-y);
  71.          ang-=30;
  72.       } 
  73.       x0=30+r+L0; y0=30+r+T0;
  74.       //获取时间
  75.       Calendar now=Calendar.getInstance();
  76.       hh=now.get(Calendar.HOUR_OF_DAY);//小时
  77.       mm=now.get(Calendar.MINUTE);//分钟
  78.       ss=now.get(Calendar.SECOND);// 秒
  79.       g.setColor(Color.pink); 
  80.       g.fillRect(L0,T0,60,28);//填充的矩形
  81.       g.setColor(Color.blue); 
  82.       if (hh < 10) st="0"+hh;     else st=""+hh; 
  83.       if (mm < 10) st=st+":0"+mm; else st=st+":"+mm; 
  84.       if (ss < 10) st=st+":0"+ss; else st=st+":"+ss; 
  85.       g.drawString(st,L0,T0+25);
  86.       //计算时间和图形的关系
  87.       sdo=90-ss*6;
  88.       mdo=90-mm*6;
  89.       hdo=90-hh*30-mm/2;
  90.       //擦除秒针
  91.       if (old_X > 0) 
  92.       {
  93.          g.setColor(getBackground());
  94.          g.drawLine(x0,y0,old_X,(h-old_Y));
  95.       } else 
  96.       { 
  97.          old_M=mdo;
  98.          old_H=hdo;
  99.       } 
  100.       //绘制秒针
  101.       g.setColor(Color.yellow); 
  102.       x=(int)((r-8)*Math.cos(RAD*sdo)+x0); 
  103.       y=(int)((r-8)*Math.sin(RAD*sdo)+y0)-2*T0; 
  104.       g.drawLine(x0,y0,x,(h-y));
  105.      
  106.       old_X=x; 
  107.       old_Y=y; 
  108.       //擦除分针和时针
  109.       if (mdo != old_M) 
  110.       {
  111.         line(g,old_M,(int)(r*0.7),getBackground());
  112.         old_M=mdo;
  113.       } 
  114.       if (hdo != old_H) 
  115.       {
  116.         line(g,old_H,(int)(r*0.5),getBackground());
  117.         old_H=hdo;
  118.     } 
  119.       //绘制分针
  120.       line(g,mdo,(int)(r*0.7),Color.green);
  121.       //绘制时针
  122.       line(g,hdo,(int)(r*0.5),Color.red);
  123.   } // end paint 
  124. public void line(Graphics g, int t, int n, Color c) 
  125.     { 
  126. int [] xp = new int[4];
  127.        int [] yp = new int[4]; 
  128.        xp[0]=x0;
  129. yp[0]=y0;
  130.        xp[1]=  (int)((n-10)*Math.cos(RAD*(t-4))+x0); 
  131.        yp[1]=h-(int)((n-10)*Math.sin(RAD*(t-4))+y0); 
  132.        xp[2]=  (int)( n    *Math.cos(RAD* t   )+x0); 
  133.        yp[2]=h-(int)( n    *Math.sin(RAD* t   )+y0); 
  134.        xp[3]=  (int)((n-10)*Math.cos(RAD*(t+4))+x0); 
  135.        yp[3]=h-(int)((n-10)*Math.sin(RAD*(t+4))+y0); 
  136.        g.setColor(c); 
  137.        g.fillPolygon(xp,yp,4);
  138.    }
  139.    
  140. public static void main(String args[])
  141.     {
  142.     new Clock();
  143.     }
  144.  }