如何在Javafx中将背景大小适合窗口大小? [英] how to fit the background size into the window size in javafx?

查看:111
本文介绍了如何在Javafx中将背景大小适合窗口大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将背景尺寸设置为窗口尺寸。
,但是,这很挑剔。我没有使用css文件格式。

I tried to make the background size as the window size. but, It's quite picky. I didn't use the css file format.

这是实现窗口的主要部分。

it's the part of main to implement the window.

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


}

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但,我不能。
我应该怎么做才能得到解决方案?

I tired to use BackgroundSize.AUTO but, I can't. what should I do to get the solution?

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

If, I can use the css format how to use that? but I can't rewrite and revise lots of codes because, mine is almost done and I'm integrating and debugging.

推荐答案

如果要拉伸图像以填充整个 Region ,则应使用:

If you want to stretch the image to fill the entire Region you should use:

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

<$ c的两个 true 参数$ c> BackgroundSize 分别表示 width height 参数是成比例的,而不是绝对。在这种情况下,宽度高度的范围应在 [0.0,1.0]范围内,也称为0%至100%。两个 false 参数分别为 contain cover 。对于 width height 参数,它们必须为 false 使用。换句话说,这就是说要JavaFX使图像填充区域的宽度和高度的100%。请注意,这不会保持图像的长宽比(请参见下文)。

The two true arguments of BackgroundSize mean the width and height arguments, respectively, are proportional rather than absolute. When that's the case the width and height should be in the range [0.0, 1.0], otherwise known as 0% to 100%. The two false arguments are contain and cover, respectively. They must be false for the width and height arguments to be used. In other words, that's telling JavaFX to make the image fill 100% of both the width and height of the Region. Note this will not maintain the aspect ratio of the image (see below).

请参见 BackgroundSize 的文档以获取更多信息:

See the documentation of BackgroundSize for more information:


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

Defines the size of the area that a BackgroundImage should fill relative to the Region it is styling. There are several properties whose values take precedence over the others. In particular there are 4 key properties, width, height, contain, and cover. Both width and height are independent of each other, however both interact with contain and cover.

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

From the CSS Specification, cover is defined to:


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

包含定义为:


  • 将图像缩放到最大尺寸,同时保留其固有的宽高比(如果有),以使其宽度

宽度和高度(以绝对值或百分比形式)指定要设置为用。仅当Cover和Contain均为false时,这两个属性才适用。如果Cover和Contain均为true,则将使用Cover。

And width and height both specify (in absolute values or percentages) the size to use. These two properties only apply if both cover and contain are false. If both cover and contain are true, then cover will be used.

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

The width and height may also be set to AUTO, indicating that the area should be sized so as to use the image's intrinsic size, or if it cannot be determined, 100%.

这篇关于如何在Javafx中将背景大小适合窗口大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆