如何用JavaFX 2绘制时钟? [英] How to draw a clock with JavaFX 2?

查看:387
本文介绍了如何用JavaFX 2绘制时钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一小时一分钟,我想绘制一个类似的时钟。



我能够创建一个带有 Circle ,但我不知道哪个类用于手以及如何放置和旋转它们。

解决方案


  1. 添加3 示例应用程序中使用的编码样式,我创建了一个重构的示例应用,它使用更具功能性的编码风格。



    此外 jfxtras项目具有AnalogueClock控件。以下是如何使用 jfxtras 时钟的要点。


    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.

    解决方案

    1. Add 3 lines (for hour, minute, second) with different strokes and lengths to your Group.
    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.
    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. Here is the gist of how to use the jfxtras clock.

    这篇关于如何用JavaFX 2绘制时钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆