Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Using JavaFX we can develop feature-rich desktop applications. JavaFX provides various effects like Blend, Bloom, BoxBlur, ColorAdjust, Lighting, etc. which we can apply into our application. All of these effects classes are used to apply different effects on the image.
In this blog, we will talk about the JavaFX lighting effect. We will see what are the different functions available inside the JavaFX lighting class and how we can use this effect into our application.
Lighting Class
javafx.scene.effect.Lighting class represents the JavaFX lighting effect. It provides a method of giving flat items a more realistic, three-dimensional appearance by simulating a light source beaming on the specified content.
Now let’s see the different types of constructors which we can use to initialize the JavaFX lighting object.
Constructors
There are 2 different types of constructors available.
Lighting(): creates a new JavaFX lighting instance with the default value of light source.
Lighting(Light light): creates a new JavaFX lighting instance with the chosen value of the light source. Here, Light object defines the light source and it can be of various types like Point, Distant, and Spot.
Available Properties
Common Methods
There are many more methods available in the JavaFX lighting class. You can refer to the official documentation of the JavaFX lighting to check all the methods that are available.
Implementation
Now, let’s have a look at the actual implementation of the JavaFX lighting class.
The below program loads the image from the provided URL and then we attach this loaded image to two different imageView objects. After few tweaking to image like setting the height, adjusting the position. we create the JavaFX lighting object and we set this JavaFX lighting effect to the second imageView object. After that we are simply creating two different texts which shows the description below the image. Lastly, we group all these nodes together and create a scene, set the title of our stage, and call the show method to show the output.
Program
// importing required libraries
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.Lighting;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.paint.Color;
public class Lighting_Example extends Application {
@Override
public void start(Stage stage) throws Exception {
// loading the image
Image image = new Image("https://files.codingninjas.in/cn-17917.png");
// imageview to display above image
ImageView imageView = new ImageView(image);
ImageView imageView2 = new ImageView(image);
// setting position of both the images
imageView.setX(50);
imageView.setY(60);
imageView2.setX(350);
imageView2.setY(60);
// setting the size of both the images
imageView.setFitHeight(200);
imageView.setFitWidth(200);
imageView2.setFitHeight(200);
imageView2.setFitWidth(200);
// preserve ratio of the image
imageView.setPreserveRatio(true);
imageView2.setPreserveRatio(true);
// creating and setting the JavaFX lighting effect
Lighting lighting = new Lighting();
imageView2.setEffect(lighting);
// text displayed below the first image
Text text = new Text();
text.setFont(Font.font(null, FontWeight.BOLD, 15));
text.setX(50);
text.setY(290);
text.setText("Without Lighting Effect");
text.setFill(Color.RED);
// text displayed below second image
Text text2 = new Text();
text2.setFont(Font.font(null, FontWeight.BOLD, 15));
text2.setX(370);
text2.setY(290);
text2.setText("With Lighting Effect");
text2.setFill(Color.RED);
// grouping all the nodes together
Group group = new Group(imageView, imageView2, text, text2);
/// creating scene
Scene scene = new Scene(group, 600, 350);
// setting scene
stage.setScene(scene);
// setting the title for our stage
stage.setTitle("Lighting Example");
// displaying the result
stage.show();
}
public static void main(String[] args) {
// launching the application
launch(args);
}
}
You can also try this code with Online Java Compiler
To execute the above program. You can follow the given steps:
Open the terminal in the same folder where you have saved your source code.
Enter the below code and hit enter. javac --module-path path/to/your/javaFx/modules --add-modules javafx.fxml,javafx.controls filename.java After you hit enter a new file will automatically be generatednamely yourFilename.class
Now, enter the below command and hit enter to execute the above program. java --module-path path/to/your/javaFx/modules --add-modules javafx.fxml,javafx.controls filename.java}
You have successfully executed the JavaFX program.
Output
Frequently Asked Questions
What are the different types of effects available in JavaFx?
Effects are used to apply some graphical modifications to the image. In JavaFX, there are various effect classes available like Blend, Bloom, BoxBlur, ColorAdjust, ColorInput, DisplacementMap, DropShadow, GaussianBlur, Glow, and many more.
Can you use JavaFX for games?
The JavaFX animation packages can be used to create games. Rich GUI for gaming apps is provided by JavaFX. Beautiful images are also provided by JavaFX, and these graphics are produced using Canvas. The AnimationTimer class allows us to add time-related actions as the majority of JavaFX games are time-based.
What are the advantages of JavaFX?
JavaFX was created to give programs access to high-end GUI elements including fluid animation, web views, audio and video playback, and Cascading Style Sheets-based styling (CSS).
Conclusion
In this article, we have extensively discussed the JavaFX lighting class available in JavaFx Framework. We have also seen the implementation of the JavaFX lighting effect.