/* * Copyright (c) 2008, 2013 Oracle and/or its affiliates. * All rights reserved. Use is subject to license terms. * * This file is available and licensed under the following license: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the distribution. * - Neither the name of Oracle Corporation nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package ensemble.samples.graphics3d.cube; import javafx.animation.Animation; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Point3D; import javafx.scene.Group; import javafx.scene.Parent; import javafx.scene.PerspectiveCamera; import javafx.scene.Scene; import javafx.scene.SceneAntialiasing; import javafx.scene.SubScene; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.transform.Translate; import javafx.stage.Stage; import javafx.util.Duration; /** * A sample that demonstrates an animated rotation of 3D cubes. * * @sampleName 3D Cubes * @preview preview.png * @see javafx.scene.shape.Box * @see javafx.scene.paint.PhongMaterial * @see javafx.scene.PerspectiveCamera * @see javafx.scene.SubScene * @see javafx.scene.SceneAntialiasing * @see javafx.scene.paint.Color * @see javafx.scene.transform.Rotate * @see javafx.scene.transform.Translate * @see javafx.animation.KeyFrame * @see javafx.animation.KeyValue * @see javafx.animation.Timeline * @see javafx.util.Duration * @conditionalFeatures SCENE3D */ public class CubeApp extends Application { private Timeline animation; public Parent createContent() { final Cube redCube = new Cube(1, Color.RED); redCube.setTranslateX(-2); redCube.rx.setAngle(45); redCube.ry.setAngle(45); final Cube greenCube = new Cube(1, Color.GREEN); greenCube.setTranslateX(2); greenCube.rx.setAngle(45); greenCube.ry.setAngle(45); final Cube orangeCube = new Cube(1, Color.ORANGE); orangeCube.getTransforms().setAll(orangeCube.rx, orangeCube.ry, orangeCube.rz); orangeCube.rx.setAngle(45); orangeCube.ry.setAngle(45); orangeCube.rz.setAxis(new Point3D(-1, 1, 1)); animation = new Timeline(); animation.getKeyFrames().addAll( new KeyFrame(Duration.ZERO, new KeyValue(redCube.ry.angleProperty(), 0d), new KeyValue(greenCube.rx.angleProperty(), 0d), new KeyValue(orangeCube.rz.angleProperty(), 0d)), new KeyFrame(Duration.seconds(1), new KeyValue(redCube.ry.angleProperty(), 360d), new KeyValue(greenCube.rx.angleProperty(), 360d), new KeyValue(orangeCube.rz.angleProperty(), 360d))); animation.setCycleCount(Timeline.INDEFINITE); PerspectiveCamera camera = new PerspectiveCamera(true); camera.getTransforms().add(new Translate(0, 0, -10)); Group root = new Group(); root.getChildren().addAll(redCube, greenCube, orangeCube); Button pauseButton = new Button("pause/play"); pauseButton.setOnAction(new EventHandler() { @Override public void handle(ActionEvent t) { if (animation.getStatus() == Animation.Status.PAUSED) { animation.play(); } else { animation.pause(); } } }); Button button1 = new Button("1"); button1.setOnAction(new EventHandler() { @Override public void handle(ActionEvent t) { greenCube.rx.setAngle(90); redCube.ry.setAngle(90); orangeCube.rz.setAngle(120); } }); Button button2 = new Button("2"); button2.setOnAction(new EventHandler() { @Override public void handle(ActionEvent t) { greenCube.rx.setAngle(180); redCube.ry.setAngle(270); orangeCube.rz.setAngle(240); } }); Button button3 = new Button("3"); button3.setOnAction(new EventHandler() { @Override public void handle(ActionEvent t) { greenCube.rx.setAngle(270); redCube.ry.setAngle(180); orangeCube.rz.setAngle(360); } }); Button button4 = new Button("4"); button4.setOnAction(new EventHandler() { @Override public void handle(ActionEvent t) { greenCube.rx.setAngle(360); redCube.ry.setAngle(360); } }); HBox hbox = new HBox(pauseButton, button1, button2, button3, button4); SubScene subScene = new SubScene(root, 640, 480, true, SceneAntialiasing.BALANCED); subScene.setCamera(camera); return new VBox(hbox, subScene); } public void play() { animation.play(); } @Override public void stop() { animation.pause(); } @Override public void start(Stage primaryStage) throws Exception { primaryStage.setScene(new Scene(createContent())); primaryStage.show(); play(); } /** * Java main for when running without JavaFX launcher * @param args command line arguments */ public static void main(String[] args) { launch(args); } }