【问题标题】:how to fit the background size into the window size in javafx?javafx 中如何将背景大小调整到窗口大小?
【发布时间】:2019-02-25 23:53:37
【问题描述】:

我尝试将背景大小设为窗口大小。 但是,它很挑剔。我没有使用css文件格式。

主要是实现窗口的部分。

public void start(Stage primaryStage) throws Exception {
    GameLoopManager loop = GameLoopManager.instance();
    Controller controller = new Controller(loop, primaryStage);
    loop.start(new MenuState());
    primaryStage.show();

    primaryStage.setFullScreen(true);


}

这是实现背景和舞台的body部分。

private UISubScene optionSubScene;
private UISubScene helpSubScene;
private UISubScene creditsSubScene;
private UISubScene buttonChooserScene;

private UISubScene sceneToHide;

List<UIButton> menuButtons;

List<ButtonPicker> buttonsList;

private BUTTON chosenButton;

public MenuViewManager(Stage mainStage) {
    sound.backgroundMusic();
    menuButtons = new ArrayList<>();
    mainPane = new AnchorPane();
    mainScene = new Scene(mainPane);
    mainScene.setFill(null);
    mainStage.setScene(mainScene);

    super.mainStage = mainStage;

    createSubScenes();
    createButtons();
    createBackground();
    createLogo();

    super.mainStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
        public void handle(WindowEvent we) {
            controller.stop();
        }
    });
    // mainStage.show();

}

private void createBackground() {
    Image backgroundImgae = new Image("main/resources/images/jxoPOUxa.gif",true);
    BackgroundImage background = new BackgroundImage(backgroundImgae, BackgroundRepeat.NO_REPEAT,
            BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT, BackgroundSize.DEFAULT);        
    mainPane.setBackground(new Background(background));


}

我厌倦了使用 BackgroundSize.AUTO,但是我不能。 我该怎么做才能得到解决方案?

如果,我可以用css格式怎么用呢?但是我不能重写和修改很多代码,因为我的代码快完成了,我正在集成和调试。

【问题讨论】:

    标签: java eclipse javafx


    【解决方案1】:

    如果您想拉伸图像以填充整个Region,您应该使用:

    // Side note: Are you sure having "main/resources" in the path is correct?
    var image = new Image("main/resources/images/jxoPOUxa.gif", true);
    var bgImage = new BackgroundImage(
            image,
            BackgroundRepeat.NO_REPEAT,
            BackgroundRepeat.NO_REPEAT,
            BackgroundPosition.DEFAULT,
            new BackgroundSize(1.0, 1.0, true, true, false, false)
    );
    mainPain.setBackground(new Background(bgImage));
    

    BackgroundSize 的两个true 参数分别表示widthheight 参数,它们是成比例的而不是绝对的。在这种情况下,widthheight 应该在[0.0, 1.0] 范围内,也称为0% 到100%。两个false 参数分别是containcover。要使用 widthheight 参数,它们必须是 false。换句话说,这告诉 JavaFX 让图像填充 Region 的宽度和高度的 100%。请注意,这不会保持图像的纵横比(见下文)。

    请参阅documentation of BackgroundSize 了解更多信息:

    定义 BackgroundImage 应填充的区域相对于其样式设置的区域的大小。有几个属性的值优先于其他属性。特别是有 4 个关键属性,widthheightcontaincover。宽度和高度都是相互独立的,但都与包含和覆盖交互。

    根据 CSS 规范,cover 被定义为:

    • 在保留其固有纵横比(如果有)的同时将图像缩放到最小尺寸,使其宽度和高度都可以完全覆盖背景定位区域。

    contain 定义为:

    • 将图像缩放到最大尺寸,同时保留其固有纵横比(如果有),使其宽度和高度都可以放在背景定位区域内。

    宽度和高度都指定(以绝对值或百分比形式)要使用的尺寸。这两个属性仅适用于覆盖和包含都为假的情况。如果cover 和contain 都为真,则使用cover。

    宽度和高度也可以设置为AUTO,表示区域的大小应该使用图像的固有大小,或者如果无法确定,则为100%。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-24
      • 1970-01-01
      • 2014-07-26
      • 2023-02-11
      • 2017-03-25
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      相关资源
      最近更新 更多