- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a Sphere (3D) in JavaFX?
A sphere is defined as the set of points that are all at the same distance r from a given point in a 3D space. This distance r is the radius of the sphere and the given point is the center of the sphere.
In JavaFX, a sphere is represented by the javafx.scene.shape.Sphere class. This class contains a property named radius. This property represents the radius of the cylinder, you can set the value to this property using the setRadius() method.
To create a 3D Box you need to −
Instantiate this class.
Set the required properties using the setter methods or, bypassing them as arguments to the constructor.
Add the created node (shape) to the Group object.
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.stage.Stage; import javafx.scene.shape.CullFace; import javafx.scene.shape.DrawMode; import javafx.scene.shape.Sphere; public class DrawingSphere extends Application { public void start(Stage stage) { //Drawing a sphere Sphere sphere = new Sphere(); //Setting the properties of the Box(cube) sphere.setRadius(140.0); //Setting other properties sphere.setCullFace(CullFace.BACK); sphere.setDrawMode(DrawMode.FILL); PhongMaterial material = new PhongMaterial(); material.setDiffuseColor(Color.BROWN); sphere.setMaterial(material); //Translating sphere.setTranslateX(300.0); sphere.setTranslateY(150.0); sphere.setTranslateZ(150.0); //Setting the perspective camera PerspectiveCamera cam = new PerspectiveCamera(); cam.setTranslateX(-50); cam.setTranslateY(25); cam.setTranslateZ(0); //Setting the Scene Group root = new Group(sphere); Scene scene = new Scene(root, 595, 300, Color.BEIGE); scene.setCamera(cam); stage.setTitle("Drawing A Sphere"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
Output
Advertisements