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

若要繪制一個(gè)時(shí)鐘,需要繪制一個(gè)圓并為秒鐘、分鐘和小時(shí)繪制三個(gè)指針。為了畫(huà)一個(gè)指針,需要確定一條直線(xiàn)的兩端:一端是時(shí)鐘的中央,位于(centerX,centerY);另外一端位于(endX,endY),由一下公式來(lái)確定:
endX=centerX+handLength×sin(θ)
endY=centerY-handLength×cos(θ)
(其中θ是指針和豎直方向12點(diǎn)的夾角)
因?yàn)橐环昼娪?0秒,所以第2個(gè)指針的角度是:second×(2π/60)
分鐘的位置由分鐘和秒鐘來(lái)決定。包含秒數(shù)的確切分鐘數(shù)是minu+second/60。例如,如時(shí)間是3分30秒,那么總的分鐘數(shù)就是3.5。由于一小時(shí)有60分鐘,因此分針的角度是: (minute+second/60)×(2π/12)
由于一個(gè)圓被分為12個(gè)小時(shí),所以時(shí)針的角度是: (hour+minute/60+second/(60×60))×(2π/12)
為了簡(jiǎn)化計(jì)算,在計(jì)算分針和時(shí)針角度的時(shí)候,可以忽略秒針,因?yàn)樗鼈償?shù)字太小,基本可以忽略。因此,秒針、分針以及時(shí)針的端點(diǎn)可以如下計(jì)算:
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類(lèi)的實(shí)現(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);
}
}
對(duì)程序的簡(jiǎn)要解讀:①Java API的GregorianCalendar類(lèi)可以使用它的無(wú)參構(gòu)造方法來(lái)商城一個(gè)具有當(dāng)前時(shí)間的Calendar實(shí)例??梢詮囊粋€(gè)Calendar對(duì)象,通過(guò)調(diào)用它的get(Calendar.HOUR)、get(Calendar.MINUTE)和get(Calendar.SECOND)方法來(lái)返回小時(shí)、分鐘以及秒鐘。②因?yàn)閜aintClock()方法在任何一個(gè)新的屬性(hour、minute、second、w以及h)被設(shè)置的時(shí)候調(diào)用,所以之前的內(nèi)容從面板中被清除。
然后就需要設(shè)計(jì)一個(gè)ClockAnimation類(lèi)來(lái)顯示時(shí)鐘的動(dòng)畫(huà)
Timeline類(lèi)可以用于通過(guò)使用一個(gè)或者更多的KeyFrame(關(guān)鍵幀)來(lái)編寫(xiě)任意動(dòng)畫(huà)。
你可以用Timeline來(lái)控制時(shí)鐘的重繪,代碼如下:
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())
);
}
}
程序簡(jiǎn)單解讀:①在時(shí)間線(xiàn)動(dòng)畫(huà)的每個(gè)關(guān)鍵幀中,這個(gè)處理器每秒被調(diào)用一次。所以動(dòng)畫(huà)中的時(shí)間每秒被更新一次。②最后兩個(gè)監(jiān)聽(tīng)器是用來(lái)修改時(shí)鐘的面板的大小的,將這個(gè)監(jiān)聽(tīng)器和窗體的寬度和高度屬性進(jìn)行注冊(cè),從而在場(chǎng)景的寬度和高度改變的情況下可以重新設(shè)置面板大小。代碼保證了時(shí)鐘面板的大小和場(chǎng)景大小是同步的。
最后運(yùn)行就能達(dá)到上面圖所示的效果了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JavaFX實(shí)現(xiàn)簡(jiǎn)易時(shí)鐘效果(二)
- JavaFX實(shí)現(xiàn)簡(jiǎn)易時(shí)鐘效果(一)
- javafx實(shí)現(xiàn)時(shí)鐘效果
- java實(shí)現(xiàn)時(shí)鐘效果
- Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
- Java實(shí)現(xiàn)動(dòng)態(tài)模擬時(shí)鐘
- Java實(shí)現(xiàn)的動(dòng)態(tài)數(shù)字時(shí)鐘功能示例【顯示世界時(shí)間】
- Java 畫(huà)時(shí)鐘遇到的問(wèn)題及解決方案
相關(guān)文章
Struts2學(xué)習(xí)筆記(6)-簡(jiǎn)單的數(shù)據(jù)校驗(yàn)
這篇文章主要介紹Struts2中的數(shù)據(jù)校驗(yàn),通過(guò)一個(gè)簡(jiǎn)單的例子來(lái)說(shuō)明,希望能給大家做一個(gè)參考。2016-06-06
Feign Client超時(shí)時(shí)間設(shè)置不生效的解決方法
這篇文章主要為大家詳細(xì)介紹了Feign Client 超時(shí)時(shí)間設(shè)置不生效的原因與解決方法,具有一定的的參考價(jià)值,希望對(duì)大家有一定的幫助2025-04-04
Maven中dependency和plugins的繼承與約束
這篇文章主要介紹了Maven中dependency和plugins的繼承與約束,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
Java java.sql.Timestamp時(shí)間戳案例詳解
這篇文章主要介紹了Java java.sql.Timestamp時(shí)間戳案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
springboot2如何禁用自帶tomcat的session功能
這篇文章主要介紹了springboot2如何禁用自帶tomcat的session功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11

