简体   繁体   English

如何用JavaFX 2绘制时钟?

[英]How to draw a clock with JavaFX 2?

Given an hour and a minute, I want to draw an analogous clock. 花了一个小时一分钟,我想绘制一个类似的时钟。

I was able to create a Group with a Circle , but I don't know which class to use for the hands and how to place and rotate them. 我能够创建一个带CircleGroup ,但我不知道哪个类用于手以及如何放置和旋转它们。

  1. Add 3 lines (for hour, minute, second) with different strokes and lengths to your Group. 为您的组添加3 (小时,分钟,秒),不同的笔画和长度。
  2. Place one end of each line in the center of your circle and the other end of each line pointing straight up at 12 o'clock. 将每条线的一端放在圆圈的中心,每条线的另一端直指12点钟位置。
  3. Create a timeline which triggers a keyframe each second. 创建一个每秒触发一个关键帧的时间轴
  4. In the keyframe: 在关键帧中:
    • Calculate the degree of rotation for the hands based on the current time . 根据当前时间计算指针的旋转度。
    • Rotate the hands the required amount. 旋转手所需的量。
  5. Set the timeline to cycle indefinitely . 将时间轴设置为无限 循环
  6. Place your Group in a Scene and add the Scene to a Stage. 将您的组放在场景中并将场景添加到舞台。
  7. Play your timeline. 播放你的时间表。

I created a sample app implementing the above principles. 我创建了一个实现上述原则的示例应用程序

在此输入图像描述

Update 更新

In response to criticism of the coding style used in the sample app, I created a refactored sample app which uses a more functional coding style. 为了回应对示例应用程序中使用的编码样式的批评 ,我创建了一个使用更多功能编码样式的重构示例应用程序

Additionally the jfxtras project has an AnalogueClock control. 此外, jfxtras项目还有一个AnalogueClock控件。 Here is the gist of how to use the jfxtras clock. 以下是如何使用jfxtras时钟的要点。

Netbeans 7.1.2 is bundled with sample applications. Netbeans 7.1.2与示例应用程序捆绑在一起。 In JavaFX part there is a StopWatch sample application. 在JavaFX部分中有一个StopWatch示例应用程序。 Have a look at it, it can give you a great clue. 看看它,它可以给你一个很好的线索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM