Home > Mobile >  Dynamically updating Polyline points in tornadofx
Dynamically updating Polyline points in tornadofx

Time:10-26

This question is a bit simplistic, but i couldn't figure it out for myself... What I'm trying to do is using a JavaFX Timeline to update my Polyline points. What I've got until now is as follows:

class MainView : View("Hello TornadoFX") {
    var myLine: Polyline by singleAssign()
    val myTimeline = timeline {
        cycleCount = INDEFINITE
    }

    override val root = hbox {
        myLine = polyline(0.0, 0.0, 100.0, 100.0)
        myTimeline.apply {
            keyFrames  = KeyFrame((1/10.0).seconds, {
                myLine.points.forEachIndexed { i, d ->
                    myLine.points[i] = d   1
                }
                println(myLine.points)
            })
            play()
        }
    }
}

Although the point list does update the values, as shown when printing them, the change is not reflected in the ui.

Thank you very much for your help!

CodePudding user response:

I have not worked with TornadoFX earlier, so I gave a try with the normal JavaFX. Looks like the the UI is updating when the points are updated. May be the issue is something related to your implementation with TornadoFX.

Please check below the working example.

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Polyline;
import javafx.stage.Stage;
import javafx.util.Duration;

public class PolyLinePointsUpdateDemo extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Pane root = new Pane();
        Scene scene = new Scene(root, 600,600);
        primaryStage.setScene(scene);
        primaryStage.setTitle("PolyLine Points Update");
        primaryStage.show();

        Polyline line = new Polyline(0.0, 0.0, 100.0, 100.0);
        root.getChildren().add(line);

        Timeline timeline = new Timeline();
        timeline.getKeyFrames().add(new KeyFrame(Duration.millis(250), e->{
            for (int i = 0; i < line.getPoints().size(); i  ) {
                double d = line.getPoints().get(i);
                line.getPoints().set(i, d 1);
            }
            System.out.println(line.getPoints());
        }));
        timeline.setCycleCount(Animation.INDEFINITE);
        timeline.play();
    }

    public static void main(String... a){
        Application.launch(a);
    }
}

CodePudding user response:

Figured out that the issue was simply using an hbox as the parent layout, instead of a pane, which handles the chidren positioning with absolute coordinates.

  • Related