Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaJavaFX | PointLight with examples

JavaFX | PointLight with examples

PointLight is a part of JavaFX. PointLight defines a point light source. PointLight is a fixed light source in space that radiates light in all direction.

Constructor of the class are:

  1. PointLight(): Creates a new instance of pointlight
  2. PointLight(Color c): Creates a new instance of point light of specific color

Commonly Used Methods:

Method Explanation
getColor() returns the color of the light
isLightOn() returns whether the light is on or not
setColor(Color value) sets the color of the light
setLightOn(boolean value) Sets the value of the property lightOn.

Below programs illustrate the PointLight class:

  1. Java program to create a point light of default color: This program creates a Sphere indicated by the name sphere(radius is passed as arguments). A PointLight is created named pointlight which is a point light source and radiates light in all direction. The Sphere will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a Group is created, and the sphere and pointlight are attached. The group is attached to the scene. Finally, the show() method is called to display the final results. A perspective camera will be created and added to the scene to render the cylinder in 3D.




    // Java program to create a point light of default color
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.shape.DrawMode;
    import javafx.scene.layout.*;
    import javafx.event.ActionEvent;
    import javafx.scene.PointLight;
    import javafx.scene.shape.Sphere;
    import javafx.scene.control.*;
    import javafx.stage.Stage;
    import javafx.scene.Group;
    import javafx.scene.PerspectiveCamera;
    import javafx.scene.paint.Color;
      
    public class pointlight_2 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
              
            // set title for the stage
            stage.setTitle("creating sphere");
      
            // create a sphere
            Sphere sphere = new Sphere(80.0f);
      
            // create a point light
            PointLight pointlight = new PointLight();
      
            // create a Group
            Group group = new Group(sphere, pointlight);
      
            // translate the sphere to a position
            sphere.setTranslateX(100);
            sphere.setTranslateY(100);
      
            // translate point light
            pointlight.setTranslateZ(-1000);
            pointlight.setTranslateX(+1000);
            pointlight.setTranslateY(+10);
      
            // create a perspective camera
            PerspectiveCamera camera = new PerspectiveCamera(false);
            camera.setTranslateX(0);
            camera.setTranslateY(0);
            camera.setTranslateZ(0);
      
            // create a scene
            Scene scene = new Scene(group, 500, 300);
      
            scene.setCamera(camera);
      
            // set the scene
            stage.setScene(scene);
      
            stage.show();
        }
          
        // Main Method
        public static void main(String args[])
        {
              
            // launch the application
            launch(args);
        }
    }

    
    

    Output:

  2. Java program to create a point light of a specified color(e.g. RED): This program creates a Sphere indicated by the name sphere( radius is passed as arguments). A PointLight is created named pointlight( with a specified color passed as an argument which is a point light source and radiates light in all direction. The Sphere will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a Group is created, and the sphere and pointlight are attached. The group is attached to the scene. Finally, the show() method is called to display the final results. A perspective camera will be created and added to the scene to render the cylinder in 3D.




    // Java program to create a point light
    // of specified color(eg RED)
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.shape.DrawMode;
    import javafx.scene.layout.*;
    import javafx.event.ActionEvent;
    import javafx.scene.PointLight;
    import javafx.scene.shape.Sphere;
    import javafx.scene.control.*;
    import javafx.stage.Stage;
    import javafx.scene.Group;
    import javafx.scene.PerspectiveCamera;
    import javafx.scene.paint.Color;
      
    public class sphere_1 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
              
            // set title for the stage
            stage.setTitle("creating sphere");
      
            // create a sphere
            Sphere sphere = new Sphere(80.0f);
      
            // create a point light
            PointLight pointlight = new PointLight(Color.RED);
      
            // create a Group
            Group group = new Group(sphere, pointlight);
      
            // translate the sphere to a position
            sphere.setTranslateX(100);
            sphere.setTranslateY(100);
      
            // translate point light
            pointlight.setTranslateZ(-1000);
            pointlight.setTranslateX(+1000);
            pointlight.setTranslateY(+10);
      
            // create a perspective camera
            PerspectiveCamera camera = new PerspectiveCamera(false);
            camera.setTranslateX(0);
            camera.setTranslateY(0);
            camera.setTranslateZ(0);
      
            // create a scene
            Scene scene = new Scene(group, 500, 300);
      
            scene.setCamera(camera);
      
            // set the scene
            stage.setScene(scene);
      
            stage.show();
        }
          
        // Main Method
        public static void main(String args[])
        {
              
            // launch the application
            launch(args);
        }
    }

    
    

    Output:

Note: The above programs might not run in an online IDE. Please use an offline compiler.

Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/PointLight.html

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments