简体   繁体   English

JavaFX 对背景图像应用效果

[英]JavaFX apply effect on a BackgroundImage

Situation情况

I am using a BackgroundImage to set the background for a JavaFX Region, like this:我正在使用 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))))

Problem问题

I would like to darken the background somehow so that the white font stays readable.我想以某种方式使背景变暗,以便白色字体保持可读性。

What I've tried我试过的

I have searched through Image, BackgroundImage and Background, but none have any way to add an effect.我已经搜索了 Image、BackgroundImage 和 Background,但没有任何方法可以添加效果。 I found that I can add an effect directly to the Region, but when I add the ColorAdjust, it darkens everything, not just the background.我发现我可以直接给 Region 添加效果,但是当我添加 ColorAdjust 时,它会使所有内容变暗,而不仅仅是背景。

I really don't care at which layer it is set, it could even be in CSS, I simply want to somehow darken the BackgroundImage.我真的不在乎它设置在哪个层,它甚至可以在 CSS 中,我只是想以某种方式使背景图像变暗。

When you apply an Effect to a node it affects all of its children, if it has any.当您将Effect应用于节点时,它会影响其所有子节点(如果有)。 You also can't apply an Effect to a Background specifically as it provides no API to do so 1 .您也不能专门将Effect应用于Background因为它没有提供 API 来执行此操作1 Instead, what you can do is have a separate Region to use for your background image and then place it and your other content in a common parent.相反,您可以做的是有一个单独的Region用于您的背景图像,然后将它和您的其他内容放在一个共同的父级中。 You could then apply the Effect to this "background region" and only affect it, not any other nodes.然后您可以将Effect应用到这个“背景区域”并且只影响它,而不影响任何其他节点。 A parent that would work well for this situation is a StackPane since it will stack your other content above the "background region";适合这种情况的父级是StackPane因为它会将您的其他内容堆叠在“背景区域”之上; it will also resize the region to fill up all available space (making it effectively a background image).它还将调整区域大小以填满所有可用空间(使其有效地成为背景图像)。

// 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...

Note I'd use a Region , like above, rather than an ImageView as the former will keep the behavior of a background image;注意我会像上面一样使用Region ,而不是ImageView因为前者会保持背景图像的行为; it's not that easy to imitate a background image with an ImageView (at least in my experience).ImageView模仿背景图像并不容易(至少以我的经验)。


1. This applies to code, CSS, and FXML. 1. 这适用于代码、CSS 和 FXML。 Note that CSS and FXML are just alternative ways to creating a Background object.请注意,CSS 和 FXML 只是创建Background对象的替代方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM