关注图老师设计创意栏目可以让大家能更好的了解电脑,知道有关于电脑的更多有趣教程,今天给大家分享一个简单的时钟教程,希望对大家能有一点小小的帮助。
【 tulaoshi.com - 编程语言 】
有诸多缺点,比如不是时间触发而是靠线程挂起
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)
package com.zhou.clock;(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)
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;i12;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;i12;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;
}
}
来源:http://www.tulaoshi.com/n/20160219/1609913.html