如何拉伸图像以使其适合 javafx 中的整个屏幕

How to stretch an image so that it fits the entire screen in javafx

相关代码

package whowantstobeamillionairetriviagame;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundPosition;
import javafx.scene.layout.BackgroundRepeat;
import javafx.scene.layout.BackgroundSize;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class WhoWantsToBeAMillionaireTriviaGame extends Application 
{   
@Override
public void start(Stage startingStage) throws Exception
{    
    Image backgroundColor = new Image("http://www.sonomare.com/darkblue_background_rot_180.jpg");

    BackgroundSize backgroundSize = new BackgroundSize(100, 100, true, true, true, false);
    BackgroundImage backgroundImage = new BackgroundImage(backgroundColor, BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, backgroundSize);

    StackPane background = new StackPane();
    background.setBackground(new Background(backgroundImage));

    Scene menuScene = new Scene(background);
    startingStage.setScene(menuScene);
    startingStage.setFullScreen(true);
    startingStage.show();
}

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

我问是因为使用 repeat 会导致图片在 x 轴和 y 轴上一遍又一遍地重复,因此它可以适合整个屏幕。使用 no repeat 会在其周围留下一堆空白。圆形,Space 并将舞台设置为全屏也无济于事。我能做什么?

您可以将背景大小设置为封面。

来自文档

the size might be defined with cover = true, meaning the image should be stretched to cover the entire rendering surface of the Region.

BackgroundSize 的定义变为

BackgroundSize backgroundSize = new BackgroundSize(100, 100, true, true, true, true);