×

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 BorderPane

In the JavaFX application, in order to set Border Pane as a layout, the BorderPane class is used. The BorderPane layout allows us to arrange the components in the left,...

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 Event Filters

In JavaFX, in order to handle the event which are created by any of the mouse action, keyboard action, rotate action, scroll action Event filters are used. The event filters...

3 minutes read.

JavaFX TilePane

In the JavaFX application, in order to set TilePane as a layout, the TilePane class is used. The TilePane layout allows us to arrange the components in the same sized...

4 minutes read.

JavaFX Gaussian Blur

In the JavaFX application, in order to apply various Gaussian blur effect, we use GaussianBlur class. It has the ability to apply blur effect on the given object. All methods...

4 minutes read.

JavaFX Fade Transition

In the JavaFX application, in order to generate the fade transition, we have to apply fade transition on any particular shape. It has the ability to show the fading colors...

4 minutes read.

JavaFX CheckBox

In the JavaFX application, to get the user information in the form of choices from the user, a form is created using checkboxes that allow the user to select two...

4 minutes read.

JavaFX Bar Chart

In the JavaFX application, in order to create Bar Chart, the BarChart class is used. The Bar Chart allows us to plot data along different bars to show the scales...

4 minutes read.

JavaFX Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the colors in these shapes using the setFill()...

6 minutes read.

JavaFX Button

The button is one of the UI control present in the JavaFX. The javafx.scene.control.Button class is used for creating a button in javafx example. It is also possible to create...

5 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 Tutorial

Introduction of JavaFX: The most popular applications called Rich Internet Application are build using the JavaFX. This application can run across many platforms. Various devices such as mobile, computers, and tablets...

4 minutes read.

JavaFX Button Styling

 In the JavaFX application, we can apply CSS styling to buttons in various ways. We can change the size of the button as well as border colour, text colour can...

3 minutes read.

JavaFX Transformation

In the JavaFX application, in order to apply various transformation effects, we use Transform class. It has the ability to create the transformation on the given object. This transformation changes...

3 minutes read.

JavaFX File Chooser

In the JavaFX application, in order to open or save the file, FileChooser is used. It allows user to choose the file from system location and save it in a...

4 minutes read.

JavaFX Circle

In the JavaFX application, in order to draw a circle, the Circle class is used. The Circle allows us to join any points in the space. All methods needed for...

4 minutes read.

JavaFX Gradient Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the gradient colors in these shapes using the...

7 minutes read.

JavaFX Architecture

JavaFX architecture consists of many built-in components such as JavaFX public API, Scene Graph, Quantum Toolkit, Prism, Glass windowing toolkit, web view, media engines. These components help to develop rich...

3 minutes read.

JavaFX Pause Transition

In the JavaFX application, in order to generate the pause transition, we have to apply pause transition on any particular shape. It has the ability to take a particular pause...

4 minutes read.

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...

4 minutes read.