⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clock.java

📁 一个时钟演示程序
💻 JAVA
字号:
import java.util.*;
import java.awt.*;
import java.awt.geom.*;
import java.text.*;
import javax.swing.*;
public class Clock
{
	public static void  main(String[] args)
	{
		ClockFrame clock=new ClockFrame();
        clock.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        clock.show();
	}
}
class ClockFrame extends JFrame
{
    public ClockFrame()
    {
		setTitle("ClockTest");
		setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
		ClockPanel panel=new ClockPanel();
		panel.start();
		Container contentPane=getContentPane();
		contentPane.add(panel);
	}
	private static final int DEFAULT_WIDTH=430;
	private static final int DEFAULT_HEIGHT=400;
}
    class ClockPanel extends JPanel implements Runnable
{

    public ClockPanel()
    {
        int x,y;
        formatter = new SimpleDateFormat ("EEE yyyy年 MMM dd日 HH:mm:ss",
                                          Locale.getDefault());
        currentDate = new Date();
        lastdate = formatter.format(currentDate);
        Font1 = new Font("Times Roman", Font.BOLD, 20);
        setBackground(Color.WHITE);
        resize(400,400);
    }
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        double xh, yh, xm, ym, xs, ys;
        int s = 0, m = 10, h = 10,S=10;
        String today;
        Graphics2D g2=(Graphics2D)g;
        currentDate = new Date();
        formatter.applyPattern("S");
        try {
            S = Integer.parseInt(formatter.format(currentDate));
        } catch (NumberFormatException n) {
            S = 0;
        }

        formatter.applyPattern("s");
        try {
            s = Integer.parseInt(formatter.format(currentDate));
        } catch (NumberFormatException n) {
            s = 0;
        }
        formatter.applyPattern("m");
        try {
            m = Integer.parseInt(formatter.format(currentDate));
        } catch (NumberFormatException n) {
            m = 10;
        }
        formatter.applyPattern("h");
        try {
            h = Integer.parseInt(formatter.format(currentDate));
        } catch (NumberFormatException n) {
            h = 10;
        }

        xs =Math.cos((s+S/1000.) * Math.PI / 30 - Math.PI / 2) * 70 + x;
        ys =Math.sin((s+S/1000.) * Math.PI / 30 - Math.PI / 2) * 70 + y;
        xm =Math.cos((m+s/60.) * Math.PI / 30 - Math.PI / 2) * 55 + x;
        ym =Math.sin((m+s/60.) * Math.PI / 30 - Math.PI / 2) * 55 + y;
        xh =Math.cos((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 46
                   + x;
        yh =Math.sin((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 46
                   + y;

        g2.setPaint(new Color(135,130,24));
        g2.setFont(Font1);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

	    g2.setComposite(AlphaComposite.getInstance(
                                       AlphaComposite.SRC_OVER, 0.6f));
		g2.fill(new Ellipse2D.Double(xcenter+50, ycenter-150, 200, 200));
        g2.setComposite(AlphaComposite.getInstance(
                                       AlphaComposite.SRC_OVER, 1.0f));
                        //时间
		g2.setPaint(new Color(105,181,247));
		g2.drawString("9", xcenter+55, ycenter-45);
	    g2.drawString("3", xcenter+235, ycenter-45);
		g2.drawString("12",xcenter+140, ycenter-130);
        g2.drawString("6", xcenter+145, ycenter+45);

        formatter.applyPattern("EEE yyyy MMM年 dd日 HH:mm:ss");
        today = formatter.format(currentDate);
        g2.setFont(Font1);

        g2.setPaint(new Color(218,112,49));
        g2.drawString(today, 50, 300);
        g2.setPaint(new Color(97,243,24));
        g2.draw(new Line2D.Double(x, y, xs, ys));//second
        g2.setPaint(new Color(17,22,71));
        g2.draw(new Line2D.Double(x, y-1, xm, ym));//minute
        g2.setPaint(new Color(214,10,243));
        g2.draw(new Line2D.Double(x, y-1, xh, yh));//hour
        lastdate = today;
        currentDate = null;
    }
    public void start() {
        timer = new Thread(this);
        timer.start();
    }
    public void stop()
    {
        timer = null;
    }
    public void run()
    {
        Thread me = Thread.currentThread();
        while (timer==me) {
            try {
                Thread.currentThread().sleep(50);
                 }
                catch (InterruptedException e)
            {
            }
            repaint();
        }
    }
        private volatile Thread timer;
	    private SimpleDateFormat formatter;
	    private String lastdate;
	    private Font Font1;
	    private Date currentDate;
	    private int xcenter = 50,  ycenter = 190;
        private int ppp=0;
        private int x=200, y=140;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -