Reputation: 28081
I have this working code:
Duration duration = Duration.millis(1000 / 30.0);
videoTick = new Timeline(new KeyFrame(duration, new EventHandler<ActionEvent>() {
public void handle(ActionEvent actionEvent) {
System.out.println("tick");
}
}));
videoTick.setCycleCount(Animation.INDEFINITE);
videoTick.playFromStart();
Perfectly. But once I change the duration slightly:
Duration duration = Duration.millis(1000 / 30.1);
It doesn't work anymore! Am I insane or JavaFx totally broken? How to work around it?
Edit: I know this bug looks not to make ANY sense, but it just happened in front of me. I provided the minimum code to reproduce it.
import javafx.animation.*;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.util.Duration;
import javafx.stage.Stage;
public class Ticker extends Application {
private Timeline videoTick;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(final Stage primaryStage) {
Duration duration = Duration.millis(1000 / 30.0);
videoTick = new Timeline(new KeyFrame(duration, new EventHandler<ActionEvent>() {
public void handle(ActionEvent actionEvent) {
System.out.println("Tick");
}
}));
videoTick.setCycleCount(Animation.INDEFINITE);
videoTick.playFromStart();
}
}
Then change the 30.0
to 30.1
.
EDIT 2:
Ok, here is a (partial) workaround: (EDIT 4: sorry, this doesn't work. Obviously I'm too tired.)Timeline#play()
works when Timeline#playFromStart()
doesn't.
But the problem isn't solved: what if you really need playFromStart
? And more importantly, why it doesn't work with 1000/30.1
?
EDIT 3:
I reproduced it on OSX 10.8 + Java/JavaFx 1.7.0_17 and Java/JavaFx 1.7.0_21.
Upvotes: 3
Views: 1227
Reputation: 159341
Java 7 Bug
It's a bug with JavaFX in Java 7. I created RT-31027 in the JavaFX issue tracker to track this issue.
I was able to reproduce this (no tick output) using Java 1.7.0_21, OSX 10.8 when compiling and running from the command line.
Sorry for earlier mis-report that it was working in Java 1.7.0_21 (I had made a setup error with java version switching in my IDE).
Fixed in Java 8
It does work (ticks are output) in Java8b92, so the bug would appear to have been fixed in a later JavaFX build. Java 8 early access builds are available. So as a workaround, you could use Java 8.
Alternate Implementation
For these kinds of animations, which happen very often, you could also consider using an AnimationTimer instead.
Update
RT-31027, the issue tracker record I created to track this, was closed as fixed in 8.0, with the comment "Only critical fixes are backported to 2.2, so closing this as fixed".
Upvotes: 1