JAVA时钟

效果图如下:

  1 //简单动态时钟程序,以图形和数字两种方式来显示当前时间
  2 import javax.swing.*;
  3 import java.awt.event.*;
  4 import java.awt.*;
  5 import java.util.Calendar;
  6 import java.util.GregorianCalendar;
  7 
  8 public class Clock extends JFrame implements ActionListener
  9 {
 10     int x,y,x0,y0,r,h,olds_x,olds_y,oldm_x,oldm_y,oldh_x,oldh_y,ss,mm,hh,old_m,old_h,ang;
 11     final double RAD = Math.PI/180;    //度数转化成弧度的比例
 12     
 13     //构造函数创建一个窗体
 14     public Clock()
 15     {
 16         super("ShuBing时钟");
 17         setDefaultCloseOperation(3);
 18         //Image image = getToolkit().getImage("clock.gif");
 19         //setIconImage(image);
 20         setSize(200,200);
 21         setBackground(Color.BLACK);
 22         setLocation(300,150);
 23         setResizable(false);
 24         show();
 25         int delay = 1000;
 26         //创造一个监听事件
 27         ActionListener drawClock = new ActionListener()
 28         {
 29             public void actionPerformed(ActionEvent evt)
 30             {
 31                 repaint();
 32             }
 33         };
 34         //创造一个时间计数器,每一秒触发一次
 35         new Timer(delay,drawClock).start();
 36     }
 37 
 38     //实现ActionListener接口必须实现的方法
 39     public void actionPerformed(ActionEvent arg0) {
 40     }
 41     //绘制图形
 42     public void paint(Graphics g)
 43     {
 44         Graphics2D g2D = (Graphics2D) g;
 45         Insets insets = getInsets();
 46         int L = insets.left/2, T=insets.top/2;
 47         h = getSize().height;
 48         g.setColor(Color.white);
 49         //画圆
 50         g2D.setStroke(new BasicStroke(4.0f));
 51         g.drawOval(L+40, T+40, h-80, h-80);
 52         r = h/2-40;
 53         x0 = 40+r-5+L;
 54         y0 = 40+r-5-T;
 55         ang = 60;
 56         //绘制时钟上的12个数字
 57         for(int i=1; i<=12; i++)
 58         {
 59             x=(int)((r+10)*Math.cos(RAD*ang)+x0);
 60             y=(int)((r+10)*Math.sin(RAD*ang)+y0);
 61             g.drawString(""+i, x, h-y);
 62             ang -= 30;
 63         }
 64         //获得现在时间
 65         Calendar now = new GregorianCalendar();
 66         int nowh = now.get(Calendar.HOUR_OF_DAY);
 67         int nowm = now.get(Calendar.MINUTE);
 68         int nows = now.get(Calendar.SECOND);
 69         String st;
 70         if(nowh<10) st = "0"+nowh; else st = ""+nowh;
 71         if(nowm<10) st += ":0"+nowm; else st += ":"+nowm;
 72         if(nows<10) st += ":0"+nows; else st += ":"+nows;
 73         //在窗体上显示时间
 74         g.setColor(Color.pink);
 75         g.fillRect(L, T, 50, 28);
 76         g.setColor(Color.BLUE);
 77         g.drawString(st,L+2,T+26);
 78         //计算时间与度数的关系
 79         ss = 90 - nows*6;
 80         mm=90 - nowm*6;
 81         hh = 90 - nowh*30-nowm/2;
 82         x0 = r+40+L;
 83         y0 = r+40+T;
 84         g2D.setStroke(new BasicStroke(1.2f));
 85         //擦除秒针
 86         if(olds_x>0)
 87         {
 88             g.setColor(getBackground());
 89             g.drawLine(x0, y0, olds_x, h-olds_y);
 90         }
 91         else
 92         {
 93             old_m = mm;
 94             old_h = hh;
 95         }
 96         //绘制秒针
 97         x=(int)(r*0.9*Math.cos(RAD*ss))+x0;
 98         y=(int)(r*0.9*Math.sin(RAD*ss))+y0-2*T;
 99         g.setColor(Color.yellow);
100         g.drawLine(x0, y0, x, h-y);
101         olds_x = x;
102         olds_y = y;
103         g2D.setStroke(new BasicStroke(2.2f));
104         //擦除分针
105         if(old_m != mm)
106         {
107             g.setColor(getBackground());
108             g.drawLine(x0, y0, oldm_x, h-oldm_y);
109         }
110         //绘制分针
111         x=(int)(r*0.7*Math.cos(RAD*mm))+x0;
112         y=(int)(r*0.7*Math.sin(RAD*mm))+y0-2*T;
113         g.setColor(Color.green);
114         g.drawLine(x0, y0, x, h-y);
115         oldm_x = x;
116         oldm_y = y;
117         old_m = mm;
118         g2D.setStroke(new BasicStroke(3.4f));
119         //擦除时针
120         if(old_h != hh)
121         {
122             g.setColor(getBackground());
123             g.drawLine(x0, y0, oldh_x, h-oldh_y);
124         }
125         //绘制时针
126         x=(int)(r*0.5*Math.cos(RAD*hh))+x0;
127         y=(int)(r*0.5*Math.sin(RAD*hh))+y0-2*T;
128         g.setColor(Color.red);
129         g.drawLine(x0, y0, x, h-y);
130         oldh_x = x;
131         oldh_y = y;
132         old_h = hh;
133     }
134     public static void main(String[] args)
135     {
136         new Clock();
137     }
138 }
原文地址:https://www.cnblogs.com/acm-bingzi/p/3553588.html