I came across a strange behaviour by switching from JDK 7 to JDK 8 (so from JavaFx 2 to 8). I'm quite new to JavaFX and I'm trying to make a simple game to try it out.
I use an AnimationTimer to do the game loop where I move a rectangle by setting its translateX and Y depending on the delta calculated from the long now value received by the handle method.
I had no problem while using Javafx 2, where the delta is about 16 ms as expected and the movement is fluid. Then I realize that I was not using JDK 8, and after switching, a slight issue appeared with the delta.
Every about 20 frames, the delta goes up to 32 ms, and then the rectangle move to far at that point, which is quite noticeable on the screen, like if the rectangle was shaking a bit.
I've tried different thing like using a TimeLine or my own thread (using Platform.RunLater()) but I got the same problem. Using setChache(true) and setCacheHint(CacheHint.SPEED) does not seem to change it either.
I've also tried it on another PC where it does the same BUT, on Linux, with JDK 8, it does not seem to have this issue...
I have Windows 7 x64 with a ATI Radeon R9 270 (in case that matters) and I'm using Netbeans.
Below's a simple full example where I'm moving a rectangle from left to right. You should (?) see the strange drop by running it under JDK 8.
I don't really know if I'm doing something that I'm not supposed to but the example is quite simple, so if you could enlighten me or try it out and tell me if you got the same, that'd be really nice.
Thanks in advance.
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.CacheHint;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Main extends Application {
private long previous;
@Override
public void start(Stage primaryStage) {
Pane root = new Pane();
Scene scene = new Scene(root, 800, 600);
final Rectangle r = new Rectangle(0, 200, 50, 50);
// r.setCache(true);
// r.setCacheHint(CacheHint.SPEED);
root.getChildren().add(r);
final AnimationTimer at = new AnimationTimer() {
@Override
public void handle(long now) {
if (previous != 0) {
final long delta = (now - previous) / 1000000;
r.setTranslateX(r.getTranslateX() + delta * 0.2);
System.out.println(delta);
}
previous = now;
}
};
primaryStage.setTitle("Moving test");
primaryStage.setScene(scene);
primaryStage.show();
at.start();
}
public static void main(String[] args) {
launch(args);
}
}