It has been so long day that I have not blogged but today I am here with some new stuffs related to the JavaFX 3D. Before touching in JavaFX it is better to introduce some lights definition. There are many lights types but let not go more we are relating only two lights


  • JDK 8 EA build ver. 91 or later (or JDK8 General Release Version)

 Ambient Light 
An ambient light source represents a fixed-intensity and fixed-color light source that affects all objects in the scene equally. Upon rendering, all objects in the scene are brightened with the specified intensity and color. This type of light source is mainly used to provide the scene with a basic view of the different objects in it.

ambient lights javafx

Point Light  
Light originates from a single point, and spreads outward in all directions. This is like the torch light in a dark room.

point light

In javafx you can use many Lights like a node because all lights object are inherited from LightBase  and the LightBase inherits from Node. All the 3D Objects needs Lights for making it more natural. Currently I have just explained 2 lights because the latest jdk8 early access build 91 contains only two lights PointLight,AmbientLight but I guess there will be more light added like AreaLight, HemisphereLight because JavaFX3D is still in development. 



Before we start our Lights demo you need to know about the Material term which is widely used in 3D stuffs. JavaFX currently supports the PhongMaterial which is very important for 3D Object. Material helps 3D Object to get more natural by the help of lights. Lets see the usage of PhoneMaterial in Shape3D of JavaFX.
Material Adding in Cylinder

Cylinder c = new Cylinder(200,200);
PhongMaterial material = new PhongMaterial();

Point Light

PointLight light = new PointLight();

Ambient Light

AmbientLight light = new AmbientLight();

Ok Now lets make one small application which makes PointLight to a Sphere.

import javafx.application.Application;
import javafx.scene.AmbientLight;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.PointLight;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Cylinder;
import javafx.scene.shape.Shape3D;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;

 * @author Narayan
public class LightsDemo extends Application {
     * Making 3D Object
     * @param x
     * @param y
     * @param radius
     * @param height
     * @return 
    public Shape3D make3DObject(double x, double y,double radius){
        Cylinder c = new Cylinder(radius,radius);
        //Creating PhongMaterial
        PhongMaterial material = new PhongMaterial();
        //Diffuse Color
        //Specular Color
        c.getTransforms().add(new Rotate(20,Rotate.X_AXIS));
        c.getTransforms().add(new Rotate(10,Rotate.Z_AXIS));
        c.getTransforms().add(new Rotate(30,Rotate.Y_AXIS));
        return c;
     * Function for starting the stage and scene components
     * @param primaryStage
     * @throws IOException 
    public void start(Stage primaryStage) throws IOException {
        Group root = new Group();
        //Creating 3DShape
        Shape3D sh = make3DObject(300,300,200);
        //Creating Ambient Light
        AmbientLight ambient = new AmbientLight();
        ambient.setColor(Color.rgb(0, 255, 0,0.6));
        //Creating Point Light
        PointLight point = new PointLight   ();
        point.setColor(Color.rgb(255, 255, 255,1));
        //Adding nodes inside Container
        //Adding to scene
        Scene scene = new Scene(root,600,600);
        //Creating Perspective View Camera
        PerspectiveCamera cam=  new PerspectiveCamera(false);
        scene.setCamera(cam );
        primaryStage.setTitle("3D World");

     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     * @param args the command line arguments
    public static void main(String[] args) {

The way it actually outputs like below:

Ambient and Point Light JavaFX

The output is pretty fine and I guess it may differ in other system because of the Graphics Card acceleration. Please make sure that you must have at least JDK-8-ea-bin-b91 package to run these codes.

Ok! till then keep coding and have great day geeks 🙂