黑马程序员JAVA编写的 时钟 程序

  import java.awt.*;
  import java.awt.event.*;
  import java.util.*;
  import javax.swing.event.*;
  import javax.swing.*;
  import javax.swing.Timer;
  public class Clock extends JFrame implements ActionListener{
      Timer timer;
      int x,y,old_X,old_Y, r,x0,y0,w,h,ang;
      int sdo,mdo,hdo,old_M,old_H;
      TimeZone tz =TimeZone.getTimeZone("JST");
      final double RAD=Math.PI/180.0;

  public static void main(String[] args){
      Clock cl = new Clock();
  }
  public void actionPerformed(ActionEvent e) {
                      timer.restart();
      }
  Clock(){
      super("@A荣天");
      setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
      setBackground(new Color(0,0,0));
      setSize(300,300);
      setVisible(true);
      setAlwaysOnTop(true);
      setResizable(false);
      setLocationRelativeTo(null);
      int delay = 1000;
      ActionListener taskPerformer = new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
                repaint();
          }  
      };
      new Timer(delay, taskPerformer).start();
  }
 
  public void paint( Graphics g ) {
        Insets insets = getInsets();
        int L0 = (insets.left)/2, T0 = (insets.top)/2;
        int hh,mm,ss;
        String st;
        h=getSize().height;
        g.setColor(Color.white);
        g.drawOval(L0+30,T0+30,h-60,h-60);
        g.drawOval(L0+32,T0+32,h-64,h-64);
        r=h/2-30;
        x0=30+r-5+L0;
        y0=30+r-5-T0;
        ang=60;
        for (int i=1; i<=12; i++) {
              x=(int)((r+10)*Math.cos(RAD*ang)+x0);
              y=(int)((r+10)*Math.sin(RAD*ang)+y0);
              g.drawString(""+i,x,h-y);
              ang-=30;
        }
        x0=30+r+L0; y0=30+r+T0;
        Calendar now=Calendar.getInstance();
        hh=now.get(Calendar.HOUR_OF_DAY);
        mm=now.get(Calendar.MINUTE);
        ss=now.get(Calendar.SECOND);
        g.setColor(Color.blue);
        g.fillRect(L0,T0,60,28);
        g.setColor(Color.lightGray);
        if (hh < 10) st="0"+hh;        else st=""+hh;
        if (mm < 10) st=st+":0"+mm; else st=st+":"+mm;
        if (ss < 10) st=st+":0"+ss; else st=st+":"+ss;
        g.drawString(st,L0,T0+25);
        sdo=90-ss*6;
        mdo=90-mm*6;
        hdo=90-hh*30-mm/2;
        if (old_X > 0) {
              g.setColor(getBackground());
              g.drawLine(x0,y0,old_X,(h-old_Y));
        } else {
              old_M=mdo;
              old_H=hdo;
        }
        g.setColor(Color.yellow);
        x=(int)((r-8)*Math.cos(RAD*sdo)+x0);
        y=(int)((r-8)*Math.sin(RAD*sdo)+y0)-2*T0;
        g.drawLine(x0,y0,x,(h-y));
       
        old_X=x;
        old_Y=y;
        if (mdo != old_M) {
            line(g,old_M,(int)(r*0.7),getBackground());
            old_M=mdo;
        }
        if (hdo != old_H) {
            line(g,old_H,(int)(r*0.5),getBackground());
            old_H=hdo;
        }
        line(g,mdo,(int)(r*0.7),Color.WHITE);
        line(g,hdo,(int)(r*0.5),Color.red);
  }
    public void line(Graphics g, int t, int n, Color c) {
          int [] xp = new int[4];
          int [] yp = new int[4];
          xp[0]=x0;
          yp[0]=y0;
          xp[1]=  (int)((n-10)*Math.cos(RAD*(t-4))+x0);
          yp[1]=h-(int)((n-10)*Math.sin(RAD*(t-4))+y0);
          xp[2]=  (int)( n      *Math.cos(RAD* t    )+x0);
          yp[2]=h-(int)( n      *Math.sin(RAD* t    )+y0);
          xp[3]=  (int)((n-10)*Math.cos(RAD*(t+4))+x0);
          yp[3]=h-(int)((n-10)*Math.sin(RAD*(t+4))+y0);
          g.setColor(c);
          g.fillPolygon(xp,yp,4);
    }
  } 黑马程序员JAVA编写的 <wbr>时钟 <wbr>程序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值