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编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |