×

JavaFX Sphere

In the JavaFX application, in order to draw a Sphere, the Sphere class is used. The Sphere allows us to form a 3D shape having circular faces. All methods needed for this purpose are present in the javafx.scene.shape.Sphere class.

Properties and corresponding methods to create Sphere:

1. radius

This property of the Sphere class allows us to define the radius of Sphere and the setRadius() method helps to set it.

Constructors of Sphere class in JavaFX:

1. Sphere()

This is the simple constructor of the Sphere class. It creates a sphere with a default 1.0 radius.

2. Sphere(double radius_value)

This is the parameterized constructor of the Sphere class. It creates a sphere with a specified radius.

3. Sphere(double radius_value, int division_value)

This is the parameterized constructor of the Sphere class. It creates a sphere with a specified radius and divisions.

JavaFX 3D Shape – Simple Sphere on colored container

Example:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.CullFace;
import javafx.scene.shape.Sphere;
import javafx.stage.Stage;


public class SphereUI extends Application {  


    @Override  
    public void start( Stage primaryStage ) throws Exception 
    {  
    Sphere newsphere = new Sphere();  
      
    newsphere.setRadius(100);  
    newsphere.setTranslateX(200);  
    newsphere.setTranslateY(150);  
    newsphere.setCullFace( CullFace.BACK );  
          
        PerspectiveCamera newcamera = new PerspectiveCamera();  
        newcamera.setTranslateX(-50);  
        newcamera.setTranslateY(0);  
        newcamera.setTranslateZ(0); 
          


         Group box = new Group();  
         box.getChildren().addAll (newsphere);


         box.getChildren().addAll();      
         Scene scene = new Scene( box, 600, 400, Color.RED);  
         scene.setCamera (newcamera);
         
           primaryStage.setScene ( scene );  
           primaryStage.setTitle(" JavaFX 3D shapes - Sphere Example");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the Sphere in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.Group, javafx.scene.shape.Sphere, javafx.scene.paint.*, javafx.scene.PerspectiveCamera, javafx.scene.shape.CullFace.

Then we have created one class named SphereUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a Sphere, a Group object is created which is then passed to the Scene class object.

The Sphere is created using the setters method to set the radius of the Sphere. Also, the PerspectiveCamera object is created to view the 3D shape of the container.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is invoked. In output Frame like container is displayed with the title, " JavaFX 3D shapes - Sphere Example”. Also, it displays a Sphere with a defined radius.

JavaFX 3D Shape -Sphere

JavaFX 3D Shape – Colored Sphere on simple container

Example:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.CullFace;
import javafx.scene.shape.Sphere;
import javafx.stage.Stage;


public class SphereUI extends Application 
{  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
    Sphere newsphere = new Sphere();  
      
    newsphere.setRadius(100);  
    newsphere.setTranslateX(200);  
    newsphere.setTranslateY(150);  
    newsphere.setCullFace(CullFace.BACK);  
          
        PerspectiveCamera newcamera = new PerspectiveCamera();  
        newcamera.setTranslateX(-50);  
        newcamera.setTranslateY(0);  
        newcamera.setTranslateZ(0); 
          
        PhongMaterial blueStuff = new PhongMaterial();
        blueStuff.setDiffuseColor(Color.BLUE);
        newsphere.setMaterial(blueStuff);




         Group box = new Group();  
         box.getChildren().addAll(newsphere);


         box.getChildren().addAll();      
         Scene scene = new Scene(box,600,400);  
         scene.setCamera(newcamera);
         
           primaryStage.setScene(scene);  
           primaryStage.setTitle(" JavaFX 3D shapes - Colored Sphere Example");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the colored Sphere on simple container in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.Group, javafx.scene.shape.Sphere, javafx.scene.paint.*, javafx.scene.PerspectiveCamera, javafx.scene.paint.Color, javafx.scene.paint.PhongMaterial, javafx.scene.shape.CullFace .

Then we have created one class named SphereUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a colored Sphere on the simple container, a Group object is created which is then passed to the Scene class object.

The Sphere is created using the setters method to set the radius of the Sphere. Also, the PerspectiveCamera object is created to view the 3D shape of the container. The setMaterial() method is used to set the color shape for the Sphere. The setCullFace() method is used to set shades for Sphere.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is invoked. In output Frame like container is displayed with the title, " JavaFX 3D shapes – Colored Sphere Example”. Also, it displays a Colored Sphere on the simple container with a defined radius.

JavaFX 3D Shape -Sphere

Related Topics

JavaFX Convenience Methods

The JavaFX allows us to use various convenience methods to handle events on various nodes in JavaFX applications. The convenience methods provides us easy and effective way to add any...

4 minutes read.

JavaFX StackPane

In the JavaFX application, in order to set Stack Pane as a layout, the StackPane class is used. The StackPane layout allows us to arrange the components in the stack...

4 minutes read.

JavaFX Blend

In the JavaFX application, in order to apply various blend effect, we use Blend class. It has the ability to blend the colors of the shape. All methods needed for...

4 minutes read.

JavaFX Bloom

In the JavaFX application, in order to apply various bloom effect, we use the Bloom class. It has the ability to bloom the colors of the shape. All methods needed...

4 minutes read.

JavaFX Sequential Transition

In the JavaFX application, in order to generate the sequential transition, we have to apply sequential transition on any particular shape. It has the ability to execute multiple transactions sequentially...

4 minutes read.

JavaFX Motion Blur

In the JavaFX application, in order to apply various Motion blur effect, we use Motion Blur class. It has the ability to apply the Motion blur effect on the given...

4 minutes read.

JavaFX Layouts

Layouts in the JavaFX application are treated as a container for different UI elements such as Button, Label, TextArea, etc. Layouts are used as parent container. The layout is used...

3 minutes read.

JavaFX Lighting

In the JavaFX application, in order to apply various Lighting effect from a single source, we use this class. It has the ability to lighten a node from a given...

4 minutes read.

JavaFX Label

The UI Control Label in a JavaFX is used to display the simple text. To use Label in JavaFX application javafx.scene.control.The label class is used. We can place Labels on...

3 minutes read.

JavaFX Color Adjust

In the JavaFX application, in order to apply various color adjust effect, we use ColorAdjust class. It has the ability to apply various color adjust effect such as hue, saturation,...

5 minutes read.

JavaFX ID selector

In the JavaFX application, in order to apply various css effect using the id selector, we use the various css classes available in the JavaFX. This is generally used to...

4 minutes read.

JavaFX TextField

In the JavaFX application, to get the user information as input from the user, a form is created using textfields which allows the user to enter input for specific fields....

4 minutes read.

JavaFX Light Distant

In the JavaFX application, in order to apply various Light Distant effect from a single source, we use this class. It has the ability to apply light from a distant...

4 minutes read.

JavaFX Light Spot

In the JavaFX application, in order to apply various Light Spot effect from a single source, we use this class. It has the ability to apply light from all the...

4 minutes read.

JavaFX Rotate Transition

In the JavaFX application, in order to generate the rotate transition, we have to apply rotation transition on any particular shape. It has the ability to rotate the shape along...

4 minutes read.

JavaFX Reflection

In the JavaFX application, in order to apply various reflection effect, we use this class. It has the ability to create the reflection of the given object. This effect adds...

4 minutes read.

JavaFX Pie Chart

In the JavaFX application, in order to create Pie Chart, the PieChart class is used. The Pie Chart allows us to plot data along with the sectors of the circle....

3 minutes read.

JavaFX Parallel Transition

In the JavaFX application, in order to generate the parallel transition, we have to apply parallel transition on any particular shape. It has the ability to execute multiple transactions parallelly....

4 minutes read.

JavaFX Inner Shadow

In the JavaFX application, in order to apply various Inner Shadow effect, we use this class. It has the ability to create the inner shadow of the given object. This...

4 minutes read.

JavaFX Image Input

In the JavaFX application, in order to apply various image input effect, we use the ImageInput class. It has the ability to fill the specified shape with a given image...

4 minutes read.