I have a JavaFX app where I draw on a canvas. The drawing follows the mouse. This movement is a bit laggy, because the rendering takes some time. That's ok so far. But when I stop the mouse, its coordinates are sometimes still on an old position.
The following code reproduces the problem:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.EllipseBuilder;
import javafx.stage.Stage;
public class TestApp extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception
{
Pane p = new Pane();
final Ellipse ellipse = EllipseBuilder.create().radiusX(10).radiusY(10).fill(Color.RED).build();
p.getChildren().add(ellipse);
p.setOnMouseMoved(event ->
{
ellipse.setCenterX(event.getX());
ellipse.setCenterY(event.getY());
Platform.runLater(() -> doSomeWork());
});
Scene scene = SceneBuilder.create().root(p).width(1024d).height(768d).build();
primaryStage.setScene(scene);
primaryStage.show();
}
void doSomeWork()
{
try
{
Thread.sleep(100);
}
catch (Exception ignore) { }
}
}
When you move the mouse fast and stop abrupt, the Circle is sometimes not under the mouse.
I've play with use or don't use Platform.runLater() or the call order. No success.
Edit: I can't reproduce this behaviour under Windows.