JavaFX Tutorial Index

JavaFX Tutorial JavaFX Installation JavaFX Application Structure JavaFX Architecture

JavaFX Applications Charts

JavaFX Bar Chart JavaFX Bubble Chart JavaFX Pie Chart JavaFX Scatter Chart JavaFX Area Chart JavaFX Line Chart

JavaFX 2D Shapes

JavaFX Arc JavaFX Circle JavaFX Ellipse JavaFX Line JavaFX Polygon JavaFX Rectangle JavaFX Color JavaFX Gradient Color

JavaFX 3D Shapes

JavaFX Box JavaFX Cylinder JavaFX Sphere

JavaFX Animations

JavaFX Rotate Transition JavaFX Translate Transition JavaFX Fade Transition JavaFX Fill Transition JavaFX Parallel Transition JavaFX Path Transition JavaFX Pause Transition JavaFX Scale Transition JavaFX Sequential Transition JavaFX Stroke Transition

JavaFX CSS

JavaFX ID Selector JavaFX Inline Styles JavaFX Selectors

JavaFX Effect

JavaFX Blend JavaFX Bloom JavaFX Color Adjust JavaFX Color Input JavaFX Drop Shadow JavaFX Gaussian Blur JavaFX Glow JavaFX Image Input JavaFX Inner Shadow JavaFX Light Distant JavaFX Light Point JavaFX Light Spot JavaFX Lighting JavaFX Motion Blur JavaFX Reflection JavaFX Shadow

JavaFX Layouts

JavaFX Layouts JavaFX BorderPane JavaFX GridPane JavaFX StackPane JavaFX HBox JavaFX TilePane

JavaFX Event Handling

JavaFX Event Handling JavaFX Event Filters JavaFX Convenience Methods

JavaFX Transformation

JavaFX Transformation JavaFX Scaling JavaFX Rotation JavaFX Translation JavaFX Shear

JavaFX UI

JavaFX Menu JavaFX Button JavaFX Button Styling JavaFX CheckBox JavaFX File Chooser JavaFX HyperLink JavaFX Label JavaFX UI Controls JavaFX PasswordField JavaFX ProgressBar JavaFX RadioButton JavaFX ScrollBar JavaFX Slider JavaFX TextField

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