javafx實(shí)現(xiàn)時(shí)鐘效果
本文實(shí)例為大家分享了javafx實(shí)現(xiàn)時(shí)鐘效果的具體代碼,供大家參考,具體內(nèi)容如下
核心為三個(gè)函數(shù):
第一個(gè)為 public void dials,繪制表盤(pán)

第二個(gè)為 public void scale,繪制刻度,這里需要注意的是字體旋轉(zhuǎn)

第三個(gè)為 public void point,繪制秒分時(shí)針以及打印時(shí)間,需要注意的是進(jìn)制問(wèn)題
總的源碼如下:
package com.wu.demo;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Duration;
public class view extends Application{
@Override
public void start(Stage stage) throws Exception{
AnchorPane root = new AnchorPane();
Canvas canvas = new Canvas(800,650);
root.getChildren().add(canvas);
Scene scene = new Scene(root,800,650);
stage.setScene(scene);
stage.setResizable(false);
stage.show();
// 獲取畫(huà)板對(duì)象
GraphicsContext gc = canvas.getGraphicsContext2D();
// 創(chuàng)建時(shí)間軸
Timeline timeLine = new Timeline();
// 獲取時(shí)間軸的幀列表
ObservableList<KeyFrame> keyFrames = timeLine.getKeyFrames();
// 添加關(guān)鍵幀
keyFrames.add(new KeyFrame(Duration.seconds(0.1),e->{
// 刷新操作
gc.clearRect(0,0,800,650);
// 繪制表盤(pán)
dials(gc);
// 繪制刻度
scale(gc);
// 繪制指針
point(gc);
}));
// 設(shè)置時(shí)間軸播放次數(shù)為無(wú)限
timeLine.setCycleCount(-1);
// 播放時(shí)間軸
timeLine.play();
}
/**
* 繪制表盤(pán)
* @param gc
*/
public void dials(GraphicsContext gc) {
// 保存現(xiàn)場(chǎng)
gc.save();
// 變換坐標(biāo)到外切圓矩形左上角坐標(biāo)
gc.translate(100,25);
gc.setLineWidth(8);
gc.setStroke(Color.GRAY);
gc.strokeOval(0, 0, 600, 600);
gc.restore();
}
/**
* 繪制刻度
* @param gc
*/
public void scale(GraphicsContext gc) {
// 保存現(xiàn)場(chǎng)
gc.save();
// 變換坐標(biāo)系原點(diǎn)到表盤(pán)中心
gc.translate(400,325);
// 坐標(biāo)逆時(shí)針旋轉(zhuǎn)角度-90
gc.rotate(-90);
// 設(shè)置字體大小
gc.setFont(Font.font(20));
for(int i = 1 ; i < 61 ; i++) {
// 每一個(gè)刻度角度為6度
gc.rotate(6);
if(i % 5 == 0) {
gc.save();
// 當(dāng)前坐標(biāo)切換到 (250,0) 即刻度左邊界位置
gc.translate(250,0);
// 設(shè)置表格數(shù)字位置 相對(duì)于桌面應(yīng)該是豎直
gc.rotate(90-i/5*30);
gc.fillText(i/5+"",0,0);
gc.restore();
gc.fillRect(275,0,22,10);
}
else{
gc.fillRect(285,0,12,5);
}
}
// 恢復(fù)現(xiàn)場(chǎng)
gc.restore();
}
/**
* 繪制指針
* @param gc
*/
public void point(GraphicsContext gc) {
LocalDateTime time = LocalDateTime.now();
int seconds = time.getSecond();
int minutes = time.getMinute();
int hours = time.getHour();
double[] pointX1 = new double[]{0,50,270,50};
double[] pointY1 = new double[]{0,5,0,-5};
double[] pointX2 = new double[]{0,30,210,30};
double[] pointY2 = new double[]{0,10,0,-10};
double[] pointX3 = new double[]{0,20,150,20};
double[] pointY3 = new double[]{0,12,0,-12};
gc.save();
// 坐標(biāo)移動(dòng)至圓心
gc.translate(400, 325);
// 時(shí)間數(shù)字
{
String timeText1 = time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
gc.setFill(Paint.valueOf("#c0c0c0"));
gc.setFont(Font.font(20));
gc.fillText(timeText1,-40,-200);
String timeText2 = time.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
gc.setFill(Paint.valueOf("#c0c0c0"));
gc.setFont(Font.font(115));
gc.fillText(timeText2,-220,30);
}
// 秒鐘
{
gc.save();
gc.rotate(-90);
gc.setFill(Color.RED);
gc.rotate(seconds*6);
// 四邊形秒鐘
gc.fillPolygon(pointX1,pointY1, 4);
gc.restore();
}
// 分鐘
{
gc.save();
gc.rotate(-90);
gc.setFill(Color.BLUE);
gc.rotate(minutes*6+0.1*seconds);
// 四邊形分鐘
gc.fillPolygon(pointX2,pointY2, 4);
gc.restore();
}
// 時(shí)鐘
{
gc.save();
gc.rotate(-90);
gc.setFill(Color.BLACK);
gc.rotate(hours*30+minutes*0.5+seconds*(0.5/60));
// 四邊形時(shí)鐘
gc.fillPolygon(pointX3,pointY3, 4);
gc.restore();
}
// 恢復(fù)現(xiàn)場(chǎng)
gc.restore();
}
public static void main(String[] args) {
launch(args);
}
}
效果圖:


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JavaFX實(shí)現(xiàn)簡(jiǎn)易時(shí)鐘效果(二)
- JavaFX實(shí)現(xiàn)簡(jiǎn)易時(shí)鐘效果(一)
- JavaFX實(shí)現(xiàn)簡(jiǎ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)文章
SpringCloud Nacos配置中心管理超詳細(xì)講解
這篇文章主要介紹了Springcloud中的Nacos服務(wù)配置,本文以用戶微服務(wù)為例,進(jìn)行統(tǒng)一的配置,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
如何使用Mockito調(diào)用靜態(tài)方法和void方法
這篇文章主要介紹了如何使用Mockito調(diào)用靜態(tài)方法和void方法的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Spring 事件監(jiān)聽(tīng)機(jī)制實(shí)現(xiàn)跨模塊調(diào)用的思路詳解
之前一個(gè)項(xiàng)目,有兩個(gè)模塊,A 模塊需要依賴 B 模塊,但現(xiàn)在 B 模塊有地方需要調(diào)用 A 模塊的方法,如果直接依賴,又會(huì)產(chǎn)生循環(huán)依賴問(wèn)題,最終選擇使用 spring 的事件監(jiān)聽(tīng)來(lái)解決該問(wèn)題,下面給大家介紹Spring 事件監(jiān)聽(tīng)機(jī)制實(shí)現(xiàn)跨模塊調(diào)用的思路,感興趣的朋友一起看看吧2024-05-05
GC調(diào)優(yōu)實(shí)戰(zhàn)之高分配速率High?Allocation?Rate
這篇文章主要為大家介紹了GC調(diào)優(yōu)之高分配速率High?Allocation?Rate的實(shí)戰(zhàn)示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-01-01
VerifyCodeServlet(一次性驗(yàn)證碼)
這篇文章主要介紹了VerifyCodeServlet一次性驗(yàn)證碼的使用方法2017-05-05
Springboot集成mybatis與jsp過(guò)程詳解
這篇文章主要介紹了Springboot集成mybatis與jsp過(guò)程,Spring Boot是一種全新的框架(相對(duì)而言),是用來(lái)簡(jiǎn)化Spring應(yīng)用的初始搭建以及開(kāi)發(fā)過(guò)程。該框架使用了特定的方式來(lái)進(jìn)行配置2021-09-09

