java—尝试在javafx中使用imageviews创建滚动背景效果

djmepvbi  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(355)

我试图创建一个滚动的背景效果与两个图片视图,其中一张图片是在另一张图片的顶部和窗外;然后我尝试通过改变它们的y坐标来向下滚动窗口以创建滚动效果。我做了一个循环这样做,并把一个线程。睡眠,所以它不会做得太快。然后我重置picutres的位置,再做一次循环。但是,当我尝试运行程序时,窗口将永远不会打开。取出循环显然正确地显示了带有picutre的窗口。

public class TestBackground extends Application{

@Override
public void start(Stage stage) throws Exception {

    stage.setTitle("DRIFT STAGE");

    Pane game = new Pane();
    Scene gameScene = new Scene(game, 956, 740);
    ImageView background = new ImageView(getClass().getResource("bg.png").toExternalForm());
    game.getChildren().add(background);

    ImageView background2 = new ImageView(getClass().getResource("bg.png").toExternalForm());
    game.getChildren().add(background2);        
    background2.setY(-740);

    //loop to scroll background vertically
    for (int j = 0; j < 20; j++) {
        for (double i = 1.0; i < 741.0; i++) {
            background.setY(background.getY() + i);
            background2.setY(background2.getY() + i);

            try {
                Thread.sleep(250);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        background.setY(0.0);
        background2.setY(-740.0);
    }

    stage.setScene(gameScene);
    stage.show();

    }
    public static void main(String[] args) { launch(args); }
}
vpfxa7rd

vpfxa7rd1#

你的循环不是正确的做法。在每个imageview上使用过渡动画。你想要翻译。
像这样:

// Animation to scroll background vertically
    TranslateTransition trans1 = new TranslateTransition(Duration.minutes(1), background);
    trans1.setFromY(0);
    trans1.setToY(740);
    trans1.setCycleCount(20);
    TranslateTransition trans2 = new TranslateTransition(Duration.minutes(1), background2);
    trans2.setFromY(-740);
    trans2.setToY(0);
    trans2.setCycleCount(20);
    ParallelTransition parTrans = new ParallelTransition(trans1, trans2);
    parTrans.play();

如果你的目的是让这些图像作为一个视差背景,滚动“永远”,设置过渡周期无限期

trans1.setCycleCount(Animation.INDEFINITE);

每种方法的持续时间略有不同。
如果使用相同的图像,请不要加载两次:

Image bgImg = new Image(getClass().getResource("bg.png").toExternalForm());
    ImageView background = new ImageView(bgImg);
    game.getChildren().add(background);

    ImageView background2 = new ImageView(bgImg);
    game.getChildren().add(background2);

以下是添加速度滑块的整个启动方法,只是为了好玩:

@Override
public void start(Stage stage) {

    stage.setTitle("DRIFT STAGE");

    Pane game = new Pane();
    Scene gameScene = new Scene(game, 956, 740);
    Image bgImg = new Image(getClass().getResource("bg.png").toExternalForm());
    ImageView background = new ImageView(bgImg);
    ImageView background2 = new ImageView(bgImg);
    Slider speedSlider = new Slider(0, 5, 1);
    game.getChildren().addAll(background, background2, speedSlider);

    // Animation to scroll background vertically
    TranslateTransition trans1 = new TranslateTransition(Duration.seconds(10), background);
    trans1.setFromY(0);
    trans1.setToY(740);
    trans1.setInterpolator(Interpolator.LINEAR);
    trans1.setCycleCount(Animation.INDEFINITE);
    TranslateTransition trans2 = new TranslateTransition(Duration.seconds(10), background2);
    trans2.setFromY(-740);
    trans2.setToY(0);
    trans2.setCycleCount(Animation.INDEFINITE);
    trans2.setInterpolator(Interpolator.LINEAR);
    ParallelTransition parTrans = new ParallelTransition(trans1, trans2);
    parTrans.rateProperty().bind(speedSlider.valueProperty());
    parTrans.play();

    stage.setScene(gameScene);
    stage.show();
}

相关问题