Table of contents
1.
Introduction
2.
JavaFX Light.Distant Effect
2.1.
Properties
2.2.
Constructors
2.3.
Commonly Used Methods
3.
Implementation
3.1.
Program
4.
Frequently Asked Questions
4.1.
What is the difference between JavaFX Light.Distant and JavaFX Light.Point?
4.2.
Why do the lighting effects get dimmer when we use JavaFX Light.Distant effect?
4.3.
What properties do JavaFX Light.Distant have?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

JavaFX Light.Distant

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

Introduction

The javafx.scene.effect.Light.Distant class inside the JavaFX deals with adding the light effects to the objects when the light source is kept at a fair distance from the object. There are many instances where we need to use these effects whenever the light source is kept far away from the object and to successfully inculcate this into our project we need JavaFX Light.Distant class. In this article, we will learn about adding this class and using this effect to enhance our developments in web applications.

JavaFX Light.Distant Effect

In this effect, we add the lighting effect on the object in such a manner that the source of the light is kept at a fair distance from the object and hence the light waves are attenuated which makes the light dim and less visible. The name of the class includes ‘distant’ which signifies the light source is placed at a distance far away from the target object. Hence adding this kind of lighting effect is a very useful effect when creating a web application using JavaFX.

It works with the help of two properties which will be discussed in the next section.

Properties

  1. Azimuth
    It is the horizontal angle or direction of a compass bearing. This property is of double type and it contains the azimuth of the light.

     
  2. Elevation
    As the name suggests it represents the elevation of the light source and it is also of double type.
     

The next section will discuss the two types of constructors that are used in this class.

Constructors

  1. public Light.Distant(): It creates a new instance of the class with the default values of azimuthelevation, and color.
     
  2. public Light.Distant(double azimuth, double elevation, Color color): It creates a new instance of the class with the specified parameters.


Let us now discuss its methods in the next section.

Commonly Used Methods

available methods table

Implementation

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

Background

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.Distant;
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
        Distant source = new Distant();
        
        // setting properties for our light source
        source.setAzimuth(0.6);  
        source.setColor(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 the difference between JavaFX Light.Distant and JavaFX Light.Point?

In JavaFX Light.Distant class the light source is kept at a fair distance from the object whereas in JavaFX Light.Point the light source is more focussed on a point of an object.
 

Why do the lighting effects get dimmer when we use JavaFX Light.Distant effect?

It happens because we are assuming that the light source is kept far away from the source hence to match the visualization the light gets attenuated and eventually becomes dimmer.
 

What properties do JavaFX Light.Distant have?

It has two properties named azimuth and elevation. Both of them are of double type.

Conclusion

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

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

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