当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:13:00  来源:本站整理

Java实现的简单的时钟[Java编程]

赞助商链接



  本文“Java实现的简单的时钟[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

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

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;
double constantY;
double length;
public Pin(double inputX, double inputY, double inputLength){
constantX=inputX;
constantY=inputY;
length =inputLength;
}
public Line2D.Double createPin(double radian){
double variableX;
double variableY;
variableX=constantX+length*Math.sin(radian);
variableY=constantY-length*Math.cos(radian);
Line2D.Double ln=new Line2D.Double(constantX,constantY,variableX,variableY);
return ln;
}
public Line2D.Double createStake(double radian){
double innerX;
double innerY;
double outerX;
double outerY;
innerX=constantX+(length-15)*Math.sin(radian);
innerY=constantY-(length-15)*Math.cos(radian);
outerX=constantX+length*Math.sin(radian);
outerY=constantY-length*Math.cos(radian);
Line2D.Double ln=new Line2D.Double(innerX,innerY,outerX,outerY);
return ln;
}
}


  以上是“Java实现的简单的时钟[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • 利用Javascript实现网页水印(非图片水印)
  • Java开辟环境的搭建
  • Ubuntu java安装与配置
  • 办理Ubuntu 10.04 Firefox3.6 Java浏览器插件不工作的问
  • Ubuntu重装后Java环境的设置
  • Sun Java进入Ubuntu 10.10软件中央
  • Ubuntu 10.10配置Java开辟环境
  • 在Ubuntu 10.10中配置Java环境变量的办法
  • Ubuntu下Java环境的搭建
  • Ubuntu 10.04 下安装 Java, JRE
  • Ubuntu 10.04下的搭建SUN JAVA开辟环境
  • Ubuntu 12.04安装java7
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .