java数字时钟

 
 
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import static java.lang.Math.*;
public class Clock extends JApplet{
    private ClockPaint panel;
    private Thread t;
    @Override
    public void init()
    {
        Calendar d=Calendar.getInstance();
        panel=new ClockPaint(d.get(Calendar.MONTH),d.get(Calendar.DATE),d.get(Calendar.DAY_OF_WEEK),d.get(Calendar.HOUR)*5+d.get(Calendar.MINUTE)/12,d.get(Calendar.MINUTE),d.get(Calendar.SECOND));
        panel.setBackground(Color.black);
        add(panel);
        Runnable r=new ClockRunnable(panel);
        t=new Thread(r);
        t.start();
    }
}
 
class ClockRunnable extends Thread
{
    private ClockPaint component;
    public ClockRunnable(ClockPaint aComponent)
    {
        component=aComponent;
    }
    @Override
    public void run()
    {
        try
        {
Calendar d=Calendar.getInstance();
            component.reset(d.get(Calendar.HOUR)*5+d.get(Calendar.MINUTE)/12,d.get(Calendar.MINUTE),d.get(Calendar.SECOND));
            for(;;)
            {
                component.repaint();
                Toolkit.getDefaultToolkit().beep();
                Thread.sleep(1000);
            }
        }
        catch(InterruptedException e){}
    }
}
 
class ClockPaint extends JPanel
{
private ArrayList<Line2D> Slines,Mlines,Hlines;   
private double X,Y;
    private int r,x,y;
    private double SR,MR,HR;
    private int Mtag,Htag,Stag;
    private String Month1,Date1,Week1;
    public ClockPaint(int month,int date,int week,int hour,int minute,int second)
    {
        Stag=second;Mtag=minute;Htag=hour;
        if(month<9)
        {
            Month1="0"+(month+1);
        }
        else
        {
            Month1=""+(month+1);
        }
        if(date<10)
        {
            Date1="0"+date;
        }
        else
        {
            Date1=""+date;
        }
        switch(week)
        {
            case 1:
                   Week1=" 日";
                   break;
            case 2:
                   Week1=" 一";
                   break;
            case 3:
                   Week1=" 二";
                   break;
            case 4:
                   Week1=" 三";
                   break;
            case 5:
                   Week1=" 四";
                   break;
            case 6:
                   Week1=" 五";
                   break;
            case 7:
                   Week1=" 六";
                   break;
        }
        X=300;Y=300;x=300;y=300;
        HR=160;SR=220;MR=180;
        r=250;
        Slines=new ArrayList<Line2D>();
        Mlines=new ArrayList<Line2D>();
        Hlines=new ArrayList<Line2D>();
        for(int i=0;i<60;++i)
        {
            Slines.add(new Line2D.Double(300,300,X+sin(PI*i/30)*SR,Y-cos(PI*i/30)*SR));
            Mlines.add(new Line2D.Double(300,300,X+sin(PI*i/30)*MR,Y-cos(PI*i/30)*MR));
            Hlines.add(new Line2D.Double(300,300,X+sin(PI*i/30)*HR,Y-cos(PI*i/30)*HR));
        }
    }
    public void reset(int h,int m,int s)
    {
        Stag=s;Mtag=m;Htag=h;
    }
    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2=(Graphics2D)g;
        g2.setPaint(new GradientPaint(80,80,Color.red,520.0F,520.0F,Color.BLUE));        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        g2.fillOval(20,20,280*2,280*2);
        g2.setPaint(Color.BLACK);
        g2.fillOval(75, 75, 225*2, 225*2);
        g2.setPaint(Color.CYAN);
        g2.fillOval(115, 115, 185*2, 185*2);
        g2.setPaint(Color.BLACK);
        g2.fillOval(290, 290, 20, 20);
        Font s14=new Font(" 宋体",Font.BOLD,20);
        g2.setFont(s14);
        g2.setPaint(Color.GREEN);
        for(int i=1;i<60;i++)
        {
            if(i%5==0)
            {
                g2.drawString(""+(i/5),x+(float)sin(PI*i/30)*r-5,y-(float)cos(PI*i/30)*r+5);
                continue;
            }
            Ellipse2D circle=new Ellipse2D.Double(x+(double)sin(PI*i/30)*r-4,y-(double)cos(PI*i/30)*r-4,8,8);
            g2.fill(circle);
        }
        g2.drawString("12",x-11,y-r+5);
        g2.setPaint(Color.BLACK);
        g2.fillRect(x-74,y-82,154,23);
        g2.setPaint(Color.WHITE);
        g2.drawString(Month1+" 月"+Date1+"日星期"+Week1,x-73,y-63);
        g2.setStroke(new BasicStroke(5.0F,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));        g2.setPaint(Color.YELLOW);
        if(Mtag<Mlines.size())
        {
            g2.draw(Mlines.get(Mtag));
        }
        else
        {
            g2.draw(Mlines.get(0));
        }
        if(Htag<Hlines.size())
        {
            g2.draw(Hlines.get(Htag));
        }
        else
        {
            g2.draw(Hlines.get(0));
        }
        if(Stag<Slines.size())
        {
            g2.draw(Slines.get(Stag++));
        }
        else
        {
            Stag=0;
            g2.draw(Slines.get(Stag++));
            if(Mtag<Mlines.size())
            {
                Mtag++;
            }
            else
            {
                Mtag=0;
                if(Htag>=Hlines.size())
                {
                    Htag=0;
                }
            }
            if(Mtag%12==0&&Htag<Hlines.size())g2.draw(Hlines.get(Htag++));   
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值