利用javafx編寫一個時鐘製做程序

1.首先建立一個時鐘類,用於編寫時鐘的各類特有屬性java

package javaclock;多線程

/**
*
* @author admin
*/
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;app

import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.scene.control.Button;
public class ClockPane extends Pane{
private int hour;
private int minute;
private int second;
private double w = 250,h = 250;
//無參構造函數
public ClockPane() {
setCurrentTime();//調用方法
}
//有參構造函數
public ClockPane(int hour,int minute,int second) {
// Button btn = new Button("調整時間");
//btn.setCurrentTime1();
this.hour = hour;
this.minute = minute;
this.second = second;
paintClock();//調用方法
}

//set與get方法

public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
paintClock();
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = minute;
paintClock();

}
public int getSecond() {
return second;
}
public void setSecond(int second) {
this.second = second;
paintClock();
}
public double getW() {
return w;
}
public void setW(double w) {
this.w = w;
paintClock();
}
public double getH() {
return h;
}
public void setH(double h) {
this.h = h;
paintClock();
}
//修改當前時間
public void setCurrentTime1(int hour,int minute,int second){
this.hour = hour;
this.minute = minute;
this.second = second;
paintClock();
}
//方法setCurrentTime()設置獲取當前時間
public void setCurrentTime() {
Calendar calendar = new GregorianCalendar();//多態
this.hour = calendar.get(Calendar.HOUR_OF_DAY);//獲取小時
this.minute = calendar.get(Calendar.MINUTE);//獲取分鐘
this.second = calendar.get(Calendar.SECOND);//獲取秒
paintClock();
}

//建立paintClock()顯示時間
public void paintClock() {
double clockRadius = Math.min(w, h)*0.8*0.5;//時鐘半徑
double centerX = w/2;//圓心座標
double centerY = h/2;

//建立時鐘圓形
Circle circle = new Circle(centerX,centerY,clockRadius);//建立圓
circle.setFill(Color.WHITE);//圓形背景色爲白色
circle.setStroke(Color.BLACK);//圓形邊緣色爲黑色

//建立四個文本對象用於將12,9,6,3填入圓形中,注意不要壓在圓形上面,須要計算座標
Text text1 = new Text(centerX-5,centerY-clockRadius+12,"12");
Text text2 = new Text(centerX-clockRadius+3,centerY+5,"9");
Text text3 = new Text(centerX+clockRadius-10,centerY+3,"3");
Text text4 = new Text(centerX-3,centerY+clockRadius-3,"6");

//分別繪製時針,分針,秒針
//秒針
double secLength = clockRadius*0.8;//秒針長度
double secondX = centerX + secLength*Math.sin(second*(2*Math.PI/60));//由於一分鐘有60秒,故求出當前秒針的角度,利用三角函數公式便可計算出秒針的端點x座標
double secondY = centerY - secLength*Math.cos(second*(2*Math.PI/60));//同理求出秒針的端點Y座標
Line secline = new Line(centerX,centerY,secondX,secondY);//建立線段對象實現秒針
secline.setStroke(Color.RED);//將秒針定義爲紅色
//分針
double minLength = clockRadius*0.65;//分針長度
double minuteX = centerX + minLength*Math.sin((minute )*(2*Math.PI/60));//由於一小時有六十分鐘,利用分針的時間加上秒針的時間,利用三角函數對應的角度,便可計算出分針的端點x座標
double minuteY = centerY - minLength*Math.cos((minute )*(2*Math.PI/60));//同理求出分針的端點Y座標
Line minline = new Line(centerX,centerY,minuteX,minuteY);//建立線段對象實現秒針
minline.setStroke(Color.GREEN);//將秒針定義爲綠色
//時針
double houLength = clockRadius*0.5;//時針長度
double hourX = centerX + houLength*Math.sin((hour%12 + minute/60.0 )*(2*Math.PI/12));//由於一天有十二小時,一小時有六十分鐘,利用小時的時間加上分針的時間再加上秒針的時間,利用三角函數對應的角度,便可計算出分針的端點x座標
double hourY = centerY - houLength*Math.cos((hour%12 + minute/60.0 )*(2*Math.PI/12));//同理求出時針的端點Y座標
Line houline = new Line(centerX,centerY,hourX,hourY);//建立線段對象實現時針
houline.setStroke(Color.BLUE);//將秒針定義爲藍色

getChildren().clear();//每調用執行一次paintClock()方法就會清空面板
getChildren().addAll(circle,text1,text2,text3,text4,secline,minline,houline);//將幾個控件添加到面板中
}

}ide

二、而後編寫一個測試類,用於經過多線程建立呈現時鐘的動畫效果函數

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaclock;
import java.util.*;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.layout.BorderPane;
import javafx.animation.Timeline;
import javafx.scene.control.Label;
import javafx.animation.KeyFrame;
import javafx.geometry.Pos;
import javafx.util.Duration;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
/**
*
* @author admin
*/
public class JavaClock extends Application {
//JavaClock jc = new JavaClock();
ClockPane clock = new ClockPane();//
BorderPane borderPane = new BorderPane();//
@Override
public void start(Stage primaryStage) throws Exception {
// TODO 自動生成的方法存根

//綁定事件源
EventHandler<ActionEvent> eventHandler = e ->{
clock.setCurrentTime();

String timeString = clock.getHour()+":"+clock.getMinute()+":"+clock.getSecond();
Label labelCurrentTime = new Label(timeString);
borderPane.setCenter(clock);
borderPane.setBottom(labelCurrentTime);
BorderPane.setAlignment(labelCurrentTime, Pos.TOP_CENTER);
};


Timeline animation = new Timeline(new KeyFrame(Duration.millis(1000),eventHandler));//設定時鐘動畫每1秒變一次,關鍵幀時間間隔
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();


//Scene
Scene scene = new Scene(borderPane,250,250);
primaryStage.setTitle("JavaClock");
primaryStage.setScene(scene);
primaryStage.show();//展現場景

borderPane.widthProperty().addListener(o->
clock.setW(borderPane.getWidth()));//保持時間面板與場景同步
borderPane.heightProperty().addListener(o->
clock.setH(borderPane.getHeight()));
//設置一個按鈕,用於調整時間

}測試


/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

動畫



}this

相關文章
相關標籤/搜索