JavaFX實現(xiàn)簡易時鐘效果
本文實例為大家分享了JavaFX實現(xiàn)簡易時鐘效果的具體代碼,供大家參考,具體內(nèi)容如下
首先要在面板中顯示一個時鐘,我們可以設(shè)計一個ClockPane類來顯示一個時鐘。
最終效果:

若要繪制一個時鐘,需要繪制一個圓并為秒鐘、分鐘和小時繪制三個指針。為了畫一個指針,需要確定一條直線的兩端:一端是時鐘的中央,位于(centerX,centerY);另外一端位于(endX,endY),由一下公式來確定:
endX=centerX+handLength×sin(θ)
endY=centerY-handLength×cos(θ)
(其中θ是指針和豎直方向12點的夾角)
因為一分鐘有60秒,所以第2個指針的角度是:second×(2π/60)
分鐘的位置由分鐘和秒鐘來決定。包含秒數(shù)的確切分鐘數(shù)是minu+second/60。例如,如時間是3分30秒,那么總的分鐘數(shù)就是3.5。由于一小時有60分鐘,因此分針的角度是: (minute+second/60)×(2π/12)
由于一個圓被分為12個小時,所以時針的角度是: (hour+minute/60+second/(60×60))×(2π/12)
為了簡化計算,在計算分針和時針角度的時候,可以忽略秒針,因為它們數(shù)字太小,基本可以忽略。因此,秒針、分針以及時針的端點可以如下計算:
secondX = centerX + secondHandLength × sin(second × (2π/60))
secondY = centerY - secondHandLength × cos(second × (2π/60))
minuteX = centerX + minuteHandLength × sin(minute × (2π/60))
minuteY = centerY - minuteHandLength × cos(minute × (2π/60))
hourX = centerX + hourHandLength × sin((hour+minute/60) × (2π/12))
hourX = centerX + hourHandLength × sin((hour+minute/60) × (2π/12))
這樣就得到了ClockPane類的實現(xiàn)程序:
package com.company;
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 java.util.Calendar;
import java.util.GregorianCalendar;
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) {
this.hour=hour;
this.minute=minute;
this.second=second;
paintClock();
}
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 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();
}
protected 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);
Text t1=new Text(centerX-5,centerY-clockRadius+12,"12");
Text t2=new Text(centerX-clockRadius+3,centerY+5,"9");
Text t3=new Text(centerX+clockRadius-10,centerY+3,"3");
Text t4=new Text(centerX-3,centerY+clockRadius-3,"6");
double sLength=clockRadius*0.8;
double scondX=centerX+sLength*Math.sin(second*(2*Math.PI/60));
double scondY=centerY-sLength*Math.cos(second*(2*Math.PI/60));
Line sline=new Line(centerX,centerY,scondX,scondY);
sline.setStroke(Color.RED);
double mLength=clockRadius*0.65;
double minuteX=centerX+mLength*Math.sin(minute*(2*Math.PI/60));
double minuteY=centerY-mLength*Math.cos(minute*(2*Math.PI)/60);
Line mline=new Line(centerX,centerY,minuteX,minuteY);
mline.setStroke(Color.BLUE);
double hLength=clockRadius*0.5;
double hourX=centerX+hLength*Math.sin((hour%12+minute/60.0)*(2*Math.PI/12));
double hourY=centerY-hLength*Math.cos((hour%12+minute/60)*(2*Math.PI/12));
Line hline=new Line(centerX,centerY,hourX,hourY);
hline.setStroke(Color.GREEN);
getChildren().clear();
getChildren().addAll(circle,t1,t2,t3,t4,sline,mline,hline);
}
}
對程序的簡要解讀:①Java API的GregorianCalendar類可以使用它的無參構(gòu)造方法來商城一個具有當(dāng)前時間的Calendar實例。可以從一個Calendar對象,通過調(diào)用它的get(Calendar.HOUR)、get(Calendar.MINUTE)和get(Calendar.SECOND)方法來返回小時、分鐘以及秒鐘。②因為paintClock()方法在任何一個新的屬性(hour、minute、second、w以及h)被設(shè)置的時候調(diào)用,所以之前的內(nèi)容從面板中被清除。
然后就需要設(shè)計一個ClockAnimation類來顯示時鐘的動畫
Timeline類可以用于通過使用一個或者更多的KeyFrame(關(guān)鍵幀)來編寫任意動畫。
你可以用Timeline來控制時鐘的重繪,代碼如下:
package com.company;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.animation.KeyFrame;
import javafx.scene.control.Label;
public class ClockAnimation extends Application {
@Override
public void start(Stage primaryStage) {
ClockPane clock=new ClockPane();
BorderPane borderPane=new BorderPane();
EventHandler<ActionEvent> eventHandler=e -> {
clock.setCurrentTime();
String timeString=clock.getHour()+":"+clock.getMinute()+":"+clock.getSecond();
Label lblCurrentTime=new Label(timeString);
borderPane.setCenter(clock);
borderPane.setBottom(lblCurrentTime);
BorderPane.setAlignment(lblCurrentTime, Pos.TOP_CENTER);
};
Timeline animation=new Timeline(new KeyFrame(Duration.millis(1000),eventHandler));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
Scene scene=new Scene(borderPane,250,250);
primaryStage.setTitle("ClockAnimation");
primaryStage.setScene(scene);
primaryStage.show();
borderPane.widthProperty().addListener(ov ->
clock.setW(borderPane.getWidth())
);
borderPane.heightProperty().addListener(ov ->
clock.setH(borderPane.getHeight())
);
}
}
程序簡單解讀:①在時間線動畫的每個關(guān)鍵幀中,這個處理器每秒被調(diào)用一次。所以動畫中的時間每秒被更新一次。②最后兩個監(jiān)聽器是用來修改時鐘的面板的大小的,將這個監(jiān)聽器和窗體的寬度和高度屬性進(jìn)行注冊,從而在場景的寬度和高度改變的情況下可以重新設(shè)置面板大小。代碼保證了時鐘面板的大小和場景大小是同步的。
最后運(yùn)行就能達(dá)到上面圖所示的效果了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Struts2學(xué)習(xí)筆記(6)-簡單的數(shù)據(jù)校驗
這篇文章主要介紹Struts2中的數(shù)據(jù)校驗,通過一個簡單的例子來說明,希望能給大家做一個參考。2016-06-06
Feign Client超時時間設(shè)置不生效的解決方法
這篇文章主要為大家詳細(xì)介紹了Feign Client 超時時間設(shè)置不生效的原因與解決方法,具有一定的的參考價值,希望對大家有一定的幫助2025-04-04
Maven中dependency和plugins的繼承與約束
這篇文章主要介紹了Maven中dependency和plugins的繼承與約束,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
Java java.sql.Timestamp時間戳案例詳解
這篇文章主要介紹了Java java.sql.Timestamp時間戳案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
springboot2如何禁用自帶tomcat的session功能
這篇文章主要介紹了springboot2如何禁用自帶tomcat的session功能,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11

