/*
* 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) { } } 效果图如下: ![]() |
java写的时钟
最新推荐文章于 2024-07-27 17:56:15 发布