Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Created June 18, 2021 21:11
Show Gist options
  • Save Da9el00/010bb17ac1c8f9db80afb9b2c522082f to your computer and use it in GitHub Desktop.
Save Da9el00/010bb17ac1c8f9db80afb9b2c522082f to your computer and use it in GitHub Desktop.
JavaFX 3D - Change sphere color and adding lights
package sample;
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Sphere;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Sphere sphere = new Sphere(100);
PhongMaterial material = new PhongMaterial();
material.setDiffuseColor(Color.valueOf("#9567fe"));
sphere.setMaterial(material);
Group group = new Group();
group.getChildren().add(sphere);
group.getChildren().addAll(lights());
Scene scene = new Scene(group,1000,800);
scene.setCamera(new PerspectiveCamera());
scene.setFill(Color.SILVER);
sphere.translateXProperty().set(500);
sphere.translateYProperty().set(400);
primaryStage.addEventHandler(KeyEvent.KEY_PRESSED, e ->{
switch (e.getCode()) {
case W -> {
sphere.translateZProperty().set(sphere.getTranslateZ() + 10);
System.out.println(sphere.getTranslateZ());
}
case S -> {
sphere.translateZProperty().set(sphere.getTranslateZ() - 10);
System.out.println(sphere.getTranslateZ());
}
}
});
primaryStage.setTitle("3D World");
primaryStage.setScene(scene);
primaryStage.show();
}
private Node[] lights() {
PointLight pointLight1 = new PointLight();
PointLight pointLight2 = new PointLight();
pointLight2.getTransforms().add(new Translate(1000,0,0));
return new Node[] {pointLight1, pointLight2};
}
public static void main(String[] args) {
launch(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment