English 中文(简体)
JavaFX - Effects
  • 时间:2024-11-03

JavaFX - Effects


Previous Page Next Page  

An effect is any action that enhances the appearance of the graphics. In JavaFX, an effect is an algorithm that is appped on nodes to enhance their appearance visually. The effect property of the Node class is used to specify the 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

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

    Set the properties of the effect.

    Apply the effect to the node using the setEffect() method.

Creating the Nodes

First of all, create the nodes in a JavaFX apppcation by instantiating their respective classes.

For example, if you want to apply glow effect to an image in your apppcation. 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 appped to the created node.

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

After instantiating the class, you need to set the properties for the effect using its setter methods.

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

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

JavaFX Effects − The following table gives you the pst of various effects (classes) provided by JavaFX. These classes exist in the package called 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 filpng it with color. Unpke other effects, if this effect is appped 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 pke 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 pke 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 pke 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 pke 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 duppcate 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 pghting effect is used to simulate a pght from a pght source. There are different kinds of pght sources namely point, distant and spot.

The class named Lighting of the package javafx.scene.effect represents the pghting effect.

16 Light.Distant

On applying this effect to a node, a pght is simulated on it, as if it is being generated by a distant pght source.

Distant Light Source − A source which is at a far distance from the node. In here, the pght is attenuated in one direction from the source.

The class named Light.Distant of the package javafx.scene.effect represents the distant pght source.

17 Light.Spot

On applying this effect to a node, a pght is simulated on it, as if it is being generated by a spot pght.

Spot pght Source − The pght from this source attenuates in all directions. The intensity of the pght depends on the distance of the object from the source.

The class named Light.Spot of the package javafx.scene.effect represents the distant pght source.

18 Point.Spot

On applying this effect to a node, a pght is simulated on it, as if it is being generated by a point pght source.

Point Light Source − The pght from this source attenuates in all directions from a single point. The intensity of the pght depends on the distance of the object from the source.

The class named Point.Spot of the package javafx.scene.effect represents the point pght.

Advertisements