Table of contents
1.
Introduction
2.
Lighting Class
2.1.
Constructors
3.
Available Properties
4.
Common Methods 
5.
Implementation
5.1.
Program
6.
Frequently Asked Questions
6.1.
What are the different types of effects available in JavaFx?
6.2.
Can you use JavaFX for games?
6.3.
What are the advantages of JavaFX?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

JavaFX Lighting

Author Harsh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

javafx logo

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.

  1. Lighting(): creates a new JavaFX lighting instance with the default value of light source.

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

available properties table

Common Methods 

common methods table

 

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
Run Code

 

How to execute the above program?

To execute the above program. You can follow the given steps:

  1. Open the terminal in the same folder where you have saved your source code.
     
  2. 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 generated namely yourFilename.class
     
  3. 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}
     
  4. You have successfully executed the JavaFX program.

     

Output

output screen

 

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.
 

If you think this blog has helped you enhance your knowledge about JavaFx lighting effect and if you would like to learn more, check out our articles JavaFXJavaFX HBoxJavaFX FileChooserJavaFX VBoxJavaFX Fade TransitionJavaFX Sequential Transition, and many more on our Website.
 

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Thank you

Please upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass