Question: in this code why the figures don't show on the screen just the gray screen show ( import javafx.application.Application; import javafx.scene.Group; import javafx.scene.PerspectiveCamera; import javafx.scene.Scene;

in this code why the figures don't show on the screen just the gray screen show (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.AmbientLight;
import javafx.scene.shape.Cylinder;
import javafx.scene.shape.Sphere;
import javafx.stage.Stage;
import javafx.scene.transform.Rotate;
public class ThreeDSceneExample extends Application {
@Override
public void start(Stage stage){
// Root group for all 3D shapes
Group root = new Group();
// Create the main central sphere
Sphere centralSphere = new Sphere(50);
PhongMaterial redMaterial = new PhongMaterial(Color.RED);
centralSphere.setMaterial(redMaterial);
// Create smaller spheres and cylinders for connections
createAndAddConnection(root, centralSphere, 100,0,0, Color.BLUE); // right
createAndAddConnection(root, centralSphere, -100,0,0, Color.GREEN); // left
createAndAddConnection(root, centralSphere, 0,100,0, Color.BLUE); // top
createAndAddConnection(root, centralSphere, 0,-100,0, Color.GREEN); // bottom
createAndAddConnection(root, centralSphere, 0,0,100, Color.BLUE); // front
createAndAddConnection(root, centralSphere, 0,0,-100, Color.GREEN); // back
// Add ambient light
AmbientLight light = new AmbientLight(Color.WHITE);
root.getChildren().add(light);
// Set up the camera
PerspectiveCamera camera = new PerspectiveCamera(true);
camera.setTranslateZ(-500);
Scene scene = new Scene(root,600,600, true);
scene.setFill(Color.LIGHTGRAY);
scene.setCamera(camera);
// Adding rotation transformation
root.getTransforms().addAll(
new Rotate(45, Rotate.X_AXIS),
new Rotate(45, Rotate.Y_AXIS)
);
// Set up the stage
stage.setTitle("3D Scene Example");
stage.setScene(scene);
stage.show();
}
private void createAndAddConnection(Group root, Sphere centralSphere, double x, double y, double z, Color color){
// Create a small sphere at the end of each arm
Sphere sphere = new Sphere(20);
PhongMaterial material = new PhongMaterial(color);
sphere.setMaterial(material);
sphere.setTranslateX(x);
sphere.setTranslateY(y);
sphere.setTranslateZ(z);
// Create a cylinder to connect the central sphere to the outer sphere
Cylinder connection = new Cylinder(10,100);
connection.setMaterial(new PhongMaterial(Color.DARKGRAY));
connection.setTranslateX((x + centralSphere.getTranslateX())/2);
connection.setTranslateY((y + centralSphere.getTranslateY())/2);
connection.setTranslateZ((z + centralSphere.getTranslateZ())/2);
if (x !=0){
connection.setRotationAxis(Rotate.Y_AXIS);
connection.setRotate(90);
} else if (y !=0){
connection.setRotationAxis(Rotate.X_AXIS);
connection.setRotate(90);
}
root.getChildren().addAll(sphere, connection);
}
public static void main(String[] args){
launch(args);
}
})

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!