快速业务通道

Java实现的简单的时钟 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-20

Java实现的简单的时钟

时间:2011-01-22

有诸多缺点,比如不是时间触发而是靠线程挂起

package com.zhou.clock; import java.awt.*; import java.awt.geom.*; import javax.swing.*; import java.lang.Math; import java.util.Date; public class Clock extends JFrame { ClockPane cp; public Clock (){ super("clock"); setDefaultCloseOperation(EXIT_ON_CLOSE); cp=new ClockPane(); cp.initTime(); //Container content=getContentPane(); //content.add(cp); setContentPane(cp);//这句可用上面两句代替 setBounds(400,400,200,220); setResizable(false); setVisible(true); } public void interval(){ while(true){ try{Thread.sleep(1000);} catch(Exception e){ } cp.drawClock(cp.getGraphics()); } } public static void main(String[] args){ Clock clock=new Clock(); clock.interval(); } class ClockPane extends JPanel{ double secondRadian; double minuteRadian; double hourRadian; public void paintCompnent(Graphics comp){ drawClock(comp); } public void initTime(){ Date d=new Date(); int hours=d.getHours(); int minutes=d.getMinutes(); int seconds=d.getSeconds(); hourRadian=hours*Math.PI/6 + ((float)minutes/60)*Math.PI/6; minuteRadian=minutes*Math.PI/30; secondRadian=seconds*Math.PI/30; } public void drawClock(Graphics comp){ Graphics2D comp2D= (Graphics2D)comp; comp2D.setColor(Color.GRAY); BasicStroke pen=new BasicStroke(10F,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND); comp2D.setStroke(pen); Arc2D.Float circle=new Arc2D.Float(20F,20F,150F,150F,0F,360F,Arc2D.OPEN); comp2D.draw(circle); comp2D.setColor(Color.white); comp2D.fill(circle); pen=new BasicStroke(1F,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND); comp2D.setStroke(pen); comp2D.setColor(Color.CYAN); circle=new Arc2D.Float(20.5F,20.5F,149F,149F,0F,360F,Arc2D.OPEN); comp2D.draw(circle); comp2D.setColor(Color.black); pen=new BasicStroke(8F,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND); comp2D.setStroke(pen); Pin stake=new Pin(95.0,95.0,75.0); double stakeRadian=0; for(int i=0;i<12;i++){ comp2D.draw(stake.createStake(stakeRadian)); stakeRadian+=Math.PI/6; } pen=new BasicStroke(2F,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND); comp2D.setStroke(pen); comp2D.setColor(Color.CYAN); for(int i=0;i<12;i++){ comp2D.draw(stake.createStake(stakeRadian)); stakeRadian+=Math.PI/6; } comp2D.setColor(Color.DARK_GRAY); Pin secondPin =new Pin(95.0,95.0,65.0); Pin minutePin =new Pin(95.0,95.0,50.0); Pin hourPin =new Pin(95.0,95.0,40.0); pen =new BasicStroke(2F,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND); BasicStroke pen1=new BasicStroke(3F,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND); BasicStroke pen2=new BasicStroke(4F,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND); initTime(); comp2D.setStroke(pen); comp2D.draw(secondPin.createPin(secondRadian)); comp2D.setStroke(pen1); comp2D.draw(minutePin.createPin(minuteRadian)); comp2D.setStroke(pen2); comp2D.draw(hourPin.createPin(hourRadian)); } } } package com.zhou.clock; import java.awt.*; import java.awt.geom.*; import javax.swing.*; import java.lang.Math; public class Pin{ double constantX; dou

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号