daily pastebin goal
27%
SHARE
TWEET

Earth rotation

a guest Feb 13th, 2018 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import javafx.animation.Animation;
  2. import javafx.animation.Interpolator;
  3. import javafx.animation.RotateTransition;
  4. import javafx.application.Application;
  5. import javafx.beans.binding.Bindings;
  6. import javafx.beans.property.BooleanProperty;
  7. import javafx.beans.property.DoubleProperty;
  8. import javafx.beans.property.SimpleBooleanProperty;
  9. import javafx.beans.property.SimpleDoubleProperty;
  10. import javafx.scene.AmbientLight;
  11. import javafx.scene.Group;
  12. import javafx.scene.Parent;
  13. import javafx.scene.PerspectiveCamera;
  14. import javafx.scene.PointLight;
  15. import javafx.scene.Scene;
  16. import javafx.scene.SceneAntialiasing;
  17. import javafx.scene.SubScene;
  18. import javafx.scene.image.Image;
  19. import javafx.scene.paint.Color;
  20. import javafx.scene.paint.PhongMaterial;
  21. import javafx.scene.shape.Sphere;
  22. import javafx.scene.transform.Rotate;
  23. import javafx.scene.transform.Translate;
  24. import javafx.stage.Stage;
  25. import javafx.util.Duration;
  26.  
  27. /**
  28.  * A sample that demonstrates features of PhongMaterial applied to a 3D Sphere.
  29.  * Provided is a playground to exercise the following properties of material:
  30.  * diffuse map and color, specular map, color and power, bump map,
  31.  * self illumination map.
  32.  */
  33. public class Simple3DSphereApp extends Application {
  34.  
  35.     private Sphere earth;
  36.     private PhongMaterial material;
  37.     private PointLight sun;
  38.     private final DoubleProperty sunDistance = new SimpleDoubleProperty(100);
  39.     private final BooleanProperty sunLight = new SimpleBooleanProperty(true);
  40.     private final BooleanProperty diffuseMap = new SimpleBooleanProperty(true);
  41.     private final BooleanProperty specularMap = new SimpleBooleanProperty(true);
  42.     private final BooleanProperty bumpMap = new SimpleBooleanProperty(true);
  43.     private final BooleanProperty selfIlluminationMap = new SimpleBooleanProperty(true);
  44.  
  45.     public Parent createContent() throws Exception {
  46.  
  47.         Image dImage = new Image(Simple3DSphereApp.class.getResource("earth-d.jpg").toExternalForm());
  48.         Image nImage = new Image(Simple3DSphereApp.class.getResource("earth-n.jpg").toExternalForm());
  49.         Image sImage = new Image(Simple3DSphereApp.class.getResource("earth-s.jpg").toExternalForm());
  50.         Image siImage = new Image(Simple3DSphereApp.class.getResource("earth-l.jpg").toExternalForm());
  51.  
  52.         material = new PhongMaterial();
  53.         material.setDiffuseColor(Color.WHITE);
  54.         material.diffuseMapProperty().bind(
  55.                 Bindings.when(diffuseMap).then(dImage).otherwise((Image) null));
  56.         material.setSpecularColor(Color.TRANSPARENT);
  57.         material.specularMapProperty().bind(
  58.                 Bindings.when(specularMap).then(sImage).otherwise((Image) null));
  59.         material.bumpMapProperty().bind(
  60.                 Bindings.when(bumpMap).then(nImage).otherwise((Image) null));
  61.         material.selfIlluminationMapProperty().bind(
  62.                 Bindings.when(selfIlluminationMap).then(siImage).otherwise((Image) null));
  63.  
  64.         earth = new Sphere(5);
  65.         earth.setMaterial(material);
  66.         earth.setRotationAxis(Rotate.Y_AXIS);
  67.  
  68.  
  69.         // Create and position camera
  70.         PerspectiveCamera camera = new PerspectiveCamera(true);
  71.         camera.getTransforms().addAll(
  72.                 new Rotate(-20, Rotate.Y_AXIS),
  73.                 new Rotate(-20, Rotate.X_AXIS),
  74.                 new Translate(0, 0, -20));
  75.  
  76.         sun = new PointLight(Color.rgb(255, 243, 234));
  77.         sun.translateXProperty().bind(sunDistance.multiply(-0.82));
  78.         sun.translateYProperty().bind(sunDistance.multiply(-0.41));
  79.         sun.translateZProperty().bind(sunDistance.multiply(-0.41));
  80.         sun.lightOnProperty().bind(sunLight);
  81.  
  82.         AmbientLight ambient = new AmbientLight(Color.rgb(1, 1, 1));
  83.  
  84.         // Build the Scene Graph
  85.         Group root = new Group();
  86.         root.getChildren().add(camera);
  87.         root.getChildren().add(earth);
  88.         root.getChildren().add(sun);
  89.         root.getChildren().add(ambient);
  90.  
  91.         RotateTransition rt = new RotateTransition(Duration.seconds(24), earth);
  92.         rt.setByAngle(360);
  93.         rt.setInterpolator(Interpolator.LINEAR);
  94.         rt.setCycleCount(Animation.INDEFINITE);
  95.         rt.play();
  96.  
  97.         // Use a SubScene
  98.         SubScene subScene = new SubScene(root, 400, 300, true, SceneAntialiasing.BALANCED);
  99.         subScene.setFill(Color.TRANSPARENT);
  100.         subScene.setCamera(camera);
  101.  
  102.         return new Group(subScene);
  103.     }
  104.  
  105.     @Override
  106.     public void start(Stage primaryStage) throws Exception {
  107.         primaryStage.setResizable(false);
  108.         Scene scene = new Scene(createContent());
  109.         primaryStage.setScene(scene);
  110.         primaryStage.show();
  111.     }
  112.  
  113.     /**
  114.      * Java main for when running without JavaFX launcher
  115.      */
  116.     public static void main(String[] args) {
  117.         launch(args);
  118.     }
  119. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top