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.media.audioclip; 33 34 import javafx.application.Application; 35 import javafx.event.EventHandler; 36 import javafx.scene.Group; 37 import javafx.scene.Parent; 38 import javafx.scene.Scene; 39 import javafx.scene.effect.Light; 40 import javafx.scene.effect.Lighting; 41 import javafx.scene.input.MouseEvent; 42 import javafx.scene.layout.StackPane; 43 import javafx.scene.media.AudioClip; 44 import javafx.scene.paint.Color; 45 import javafx.scene.shape.Rectangle; 46 import javafx.stage.Stage; 47 48 /** 49 * A sample that demonstrates the basics of AudioClips. 50 * 51 * @sampleName Audio Clip 52 * @preview preview.png 53 * @docUrl http://docs.oracle.com/javase/8/javafx/media-tutorial/overview.htm#JFXMD101 Using JavaFX Media 54 * @see javafx.scene.layout.StackPane 55 * @see javafx.scene.media.AudioClip 56 * @highlight 57 * @conditionalFeatures WEB, MEDIA 58 * 59 * @related /Media/Alpha Media Player 60 * @related /Media/Overlay Media Player 61 * @related /Media/Streaming Media Player 62 * @related /Graphics 3d/Xylophone 63 */ 64 public class AudioClipApp extends Application { 65 public Parent createContent() { 66 final double xStart = 12; 67 final double xOffset = 30.0; 68 final double barWidth = 22.0; 69 70 Rectangle r1 = new Rectangle(0, 15, barWidth * 11.5, 10); 71 r1.setFill(new Color(0.2, 0.12, 0.1, 1.0)); 72 Rectangle r2 = new Rectangle(0, -25, barWidth * 11.5, 10); 73 r2.setFill(new Color(0.2, 0.12, 0.1, 1.0)); 74 75 final Group content = new Group( 76 r1, 77 r2, 78 createKey(Color.PURPLE, xStart + 0 * xOffset, barWidth, 1), 79 createKey(Color.BLUEVIOLET, xStart + 1 * xOffset, barWidth, 2), 80 createKey(Color.BLUE, xStart + 2 * xOffset, barWidth, 3), 81 createKey(Color.GREEN, xStart + 3 * xOffset, barWidth, 4), 82 createKey(Color.GREENYELLOW, xStart + 4 * xOffset, barWidth, 5), 83 createKey(Color.YELLOW, xStart + 5 * xOffset, barWidth, 6), 84 createKey(Color.ORANGE, xStart + 6 * xOffset, barWidth, 7), 85 createKey(Color.RED, xStart + 7 * xOffset, barWidth, 8)); 86 87 // A StackPane by default centers its children, here we extend it to 88 // scale the content to fill the StackPane first. 89 StackPane root = new StackPane() { 90 @Override protected void layoutChildren() { 91 // find biggest scale that will fit while keeping proportions 92 double scale = Math.min( 93 (getWidth()-20) / content.getBoundsInLocal().getWidth(), 94 (getHeight()-20) / content.getBoundsInLocal().getHeight() 95 ); 96 content.setScaleX(scale); 97 content.setScaleY(scale); 98 super.layoutChildren(); 99 } 100 }; 101 root.getChildren().add(content); 102 return root; 103 } 104 105 private static final String[] urls = { 106 "/ensemble/samples/shared-resources/Note1.wav", 107 "/ensemble/samples/shared-resources/Note2.wav", 108 "/ensemble/samples/shared-resources/Note3.wav", 109 "/ensemble/samples/shared-resources/Note4.wav", 110 "/ensemble/samples/shared-resources/Note5.wav", 111 "/ensemble/samples/shared-resources/Note6.wav", 112 "/ensemble/samples/shared-resources/Note7.wav", 113 "/ensemble/samples/shared-resources/Note8.wav" 114 }; 115 116 public static Rectangle createKey(Color color, double x, 117 double width, int note) { 118 119 double height = 100 - ((note - 1) * 5); 120 // create a audio clip that this key will play 121 final AudioClip barNote = new AudioClip( 122 AudioClipApp.class.getResource(urls[note - 1]).toExternalForm()); 123 // create the rectangle that draws the key 124 Rectangle rectangle = new Rectangle(x, -(height / 2), width, height); 125 rectangle.setFill(color); 126 Lighting lighting = new Lighting(new Light.Point(-20, -20, 100, Color.WHITE)); 127 lighting.setSurfaceScale(1); 128 rectangle.setEffect(lighting); 129 rectangle.setOnMousePressed((MouseEvent me) -> { 130 barNote.play(); 131 }); 132 return rectangle; 133 } 134 135 @Override public void start(Stage primaryStage) throws Exception { 136 primaryStage.setScene(new Scene(createContent())); 137 primaryStage.show(); 138 } 139 140 /** 141 * Java main for when running without JavaFX launcher 142 * @param args command line arguments 143 */ 144 public static void main(String[] args) { launch(args); } 145 }