Skip to content

Instantly share code, notes, and snippets.

@jpt1122
Last active December 31, 2015 14:49
Show Gist options
  • Save jpt1122/8002519 to your computer and use it in GitHub Desktop.
Save jpt1122/8002519 to your computer and use it in GitHub Desktop.
To compile JavaFX with 3D features you have to get the early access version of the JDK8. At the moment as far as I know there is only a windows support for the 3D features, but a build for Mac and Linux will soon be released. (Luckily enough 3D support also works for a virtualized Windows running on Mac - this is how i got to the screeenshots.) …
package application;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Point3D;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.PointLight;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.shape.Sphere;
import javafx.stage.Stage;
public class SphereAndBox extends Application {
double anchorX, anchorY, anchorAngle;
Image bumpMap = new Image(getClass().getResourceAsStream("panchayt-map.jpg"));
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("SphereAndBox");
System.out.println("@@@@@@@@@@@@@");
PhongMaterial boxMaterial = new PhongMaterial();
boxMaterial.setDiffuseColor(Color.GREEN);
boxMaterial.setSpecularColor(Color.WHITESMOKE);
// BOX
Box box = new Box(400, 400, 400);
box.setMaterial(boxMaterial);
box.setTranslateX(250);
box.setTranslateY(250);
box.setTranslateZ(450);
// Sphere
PhongMaterial sphereMaterial = new PhongMaterial();
sphereMaterial.setDiffuseColor(Color.WHITE);
sphereMaterial.setSpecularColor(Color.LIGHTBLUE);
sphereMaterial.setBumpMap(bumpMap);
Sphere sphere = new Sphere(200);
sphere.setMaterial(sphereMaterial);
sphere.setTranslateX(250);
sphere.setTranslateY(250);
sphere.setTranslateZ(50);
final Group parent = new Group(box, sphere);
parent.setTranslateZ(500);
parent.setRotationAxis(new Point3D(1, 1, 1));
Group root = new Group(parent);
Scene scene = new Scene(root, 500, 500, true);
scene.setCamera(new PerspectiveCamera(false));
scene.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
anchorX = event.getSceneX();
anchorY = event.getSceneY();
anchorAngle = parent.getRotate();
}
});
scene.setOnMouseDragged(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
parent.setRotate(anchorAngle + anchorX - event.getSceneX());
}
});
PointLight pointLight = new PointLight(Color.ANTIQUEWHITE);
pointLight.setTranslateX(15);
pointLight.setTranslateY(-10);
pointLight.setTranslateZ(-100);
root.getChildren().add(pointLight);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
System.setProperty("prism.dirtyopts", "false");
launch(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment