Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

java - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  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.

enter image description here

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...