Simple JavaFX Cube 3D
So I started playing with JavaFX and Java 1.8 and I have to say I am loving it. Everything seems to be simple enough to use and I like that it is part of the JDK. So I created my first 3D JavaFX 8 application it is just a simple rotatable cube but I already can see the possibilities.
The final goal is to create a fully networked game using JavaFX 8 so hopefully I will find the time to peruse this.
Feel free to copy and compile this example I am excited to see what other developers out there do with JavaFX 3D.
The final goal is to create a fully networked game using JavaFX 8 so hopefully I will find the time to peruse this.
package me.skidrunner.javafx; import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.PerspectiveCamera; import javafx.scene.Scene; import javafx.scene.input.MouseEvent; import javafx.scene.paint.Color; import javafx.scene.paint.PhongMaterial; import javafx.scene.shape.Box; import javafx.scene.transform.Rotate; import javafx.scene.transform.Translate; import javafx.stage.Stage; public class TestCube3D extends Application { private final Group root = new Group(); private PerspectiveCamera camera; private Rotate cameraRotateX, cameraRotateY, cameraRotateZ; private Translate cameraTranslate; private double mouseOldX, mouseNewX; private double mouseOldY, mouseNewY; @Override public void start(Stage stage) throws Exception { // Create Camera camera = new PerspectiveCamera(true); cameraRotateX = new Rotate(-20, Rotate.X_AXIS); cameraRotateY = new Rotate(0, Rotate.Y_AXIS); cameraRotateZ = new Rotate(0, Rotate.Z_AXIS); cameraTranslate = new Translate(0, 0, -20); camera.getTransforms().addAll( cameraRotateX, cameraRotateY, cameraRotateZ, cameraTranslate); root.getChildren().add(camera); // Create Material PhongMaterial phongMaterial = new PhongMaterial(); phongMaterial.setDiffuseColor(Color.DARKRED); phongMaterial.setSpecularColor(Color.RED); // Create Box Box box = new Box(5, 5, 5); box.setMaterial(phongMaterial); root.getChildren().add(box); // Create Scene and Handlers Scene scene = new Scene(root, 800, 450, true); scene.setOnMousePressed(new EventHandler() { @Override public void handle(MouseEvent mouseEvent) { onMousePressed(mouseEvent); } }); scene.setOnMouseDragged(new EventHandler () { @Override public void handle(MouseEvent mouseEvent) { onMouseDragged(mouseEvent); } }); // Set the Stage stage.setTitle("Test Cube 3D"); stage.setScene(scene); scene.setFill(Color.GRAY); scene.setCamera(camera); stage.show(); } public void onMousePressed(MouseEvent mouseEvent) { mouseOldX = mouseNewX = mouseEvent.getSceneX(); mouseOldY = mouseNewY = mouseEvent.getSceneY(); } public void onMouseDragged(MouseEvent mouseEvent) { mouseOldX = mouseNewX; mouseOldY = mouseNewY; mouseNewX = mouseEvent.getSceneX(); mouseNewY = mouseEvent.getSceneY(); double mouseDeltaX = (mouseNewX - mouseOldX); double mouseDeltaY = (mouseNewY - mouseOldY); cameraRotateX.setAngle(cameraRotateX.getAngle() - mouseDeltaY); cameraRotateY.setAngle(cameraRotateY.getAngle() + mouseDeltaX); } public static void main(String[] args) { launch(); } }
Sorry about the </mouseevent></mouseevent> tags I can't seem to get rid of them.
Feel free to copy and compile this example I am excited to see what other developers out there do with JavaFX 3D.
Comments
Post a Comment