Javafx 简明教程
JavaFX - Effects
效果是任何增强图形外观的操作。在 JavaFX 中,效果是一种应用于节点以在视觉上增强其外观的算法。 Node 类的 effect 属性用于指定效果。
An effect is any action that enhances the appearance of the graphics. In JavaFX, an effect is an algorithm that is applied on nodes to enhance their appearance visually. The effect property of the Node class is used to specify the effect.
在 JavaFX 中,您可以为一个节点设置各种效果,例如 bloom, blur 和 glow 。每种这些效果都由一个类表示,所有这些类都存在于名为 javafx.scene.effect 的包中。
In JavaFX, you can set various effects to a node such as bloom, blur and glow. Each of these effects are represented by a class and all these classes are available in a package named javafx.scene.effect.
Applying Effects to a Node
您可以使用 setEffect() 方法将效果应用于节点。对于此方法,您需要传入效果对象。
You can apply an effect to a node using the setEffect() method. To this method, you need to pass the object of the effect.
要将效果应用于节点,您需要−
To apply an effect to a node, you need to −
-
Create the node.
-
Instantiate the respective class of the effect that is needed to be applied.
-
Set the properties of the effect.
-
Apply the effect to the node using the setEffect() method.
Creating the Nodes
首先,通过实例化其相应的类,在 JavaFX 应用程序中创建节点。
First of all, create the nodes in a JavaFX application by instantiating their respective classes.
例如,如果你想对应用程序中的图像应用发光效果。首先,你需要通过实例化 Image 类创建一个图像节点,并设置其视图,如下所示。
For example, if you want to apply glow effect to an image in your application. Firstly, you need to create an image node by instantiating the Image class and set its view as shown below.
//Creating an image
Image image = new Image("https://www.tutorialspoint.com/green/images/logo.png");
//Setting the image view
ImageView imageView = new ImageView(image);
//Setting the position of the image
imageView.setX(100);
imageView.setY(70);
//setting the fit height and width of the image view
imageView.setFitHeight(200);
imageView.setFitWidth(400);
//Setting the preserve ratio of the image view
imageView.setPreserveRatio(true);
Instantiating the Respective Class
实例化表示需要应用到创建的节点的效果的类。
Instantiate the class representing the effect that is needed to be applied to the created node.
例如 − 要应用发光效果,你需要实例化 Glow 类,如下面的代码框所示 −
For example − To apply the glow effect, you need to instantiate the Glow class as shown in the following code box −
Glow glow = new Glow();
Setting the Properties of the Effect
实例化类后,你需要使用其 setter 方法为效果设置属性。
After instantiating the class, you need to set the properties for the effect using its setter methods.
例如 − 要绘制一个 3 维盒子,你需要传递其宽度、高度和深度。你可以使用它们各自的 setter 方法指定这些值,如下所示 −
For example − To draw a 3-Dimensional box, you need to pass its width, height and depth. You can specify these values using their respective setter methods as shown below −
//setting the level property
glow.setLevel(0.9);
Adding Effect to the Node
最后,你可以使用 setEffect() 方法向节点应用所需的效果。例如:要将发光效果设置到图像节点,你需要按照如下方式将 Glow 类的对象传递给该方法 −
Finally, you can apply the required effect to the node using the setEffect() method. For example: To set the glow effect to the image node, you need to pass the object of the Glow class to this method as follows −
imageView.setEffect(glow);
Types of JavaFX Effects
下表列出了 JavaFX 提供的各种效果(类)。这些类存在名为 javafx.scene.effect 的包中。
The following table gives you the list of various effects (classes) provided by JavaFX. These classes exist in the package called javafx.scene.effect.
S.No |
Effect and Description |
1 |
Color AdjustYou can adjust the color of an image by applying the color adjust effect to it. This includes the adjustment of the hue, saturation, brightness and contrast on each pixel The class named ColorAdjust of the package javafx.scene.effect represents the color adjust effect. |
2 |
Color InputColor Input Effect gives the same output as drawing a rectangle and filling it with color. Unlike other effects, if this effect is applied to any node, it displays only a rectangular box (not the node). This effect is mostly used to pass as an input for other effects. The class named ColorInput of the package javafx.scene.effect represents the color input effect. |
3 |
Image InputImage input effect in JavaFX just embeds an image to the JavaFX screen. Just like Color Input effect (It is used to pass the specified colored rectangular region as input to other effect), Image Input effect is used to pass the specified image as an input to another effect. The class named ImageInput of the package javafx.scene.effect represents the Image Input effect. |
4 |
BlendIn general, blend means mixture of two or more different things or substances. If we apply this blend effect, it takes the pixels of two different inputs, at the same location and it produces a combined output based on the blend mode. The class named Blend of the package javafx.scene.effect represents the blend effect. |
5 |
BloomOn applying bloom effect, pixels in some portions of the node are made to glow. The class named Bloom of the package javafx.scene.effect represents the bloom effect. |
6 |
GlowJust like bloom, the Glow effect makes the given input image to glow, this effect makes the bright pixels of the input brighter. The class named Glow of the package javafx.scene.effect represents the glow effect. |
7 |
Box BlurOn applying this blur effect to a node, it is made unclear. Box blur is a kind of blur effect provided by JavaFX. In this effect, when we apply blur to a node, a simple box filter is used. The class named BoxBlur of the package javafx.scene.effect represents the boxblur effect. |
8 |
GaussianBlurJust like Box Blur Gaussian is an effect to blur the nodes in JavaFX. The only difference in the Gaussian Blur effect is that a Gaussian convolution kernel is used to produce a blurring effect. The class named GaussianBlur of the package javafx.scene.effect represents the Gaussian Blur effect. |
9 |
MotionBlurJust like Gaussian Effects, Motion Blur is an effect to blur the nodes in JavaFX. It also uses a Gaussian convolution kernel to produce a blurring effect, but the difference is in this effect the Gaussian convolution kernel is used with a specified angle. The class named MotionBlur of the package javafx.scene.effect represents the Motion Blur effect. |
10 |
ReflectionOn applying the reflection effect to a node in JavaFX, a reflection of it is added at the bottom of the node. The class named Reflection of the package javafx.scene.effect represents the reflection effect. |
11 |
SepiaToneOn applying the Sepia tone effect to a node in JavaFX (image in general), it is toned with a reddish brown color. The class named SepiaTone of the package javafx.scene.effect represents the sepia tone effect. |
12 |
ShadowThis effect creates a duplicate of the specified node with blurry edges. The class named Shadow of the package javafx.scene.effect represents the sepia tone effect. |
13 |
DropShadowOn applying this effect to a node, a shadow will be created behind the specified node. The class named DropShadow of the package javafx.scene.effect represents the drop shadow effect. |
14 |
InnerShadowOn applying this effect to a node, a shadow will be created inside the edges of the node. The class named InnerShadow of the package javafx.scene.effect represents the inner shadow effect. |
15 |
LightingThe lighting effect is used to simulate a light from a light source. There are different kinds of light sources namely point, distant and spot. The class named Lighting of the package javafx.scene.effect represents the lighting effect. |
16 |
Light.DistantOn applying this effect to a node, a light is simulated on it, as if it is being generated by a distant light source. Distant Light Source − A source which is at a far distance from the node. In here, the light is attenuated in one direction from the source. The class named Light.Distant of the package javafx.scene.effect represents the distant light source. |
17 |
Light.SpotOn applying this effect to a node, a light is simulated on it, as if it is being generated by a spot light. Spot light Source − The light from this source attenuates in all directions. The intensity of the light depends on the distance of the object from the source. The class named Light.Spot of the package javafx.scene.effect represents the distant light source. |
18 |
Point.SpotOn applying this effect to a node, a light is simulated on it, as if it is being generated by a point light source. Point Light Source − The light from this source attenuates in all directions from a single point. The intensity of the light depends on the distance of the object from the source. The class named Point.Spot of the package javafx.scene.effect represents the point light. |