2

I wanna move 3 Shapes at the same time in my scene. I already tried a lot with Threats but nothing worked so far. I hope you can help me, so I am able to see my 3 shapes moving from one side to the other side. Here is my Class:

public class TestingArea {
    private Stage stage;
    private Scene scene;
    private Group root;
    private ObservableList<Shape> shapes;

    public TestingArea() {

        shapeTest();

        stage = new Stage();
        stage.setScene(scene);
        stage.show();
    }

    //This method is called by the main class, right after
    //this stage here is shown
    public void startAnimation() {
        animationGo();
    }

    private void shapeTest() {
        root = new Group();
        shapes = FXCollections.observableArrayList();
        int startX = 100;
        int startY = 100;
        int Size = 10;
        //Here i just draw 3 triangles
        Polygon poly = new Polygon(startX, startY, startX+Size*10, startY,
                                   startX+Size*10/2, startY+Size*10);
        for(Double doub: poly.getPoints()) {
            System.out.println(doub);
        }
        int newStartX = poly.getPoints().get(2).intValue();
        int newStartY = poly.getPoints().get(3).intValue();
        Polygon poly2 = new Polygon(newStartX, newStartY
                                    ,newStartX+Size*10, newStartY
                                    ,newStartX+Size*10/2, startY+Size*10
                                    );
        int nextStartX = startX+Size*10/2;
        int nextStartY = startY+Size*10;
        Polygon poly3 = new Polygon(nextStartX, nextStartY
                                    ,nextStartX+Size*10, nextStartY
                                    ,nextStartX+Size*10/2, nextStartY+Size*10 );
        //Add shapes to a List
        shapes.add(poly);
        shapes.add(poly2);
        shapes.add(poly3);

        //Add shapes to root
        root.getChildren().add(poly);
        root.getChildren().add(poly2);
        root.getChildren().add(poly3);

        scene = new Scene(root);
    }

    /**
     * This method should move my 3 triangles 10 times and
     * i wanna see every move.
     */
    private void animationGo() {
        for(int i = 0; i < 10; i++) {
            for(Shape shape: shapes) {
                double x = shape.getLayoutX();
                shape.setLayoutX(x-10);             
            }
        }
    }
}
James_D
  • 201,275
  • 16
  • 291
  • 322
Dubi
  • 33
  • 3
  • See https://stackoverflow.com/questions/9966136/javafx-periodic-background-task / – James_D Dec 18 '17 at 15:30
  • Or more generally, since you are really just animating some shapes here, you need to use the [Animation API](https://docs.oracle.com/javase/9/docs/api/javafx/animation/package-summary.html) (e.g. this is simply a collection of translate transitions, in parallel). – James_D Dec 18 '17 at 15:37
  • The Timeline worked verry well for me! The Animation API is verry good aswell. Thanks a lot for the quick help – Dubi Dec 19 '17 at 07:26

0 Answers0