Table of contents
1.
Introduction
2.
Light.Point class
2.1.
Constructors
2.2.
Properties
2.3.
Available Methods
3.
Implementation
3.1.
Program
4.
Frequently Asked Questions
4.1.
What is JavaFX?
4.2.
What is JavaFX Stage?
4.3.
How many stages are there in JavaFX?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

JavaFX Light.Point

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

Introduction

We can create feature-rich desktop apps with JavaFX. JavaFX has a number of effects that we may add to our application, including Blend, Bloom, BoxBlur, ColorAdjust, and Lighting. Different effects can be applied to the image using any of these effect classes.

Javafx Light.Point is the light source that can be used in the lighting effect to change the source of light in the effect.

In this blog, we will discuss how we can use this Light source with the lighting effect and change the direction of the light.

JavaFx logo

Light.Point class

Light.Point class represents the light source in 3D space. It is part of the javafx framework. It can be used with the lighting effect to change the source of the light.

Now, let's see the different constructors using which we can initialize this class.

Constructors

  1. Point(): Initialize a new Light.Point object with default values.
     
  2. Point(double x, double y, double z, Color color): Initialize a new Light.Point object with x, y, z, and color values.

Properties

Properties table

Available Methods

Available methods table

Implementation

Let's now examine the lighting class's real implementation.

The application below retrieves the image from the specified URL, loads it, and then attaches it to two distinct imageView instances. after making a few adjustments to the image, such as changing the height and positioning. The lighting object is made, and then we set the light source for it. After that, the second imageView object is given this lighting effect. Then, we create two distinct text objects that display the image's description underneath them. Finally, we combine all of these nodes into a scene, give it a title, and invoke the show function to display the results.

Program

// importing required libraries
import javafx.application.Application;  
import javafx.scene.Group;  
import javafx.scene.Scene;
import javafx.scene.effect.Lighting;
import javafx.scene.effect.Light.Point;
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);

        // light source
        Point source = new Point(400, 150, 250, Color.ORANGE);
        
        // creating and setting the lighting effect
        Lighting lighting = new Lighting();
        imageView2.setEffect(lighting);

        // setting the light source
        lighting.setLight(source);
        
        // 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

Frequently Asked Questions

What is JavaFX?

JavaFX is a software framework for developing and delivering desktop and rich web applications that can operate on a wide range of devices. JavaFX is compatible with Microsoft Windows, Linux, and macOS desktop PCs and web browsers, as well as iOS and Android mobile devices.
 

What is JavaFX Stage?

A Scene, which is made up of visual components, is hosted by a Stage in JavaFX. A stage in a JavaFX application is represented by the Stage class in the javafx. stage package. The start(Stage stage) method of the Application class receives the platform-created primary stage as a parameter.
 

How many stages are there in JavaFX?

JavaFX Stage is a display window for a single application. The main component in JavaFX is the stage. There is only one primary stage, but we can add as many as we like.

Conclusion

In this article, we have extensively discussed the Light.Point class available in JavaFx Framework. We have also seen the implementation of light.Point and we can use this light.point light source into our program.
 

If you think this blog has helped you enhance your knowledge about JavaFx light.point light source 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.

Check out this problem - Smallest Distinct Window.

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!

Please upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass