gpt4 book ai didi

JavaFX 对 BackgroundImage 应用效果

转载 作者:行者123 更新时间:2023-11-30 10:02:46 24 4
gpt4 key购买 nike

情况

我正在使用 BackgroundImage 为 JavaFX 区域设置背景,如下所示:

region.setBackground(Background(BackgroundImage(Image(url)), BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, BackgroundSize(100.0, 100.0, true, true, true, true))))

问题

我想以某种方式使背景变暗,以便白色字体保持可读性。

我尝试过的

我已经搜索过 Image、BackgroundImage 和 Background,但都没有办法添加效果。我发现我可以直接向区域添加效果,但是当我添加 ColorAdjust 时,它会使所有内容变暗,而不仅仅是背景。

我真的不在乎它设置在哪一层,它甚至可以在 CSS 中,我只是想以某种方式使 BackgroundImage 变暗。

最佳答案

当您将 Effect 应用于节点时,它会影响其所有子节点(如果有的话)。您也不能将 Effect 专门应用于 Background,因为它不提供执行此操作的 API1。相反,您可以做的是有一个单独的 Region 用于您的背景图像,然后将它和您的其他内容放在一个共同的父级中。然后您可以将 Effect 应用到这个“背景区域”并且只影响它,而不影响任何其他节点。 StackPane 适合这种情况,因为它会将您的其他内容堆叠在“背景区域”之上;它还会调整该区域的大小以填满所有可用空间(使其成为有效的背景图像)。

// Can also apply background through CSS or FXML. Same with the
// effect, though only DropShadow and InnerShadow can be set through
// CSS—any effect can be set though FXML.
Region backgroundRegion = new Region();
backgroundRegion.setBackground(new Background(new BackgroundImage(...)));
backgroundRegion.setEffect(new ColorAdjust(...));

Node otherContent = ...;

StackPane parent = new StackPane(backgroundRegion, otherContent);
// add "parent" to scene graph...

请注意,我会像上面那样使用 Region,而不是 ImageView,因为前者将保持背景图像的行为;用 ImageView 模仿背景图像并不容易(至少以我的经验)。


1。这适用于代码、CSS 和 FXML。请注意,CSS 和 FXML 只是创建 Background 对象的替代方法。

关于JavaFX 对 BackgroundImage 应用效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56637501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com