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

📄 paintclock.java

📁 用Java实现时钟
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package clock;import java.awt.Canvas;import java.awt.Color;import java.awt.Graphics;import java.text.SimpleDateFormat;import java.util.Date;/** * * @author Administrator */public class PaintClock extends Canvas {    double 时x = 100.0;    double 时y = 100.0;    double 分x = 100.0;    double 分y = 100.0;    double 秒x = 100.0;    double 秒y = 100.0;    final double PI = 3.1415926;    PaintClock() {        new Thread(new Runnable() {            public void run() {                while (true) {                    try {                        nowtime();                        repaint();                        Thread.sleep(1000);                    } catch (InterruptedException e) {                        System.out.println("线程错误");                    }                }            }        }).start();    }    public void nowtime() {        Date time = new Date();        SimpleDateFormat h = new SimpleDateFormat("hh");        int 时 = Integer.parseInt(h.format(time));        SimpleDateFormat m = new SimpleDateFormat("mm");        int 分 = Integer.parseInt(m.format(time));        SimpleDateFormat s = new SimpleDateFormat("ss");        int 秒 = Integer.parseInt(s.format(time));        this.秒x = Math.cos(秒 * PI / 30 - PI / 2) * 50 + 100; //三角函数计算坐标        this.秒y = Math.sin(秒 * PI / 30 - PI / 2) * 50 + 100; //一秒一动        this.分x = Math.cos(分 * PI / 30 - PI / 2) * 45 + 100;        this.分y = Math.sin(分 * PI / 30 - PI / 2) * 45 + 100; //一分一动        this.时x = Math.cos((时 * 60 + 分) * PI / 360 - PI / 2) * 40 + 100; //一分一动        this.时y = Math.sin((时 * 60 + 分) * PI / 360 - PI / 2) * 40 + 100;    }    @Override    public void paint(Graphics g) {        //线        g.drawLine(100, 100, (int) 时x, (int) 时y);        g.setColor(Color.GREEN);        g.drawLine(100, 100, (int) 分x, (int) 分y);        g.setColor(Color.RED);        g.drawLine(100, 100, (int) 秒x, (int) 秒y);        //圆        g.setColor(Color.RED);        g.drawOval(50, 50, 100, 100);        g.setColor(Color.blue);        g.drawOval(60, 60, 80, 80);        //字        g.setColor(Color.black);        g.drawString("12", 94, 60);        //g.drawString("2", 135, 85);        g.drawString("3", 142, 104);        g.drawString("6", 94, 150);        g.drawString("9", 52, 104);       // validate();    }}

⌨️ 快捷键说明

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