JavaFX實(shí)現(xiàn)簡(jiǎn)易時(shí)鐘效果(一)
本文實(shí)例為大家分享了JavaFX實(shí)現(xiàn)簡(jiǎn)易時(shí)鐘效果的具體代碼,供大家參考,具體內(nèi)容如下
效果圖
用當(dāng)前時(shí)間創(chuàng)建時(shí)鐘,繪制表盤(pán)。
鐘表是靜止的。讓指針動(dòng)起來(lái),請(qǐng)參照:繪制簡(jiǎn)易時(shí)鐘(二)
主函數(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)建時(shí)鐘面板 ClockPane clock = new ClockPane(); // 當(dāng)前時(shí)間整理為字符串 String timeString = clock.getHour() + ":" + clock.getMinute() + ":" + clock.getSecond(); Label lbCurrentTime = new Label(timeString); BorderPane pane = new BorderPane(); pane.setCenter(clock); pane.setBottom(lbCurrentTime); // 將時(shí)鐘字符串設(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 類(lèi)
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; // 時(shí)鐘面板的寬度和高度 private double w = 250, h = 250; /** 用當(dāng)前時(shí)間創(chuàng)建時(shí)鐘 */ 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() { // 用當(dāng)前時(shí)間創(chuàng)建Calendar類(lèi) 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(); } /** 繪制時(shí)鐘 */ protected void paintClock() { double clockRadius = Math.min(w,h)*0.4; // 時(shí)鐘半徑 // 時(shí)鐘中心x, y坐標(biāo) double centerX = w/2; double centerY = h/2; // 繪制鐘表 Circle circle = new Circle(centerX, centerY, clockRadius); circle.setFill(Color.WHITE); // 填充顏色 circle.setStroke(Color.BLACK); // 筆畫(huà)顏色 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); // 時(shí)針 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é)點(diǎn)清空,繪制新創(chuàng)建的結(jié)點(diǎn) getChildren().clear(); getChildren().addAll(circle, t1, t2, t3, t4, sLine, mLine, hLine); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- java實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘并設(shè)置鬧鐘功能
- Java實(shí)現(xiàn)的簡(jiǎn)單數(shù)字時(shí)鐘功能示例
- java多線程編程制作電子時(shí)鐘
- java實(shí)現(xiàn)的小時(shí)鐘示例分享
- Java編程小實(shí)例—數(shù)字時(shí)鐘的實(shí)現(xiàn)代碼示例
- Java實(shí)現(xiàn)的動(dòng)態(tài)數(shù)字時(shí)鐘功能示例【顯示世界時(shí)間】
- java實(shí)現(xiàn)時(shí)鐘效果
- Java實(shí)現(xiàn)動(dòng)態(tài)模擬時(shí)鐘
- Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
- java實(shí)現(xiàn)時(shí)鐘表盤(pán)
相關(guān)文章
SpringBoot項(xiàng)目中@Test不出現(xiàn)可點(diǎn)擊運(yùn)行的按鈕問(wèn)題
這篇文章主要介紹了SpringBoot項(xiàng)目中@Test不出現(xiàn)可點(diǎn)擊運(yùn)行的按鈕問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Java讀取Properties文件的七種方法的總結(jié)
這篇文章主要介紹了Java讀取Properties文件的七種方法的總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-07-07springboot實(shí)現(xiàn)修改請(qǐng)求狀態(tài)404改為200
這篇文章主要介紹了springboot實(shí)現(xiàn)修改請(qǐng)求狀態(tài)404改為200方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07java 使用簡(jiǎn)單的demo實(shí)例告訴你優(yōu)化算法的強(qiáng)大
本篇文章介紹了,在java中使用簡(jiǎn)單的demo實(shí)例告訴你優(yōu)化算法的強(qiáng)大。需要的朋友參考下2013-05-05關(guān)于SpringBoot集成Lettuce連接Redis的方法和案例
這篇文章主要介紹了關(guān)于SpringBoot集成Lettuce連接Redis的方法和案例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04SpringBoot如何配置Controller實(shí)現(xiàn)Web請(qǐng)求處理
這篇文章主要介紹了SpringBoot如何配置Controller實(shí)現(xiàn)Web請(qǐng)求處理,文中通過(guò)圖解示例介紹的很詳細(xì),具有有一定的參考價(jià)值,需要的小伙伴可以參考一下2023-05-05Java使用Math.random()結(jié)合蒙特卡洛方法計(jì)算pi值示例
這篇文章主要介紹了Java使用Math.random()結(jié)合蒙特卡洛方法計(jì)算pi值的方法,簡(jiǎn)單說(shuō)明了結(jié)合具體實(shí)例蒙特卡洛方法的原理,并結(jié)合具體實(shí)例形式分析了java使用蒙特卡洛方法計(jì)算PI值的操作技巧,需要的朋友可以參考下2017-09-09簡(jiǎn)單理解java泛型的本質(zhì)(非類(lèi)型擦除)
泛型在java中有很重要的地位,在面向?qū)ο缶幊碳案鞣N設(shè)計(jì)模式中有非常廣泛的應(yīng)用。泛型是參數(shù)化類(lèi)型的應(yīng)用,操作的數(shù)據(jù)類(lèi)型不限定于特定類(lèi)型,可以根據(jù)實(shí)際需要設(shè)置不同的數(shù)據(jù)類(lèi)型,以實(shí)現(xiàn)代碼復(fù)用。下面小編來(lái)簡(jiǎn)單講一講泛型2019-05-05SpringCloud集成Sleuth和Zipkin的思路講解
Zipkin 是 Twitter 的一個(gè)開(kāi)源項(xiàng)目,它基于 Google Dapper 實(shí)現(xiàn),它致力于收集服務(wù)的定時(shí)數(shù)據(jù),以及解決微服務(wù)架構(gòu)中的延遲問(wèn)題,包括數(shù)據(jù)的收集、存儲(chǔ)、查找和展現(xiàn),這篇文章主要介紹了SpringCloud集成Sleuth和Zipkin,需要的朋友可以參考下2022-11-11