程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 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;
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved