java写的时钟

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package CanvasDrawTest;
import java.awt.Color;
import java.awt.Point;
import javax.swing.*;
/**
*
* @author shuijian00
* @CanvasTestMain.java
*/
public class CanvasTestMain{
    public static void main(String[] args) {
        int width = 300,height = 300;
        JFrame frame = new JFrame("Clock");
        frame.setSize(width, height);
        CanvasDrawShape drawShape = new CanvasDrawShape(new Point(70,50),width/2,height/2);
        frame.add(drawShape);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package CanvasDrawTest;
//import java.sql.Timestamp;
import java.util.*;
/**
*
* @author LJ
* @Clock.java
*/
public class Clock {
    static int hour,minute,second;
    GregorianCalendar gc;
    public Clock() {
        gc = new GregorianCalendar();
        gc.setTime(new Date()); //获取当前时间并存入gc中
        hour = gc.get(Calendar.HOUR);
        minute = gc.get(Calendar.MINUTE);
        second = gc.get(Calendar.SECOND);
    }
    public void Update() {
        if(++second >= 60) {
            second = 0;
            if(++minute >= 60) {
                minute = 0;
                if(++hour>=24) {
                    hour = 0;
                }
            }
        }
    }
}



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package CanvasDrawTest;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.ImageIcon;
/**
*
* @author shuijian00
* @CanvasDrawShape.java
*/
public class CanvasDrawShape extends Canvas implements MouseListener,MouseMotionListener,Runnable{
    Point center;
    Image clockImg = null;
    URL urlClk = null;
    File fsClk = null;
    int rectW,rectH;
    boolean isDragged = false;
    Point secondEndPoint = new Point(145,70),minuteEndPoint = new Point(145,85),hourEndPoint = new Point(145,95);
    Thread thread = new Thread(this);
    Clock clock =new Clock();
    final static double ROTATE_DEGREE = 6.0/180*Math.PI;
    final static double HOUR_ROTATE_DEGREE = 30.0 / 180 * Math.PI;
    public CanvasDrawShape() {
        this.addMouseMotionListener(this);
        this.addMouseListener(this);
    }
    public CanvasDrawShape(Point point) {
        center = point;
        this.addMouseMotionListener(this);
        this.addMouseListener(this);
    }
    public CanvasDrawShape(Point point, int width, int height) {
        center = point;
        rectW = width;
        rectH = height;
        fsClk = new File("images/clock.jpg");
        try {
            urlClk = new URL("file:"+fsClk.getAbsolutePath()); //获取文件的绝对路径
        }catch(MalformedURLException ex) {
            ex.printStackTrace();
        }
        clockImg = (new ImageIcon(urlClk)).getImage();
        this.setBackground(Color.white);
        thread.start();
        rotate(Clock.hour*HOUR_ROTATE_DEGREE+Clock.minute*HOUR_ROTATE_DEGREE/60,hourEndPoint); //设置时分秒针初始位置
        rotate(Clock.minute*ROTATE_DEGREE,minuteEndPoint);
        rotate(Clock.second*ROTATE_DEGREE,secondEndPoint);
        this.addMouseMotionListener(this);
        this.addMouseListener(this);
    }
    public void run() {
        try{
            while(true){
                rotate(ROTATE_DEGREE,secondEndPoint);
                if(Clock.second == 0) { //秒钟到达12点位置,分钟旋转
                    rotate(ROTATE_DEGREE,minuteEndPoint);
                }
                if(((Clock.minute % 12) == 0) && Clock.minute != 0) { //分钟每走12分钟,时钟走一小格(6度)
                    rotate(ROTATE_DEGREE,hourEndPoint);
                }
                repaint();
                clock.Update();
                Thread.sleep(1000);
            }
        }catch(InterruptedException e) {
            e.printStackTrace();
        }
    }
    public void setCenter(Point point) {
        center = point;
    }
    @Override
    public void setSize(int width,int height) {
        rectW = width;
        rectH = height;
    }
    private synchronized Point rotate(double degree,Point endPoint) {
        int xTemp,yTemp;
        //for(int i = 0;i<polyX.length;i++) {
            xTemp = endPoint.x;
            yTemp = endPoint.y;
            endPoint.x = (int)((float)xTemp*Math.cos(degree) - (float)yTemp*Math.sin(degree) +
                    145*(1-Math.cos(degree))+125*Math.sin(degree) + 0.5);
            endPoint.y = (int)((float)xTemp*Math.sin(degree) + (float)yTemp*Math.cos(degree) +
                    125*(1-Math.cos(degree)) - 145*Math.sin(degree) + 0.5);
            return endPoint;
        //}
    }
    @Override
    public void paint(Graphics g) {
        g.setColor(Color.blue);
        g.drawImage(clockImg, 71, 60, 153, 139, Color.blue,this);
        //g.drawOval(center.x, center.y, rectW, rectH);
        g.drawString(Clock.hour + ":" + Clock.minute + ":" + Clock.second, 130, 220);
        //g.drawPolygon(polyX, polyY, 4);
        g.drawLine(center.x+rectW/2, center.y+rectH/2, secondEndPoint.x, secondEndPoint.y);
        g.setColor(Color.black);
        g.drawLine(center.x+rectW/2, center.y+rectH/2, minuteEndPoint.x, minuteEndPoint.y);
        g.setColor(Color.red);
        g.drawLine(center.x+rectW/2, center.y+rectH/2, hourEndPoint.x, hourEndPoint.y);
    }
    //鼠标时间监听没有用,留作以后做交互时用
    public void mouseClicked(MouseEvent e) {
        
    }
    public void mouseEntered(MouseEvent e) {
        
    }
    public void mousePressed(MouseEvent e) {
        
    }
    
    public void mouseExited(MouseEvent e) {
        
    }
    public void mouseReleased(MouseEvent e) {
        
    }
    public void mouseMoved(MouseEvent e) {
        
    }
    public void mouseDragged(MouseEvent e) {
    }
}


效果图如下:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

岛上码农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值