📜  JavaFX-效果

📅  最后修改于: 2020-11-14 07:10:09             🧑  作者: Mango


效果是可以增强图形外观的任何操作。在JavaFX中,效果是一种应用于节点以在视觉上增强其外观的算法。 Node类的effect属性用于指定效果。

在JavaFX中,可以为节点设置各种效果,例如Bloom,Blurglow 。这些效果中的每一个都由一个类表示,并且所有这些类在名为javafx.scene.effect的包中可用。

将效果应用于节点

您可以使用setEffect()方法将效果应用于节点。对于此方法,您需要传递效果的对象。

要将效果应用于节点,您需要-

  • 创建节点。

  • 实例化需要应用的效果的相应类别。

  • 设置效果的属性。

  • 使用setEffect()方法将效果应用于节点。

创建节点

首先,通过实例化它们各自的类在JavaFX应用程序中创建节点。

例如,如果要在应用程序中对图像应用发光效果。首先,您需要通过实例化Image类并创建其视图来创建一个图像节点,如下所示。

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

实例化相应的类

实例化表示需要应用于创建的节点的效果的类。

例如-要应用发光效果,您需要实例化Glow类,如以下代码框中所示-

Glow glow = new Glow();

设置效果的属性

实例化类后,需要使用其setter方法设置效果的属性。

例如-要绘制三维框,您需要传递其宽度,高度和深度。您可以使用它们各自的设置方法指定这些值,如下所示:

//setting the level property 
glow.setLevel(0.9);

为节点增加效果

最后,您可以使用setEffect()方法将所需的效果应用于节点。例如:要将发光效果设置为图像节点,需要将Glow类的对象传递给此方法,如下所示:

imageView.setEffect(glow); 

JavaFX效果-下表列出了JavaFX提供的各种效果(类)。这些类存在于名为javafx.scene.effect的软件包中。

S.No Shape and Description
1 Color Adjust

You 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 Input

Color 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 Input

Image 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 Blend

In 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 Bloom

On 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 Glow

Just 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 Blur

On 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 GaussianBlur

Just 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 MotionBlur

Just 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 Reflection

On 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 SepiaTone

On 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 Shadow

This 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 DropShadow

On 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 InnerShadow

On 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 Lighting

The 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.Distant

On 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.Spot

On 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.Spot

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