How to draw a clock with JavaFX 2?

Given an hour and a minute, I want to draw a similar clock.

I managed to create a Group with Circle , but I don’t know which class to use for hands and how to place and rotate them.

+4
source share
2 answers
  • Add 3 lines (for hour, minute, second) with different strokes and lengths for your group.
  • Place one end of each line at the center of your circle and the other end of each line straight up at 12 o’clock.
  • Create a timeline that runs a keyframe every second.
  • In the key frame:
    • Calculate the degree of rotation of the hands based on the current time .
    • Rotate hands should receive the required amount.
  • Set the cycle timeline indefinitely .
  • Put your group in the scene and add the scene to the scene.
  • Play your timeline.

I created an example application that implements the above principles.

enter image description here

Update

In response to criticism of the coding style used in the sample application, I created a refactored sample application that uses a more functional coding style.

In addition, the jfxtras project has an AnalogueClock control. Here is the gist of how to use the jfxtras clock.

+19
source

Netbeans 7.1.2 comes with sample applications. The JavaFX part has a StopWatch application. Look at this, it can give you a great clue.

+5
source

Source: https://habr.com/ru/post/1411861/


All Articles