Clock.java

package clock;

import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Clock extends JFrame implements Runnable{
    public Clock()
    {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        try
        {
            jbInit();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    Thread thread1;//定义线程对象
    Color handColor;//定义表示时针、分针和秒针颜色的变量
    Color numberColor;//定义表示时钟数字颜色的变量

    JLabel jLabel1 = new JLabel();
    public void init()
    {
        int x,y;
        handColor = Color.blue;
        numberColor = Color.darkGray;
        setBackground(Color.white);
    }
    //paint is the main part of the program
    public void paint(Graphics g)
    {
        int xh,yh,xm,ym,xs,ys,s = 0,m = 10,h=0,xpoint,ypoint;
        int day,month,year,weekday;
        Calendar c1 = Calendar.getInstance();
        //或的当前时间
        s = c1.get(Calendar.SECOND);
        m = c1.get(Calendar.MINUTE);
        h = c1.get(Calendar.HOUR_OF_DAY);
        //获得当前日期
        day = c1.get(Calendar.DATE);
        month = c1.get(Calendar.MONTH) + 1;
        year = c1.get(Calendar.YEAR);
        weekday = c1.get(Calendar.DAY_OF_WEEK)-1;
        //在标签控件上显示日期和时间
        jLabel1.setText("    "+year+"年"+month+"月"+day+"日"+"    "+"星期"+weekday+"    "+h+":"+m+":"+s);
        xpoint = 130;
        ypoint = 100;
        //计算时针、分针和秒针的位置
        xs = (int)(Math.cos(s * 3.14f/30 -3.14f/2) * 45 + xpoint);
        ys = (int)(Math.sin(s * 3.14f/30 -3.14f/2) * 45 + ypoint);
        xm = (int)(Math.cos(m * 3.14f/30 -3.14f/2) * 40 + xpoint);
        ym = (int)(Math.sin(m * 3.14f/30 -3.14f/2) * 40 + ypoint);
        xh = (int)(Math.cos((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 +xpoint);
        yh = (int)(Math.sin((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 +ypoint);
        //绘制时钟背景
        g.setColor(handColor);
        g.clearRect(0,0,260,200);
        g.drawOval(xpoint/2+10,ypoint/2-5,110,110);
        g.setColor(numberColor);
        g.drawString("9",xpoint-45,ypoint+3);
        g.drawString("3",xpoint+40,ypoint+3);
        g.drawString("12",xpoint-5,ypoint-37);
        g.drawString("6",xpoint-3,ypoint+45);
        g.setColor(getBackground());

        g.setColor(numberColor);
        g.drawString("",5,125);
        //绘制时针、分针和秒针
        g.drawLine(xpoint,ypoint,xs,ys);
        g.setColor(handColor);
        g.drawLine(xpoint,ypoint,xm,ym);
        g.drawLine(xpoint,ypoint,xh,yh);
    }

    //定义线程的run方法
    public void run()
    {
        Thread me = Thread.currentThread();
        while(thread1 == me)
        {
            try
            {
                Thread.currentThread().sleep(100);
            }
            catch(InterruptedException e)
            {

            }
            repaint();
        }
    }
    //开始执行线程
    public void start()
    {
        thread1 = new Thread(this);
        thread1.start();
    }
    //停止线程
    public void stop()
    {
        thread1 = null;
    }
    public void update(Graphics g)
    {
        paint(g);
    }
    private void jbInit() throws Exception
    {
        this.getContentPane().add(jLabel1,BorderLayout.SOUTH);
    }
    protected void processWindowEvent(WindowEvent e)
    {
        super.processWindowEvent(e);
        if(e.getID() == WindowEvent.WINDOW_CLOSING)
        {
            System.exit(0);
        }

    }

    public static void main(String[] args) {
        Clock clock1 = new Clock();
        clock1.init();
        clock1.start();
        clock1.setSize(280,230);
        clock1.setResizable(false);
        clock1.setVisible(true);

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值