JavaFX更新移动形状

时间:2017-12-18 15:14:39

标签: java javafx

我想在我的场景中同时移动3个形状。我已经尝试了很多威胁但到目前为止没有任何工作。我希望你能帮助我,所以我能看到我的3个形状从一侧移动到另一侧。这是我的班级:

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);             
            }
        }
    }
}

0 个答案:

没有答案