JavaFX實現(xiàn)簡易時鐘效果(一)
更新時間:2020年11月15日 11:20:41 作者:酸甜梅子
這篇文章主要為大家詳細介紹了JavaFX實現(xiàn)簡易時鐘效果的第一篇,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了JavaFX實現(xiàn)簡易時鐘效果的具體代碼,供大家參考,具體內(nèi)容如下
效果圖
用當前時間創(chuàng)建時鐘,繪制表盤。
鐘表是靜止的。讓指針動起來,請參照:繪制簡易時鐘(二)

主函數(shù)文件 ShowClock:
package primier;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.shape.Line;
public class ShowClock extends Application {
@Override //Override the start method in the Application class
public void start(Stage primaryStage) {
// 創(chuàng)建時鐘面板
ClockPane clock = new ClockPane();
// 當前時間整理為字符串
String timeString = clock.getHour() + ":" + clock.getMinute()
+ ":" + clock.getSecond();
Label lbCurrentTime = new Label(timeString);
BorderPane pane = new BorderPane();
pane.setCenter(clock);
pane.setBottom(lbCurrentTime);
// 將時鐘字符串設(shè)為靠上居中
BorderPane.setAlignment(lbCurrentTime, Pos.TOP_CENTER);
Scene scene = new Scene(pane, 250,250);
primaryStage.setTitle("Display Clock");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main (String[] args) {
Application.launch(args);
}
}
ClockPane 類
package primier;
import java.util.Calendar;
import java.util.GregorianCalendar;
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;
public class ClockPane extends Pane {
private int hour;
private int minute;
private int second;
// 時鐘面板的寬度和高度
private double w = 250, h = 250;
/** 用當前時間創(chuàng)建時鐘 */
public ClockPane() {
setCurrentTime();
}
/** Return hour */
public int getHour() { return hour; }
/** Return minute */
public int getMinute() { return minute; }
/** Return second */
public int getSecond() { return second; }
/** Set the current time for the clock */
public void setCurrentTime() {
// 用當前時間創(chuàng)建Calendar類
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.4; // 時鐘半徑
// 時鐘中心x, y坐標
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 secondX = centerX + sLength * Math.sin(second * (2 * Math.PI / 60));
double secondY = centerY - sLength * Math.cos(second * (2 * Math.PI / 60));
Line sLine = new Line(centerX, centerY, secondX, secondY);
sLine.setStroke(Color.GRAY);
// 分針
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.0) * (2 * Math.PI / 12));
Line hLine = new Line(centerX, centerY, hourX, hourY);
sLine.setStroke(Color.GREEN);
// 將之前的結(jié)點清空,繪制新創(chuàng)建的結(jié)點
getChildren().clear();
getChildren().addAll(circle, t1, t2, t3, t4, sLine, mLine, hLine);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringSecurity在分布式環(huán)境下的使用流程分析
文章介紹了Spring?Security在分布式環(huán)境下的使用,包括單點登錄(SSO)的概念、流程圖以及JWT(JSON?Web?Token)的生成和校驗,通過使用JWT和RSA非對稱加密,可以實現(xiàn)安全的分布式認證,感興趣的朋友一起看看吧2025-02-02
使用Spring AntPathMatcher的doMatch方法
這篇文章主要介紹了使用Spring AntPathMatcher的doMatch方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
Mybatis?selectKey 如何返回新增用戶的id值
這篇文章主要介紹了Mybatis?selectKey 如何返回新增用戶的id值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01

