1 /*
   2  * Copyright (c) 2008, 2016, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 package ensemble.samples.graphics3d.xylophone;
  33 
  34 import javafx.animation.Interpolator;
  35 import javafx.animation.KeyFrame;
  36 import javafx.animation.KeyValue;
  37 import javafx.animation.Timeline;
  38 import javafx.application.Application;
  39 import javafx.scene.Group;
  40 import javafx.scene.SceneAntialiasing;
  41 import javafx.scene.Parent;
  42 import javafx.scene.PerspectiveCamera;
  43 import javafx.scene.Scene;
  44 import javafx.scene.SubScene;
  45 import javafx.scene.input.MouseEvent;
  46 import javafx.scene.media.AudioClip;
  47 import javafx.scene.paint.Color;
  48 import javafx.scene.paint.PhongMaterial;
  49 import javafx.scene.shape.Box;
  50 import javafx.stage.Stage;
  51 import javafx.util.Duration;
  52 
  53 /**
  54  * A sample that demonstrates a xylophone made of 3D cubes. It is animated and
  55  * plays sounds when clicked.
  56  *
  57  * @sampleName Xylophone
  58  * @preview preview.png
  59  * @docUrl http://docs.oracle.com/javase/8/javafx/graphics-tutorial/javafx-3d-graphics.htm#JFXGR256 JavaFX 3D Graphics
  60  * @see javafx.scene.PerspectiveCamera
  61  * @see javafx.scene.SceneAntialiasing
  62  * @see javafx.scene.SubScene
  63  * @see javafx.scene.input.MouseEvent
  64  * @see javafx.scene.media.AudioClip
  65  * @see javafx.scene.paint.PhongMaterial
  66  * @see javafx.scene.shape.Box
  67  * @see javafx.scene.transform.Rotate
  68  * @see javafx.scene.transform.Scale
  69  * @conditionalFeatures SCENE3D
  70  *
  71  * @related /Graphics 3d/3D Box
  72  * @related /Graphics 3d/3D Cubes
  73  * @related /Graphics 3d/3D Sphere
  74  * @related /Graphics 3d/3D Sphere System
  75  */
  76 public class XylophoneApp extends Application {
  77 
  78     private Timeline animation;
  79     private Timeline animation2;
  80 
  81     private static final String[] urls = {
  82         "/ensemble/samples/shared-resources/Note1.wav",
  83         "/ensemble/samples/shared-resources/Note2.wav",
  84         "/ensemble/samples/shared-resources/Note3.wav",
  85         "/ensemble/samples/shared-resources/Note4.wav",
  86         "/ensemble/samples/shared-resources/Note5.wav",
  87         "/ensemble/samples/shared-resources/Note6.wav",
  88         "/ensemble/samples/shared-resources/Note7.wav",
  89         "/ensemble/samples/shared-resources/Note8.wav"
  90     };
  91 
  92     public Parent createContent() {
  93         Xform sceneRoot = new Xform();
  94         sceneRoot.rx.setAngle(45.0);
  95         sceneRoot.ry.setAngle(30.0);
  96         sceneRoot.setScale(2 * 1.5);
  97 
  98         Group rectangleGroup = new Group();
  99 
 100         double xStart = -110.0;
 101         double xOffset = 30.0;
 102         double yPos = 25.0;
 103         double barWidth = 22.0;
 104         double barDepth = 7.0;
 105 
 106         // Base1
 107         Box base1Cube = new Box(barWidth * 11.5, barDepth * 2.0, 10.0);
 108         base1Cube.setMaterial(new PhongMaterial(new Color(0.2, 0.12, 0.1, 1.0)));
 109         base1Cube.setTranslateX(xStart + 128);
 110         base1Cube.setTranslateZ(yPos + 20.0);
 111         base1Cube.setTranslateY(11.0);
 112 
 113         // Base2
 114         Box base2Cube = new Box(barWidth * 11.5, barDepth * 2.0, 10.0);
 115         base2Cube.setMaterial(new PhongMaterial(new Color(0.2, 0.12, 0.1, 1.0)));
 116         base2Cube.setTranslateX(xStart + 128);
 117         base2Cube.setTranslateZ(yPos - 20.0);
 118         base2Cube.setTranslateY(11.0);
 119 
 120         Box[] barCubes = new Box[8];
 121         Color[] colors = {
 122             Color.PURPLE, Color.BLUEVIOLET, Color.BLUE, Color.GREEN,
 123             Color.GREENYELLOW, Color.YELLOW, Color.ORANGE, Color.RED
 124         };
 125         for (int i = 0; i < barCubes.length; i++) {
 126             String url = getClass().getResource(urls[i]).toString();
 127             final AudioClip barNote = new AudioClip(url);
 128 
 129             barCubes[i] = new Box(barWidth, barDepth, 100.0 - (i * 5.0));
 130             barCubes[i].setTranslateX(xStart + ((1 + i) * xOffset));
 131             barCubes[i].setTranslateZ(yPos);
 132             barCubes[i].setMaterial(new PhongMaterial(colors[i]));
 133             barCubes[i].setOnMousePressed((MouseEvent me) -> {
 134                 barNote.play();
 135             });
 136         }
 137 
 138         rectangleGroup.getChildren().addAll(base1Cube, base2Cube);
 139         rectangleGroup.getChildren().addAll(java.util.Arrays.asList(barCubes));
 140         sceneRoot.getChildren().add(rectangleGroup);
 141 
 142         animation = new Timeline();
 143         animation.getKeyFrames().addAll(new KeyFrame(Duration.ZERO,
 144                 new KeyValue(sceneRoot.ry.angleProperty(), 390d,
 145                 Interpolator.TANGENT(Duration.seconds(0.5), 390d,
 146                 Duration.seconds(0.5), 390d))),
 147                 new KeyFrame(Duration.seconds(2),
 148                 new KeyValue(sceneRoot.ry.angleProperty(), 30d,
 149                 Interpolator.TANGENT(Duration.seconds(0.5), 30d,
 150                 Duration.seconds(0.5), 30d))));
 151 
 152         animation2 = new Timeline();
 153         animation2.getKeyFrames().addAll(new KeyFrame(Duration.ZERO,
 154                 new KeyValue(sceneRoot.rx.angleProperty(), 60d,
 155                 Interpolator.TANGENT(Duration.seconds(1.0), 60d))),
 156                 new KeyFrame(Duration.seconds(4),
 157                 new KeyValue(sceneRoot.rx.angleProperty(), 80d,
 158                 Interpolator.TANGENT(Duration.seconds(1.0), 80d))),
 159                 new KeyFrame(Duration.seconds(8),
 160                 new KeyValue(sceneRoot.rx.angleProperty(), 60d,
 161                 Interpolator.TANGENT(Duration.seconds(1.0), 60d))));
 162         animation2.setCycleCount(Timeline.INDEFINITE);
 163 
 164         PerspectiveCamera camera = new PerspectiveCamera();
 165 
 166         SubScene subScene = new SubScene(sceneRoot, 780 * 1.5, 380 * 1.5,
 167                                          true, SceneAntialiasing.BALANCED);
 168         subScene.setCamera(camera);
 169 
 170         sceneRoot.translateXProperty().bind(subScene.widthProperty().divide(2.2));
 171         sceneRoot.translateYProperty().bind(subScene.heightProperty().divide(1.6));
 172 
 173         return new Group(subScene);
 174     }
 175 
 176     public void play() {
 177         animation.play();
 178         animation2.play();
 179     }
 180 
 181     @Override
 182     public void stop() {
 183         animation.pause();
 184         animation2.pause();
 185     }
 186 
 187     @Override
 188     public void start(Stage primaryStage) throws Exception {
 189         primaryStage.setScene(new Scene(createContent()));
 190         primaryStage.show();
 191         play();
 192     }
 193 
 194     /**
 195      * Java main for when running without JavaFX launcher
 196      * @param args command line arguments
 197      */
 198     public static void main(String[] args) {
 199         launch(args);
 200     }
 201 }