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
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
|
1 /*
2 * Copyright (c) 2008, 2017, 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 java.io.File;
35 import java.net.HttpURLConnection;
36 import java.net.URI;
37 import java.net.URL;
38 import javafx.application.Application;
39 import javafx.scene.Group;
40 import javafx.scene.Parent;
41 import javafx.scene.Scene;
42 import javafx.scene.effect.Light;
43 import javafx.scene.effect.Lighting;
44 import javafx.scene.input.MouseEvent;
45 import javafx.scene.layout.StackPane;
46 import javafx.scene.media.AudioClip;
47 import javafx.scene.paint.Color;
48 import javafx.scene.shape.Rectangle;
49 import javafx.stage.Stage;
50
51 /**
52 * A sample that demonstrates the basics of AudioClips.
53 *
54 * @sampleName Audio Clip
55 * @preview preview.png
56 * @docUrl http://docs.oracle.com/javase/8/javafx/media-tutorial/overview.htm#JFXMD101 Using JavaFX Media
57 * @see javafx.scene.layout.StackPane
58 * @see javafx.scene.media.AudioClip
88 createKey(Color.RED, xStart + 7 * xOffset, barWidth, 8));
89
90 // A StackPane by default centers its children, here we extend it to
91 // scale the content to fill the StackPane first.
92 StackPane root = new StackPane() {
93 @Override protected void layoutChildren() {
94 // find biggest scale that will fit while keeping proportions
95 double scale = Math.min(
96 (getWidth()-20) / content.getBoundsInLocal().getWidth(),
97 (getHeight()-20) / content.getBoundsInLocal().getHeight()
98 );
99 content.setScaleX(scale);
100 content.setScaleY(scale);
101 super.layoutChildren();
102 }
103 };
104 root.getChildren().add(content);
105 return root;
106 }
107
108 /*
109 * See JDK-8177428 for an explanation of why this is here.
110 */
111 private static AudioClip getNoteClip(String name) {
112 // First look for the clips in a directory next to our jar file
113 try {
114 // Get a URI to this class file
115 URI baseURI = AudioClipApp.class.getResource("AudioClipApp.class").toURI();
116
117 // If we have a jar URL, get the embedded http or file URL
118 // and trim off the internal jar path, this will leave us
119 // with a URL to the jar file
120 if (baseURI.getScheme().equals("jar")) {
121 String basePath = baseURI.getSchemeSpecificPart();
122 if (basePath.contains("!/")) {
123 basePath = basePath.substring(0, basePath.indexOf("!/"));
124 }
125 baseURI = new URI(basePath);
126 }
127
128 URL noteURL = baseURI.resolve("resources/"+name).toURL();
129
130 // check if the resource exists, then try to load it
131 if (noteURL.getProtocol().equals("http")) {
132 HttpURLConnection urlCon = (HttpURLConnection)noteURL.openConnection();
133 urlCon.setRequestMethod("HEAD");
134 urlCon.connect();
135 if (urlCon.getResponseCode() != HttpURLConnection.HTTP_OK) {
136 noteURL = null;
137 }
138 urlCon.disconnect();
139 } else if (noteURL.getProtocol().equals("file")) {
140 File f = new File(noteURL.getPath());
141 if (!f.exists() || !f.isFile()) {
142 noteURL = null;
143 }
144 } else {
145 // unsupported protocol
146 noteURL = null;
147 }
148 if (noteURL != null) {
149 return new AudioClip(noteURL.toExternalForm());
150 }
151 } catch (Exception e) {} // fail gracefully
152
153 // Fall back on the embedded clips
154 return new AudioClip(
155 AudioClipApp.class.getResource("/ensemble/samples/shared-resources/"+name).toExternalForm());
156 }
157
158 public static Rectangle createKey(Color color, double x,
159 double width, int note) {
160
161 double height = 100 - ((note - 1) * 5);
162 // create a audio clip that this key will play
163 final AudioClip barNote = getNoteClip("Note"+note+".wav");
164
165 // create the rectangle that draws the key
166 Rectangle rectangle = new Rectangle(x, -(height / 2), width, height);
167 rectangle.setFill(color);
168 Lighting lighting = new Lighting(new Light.Point(-20, -20, 100, Color.WHITE));
169 lighting.setSurfaceScale(1);
170 rectangle.setEffect(lighting);
171 rectangle.setOnMousePressed((MouseEvent me) -> {
172 barNote.play();
173 });
174 return rectangle;
175 }
176
177 @Override public void start(Stage primaryStage) throws Exception {
178 primaryStage.setScene(new Scene(createContent()));
179 primaryStage.show();
180 }
181
182 /**
183 * Java main for when running without JavaFX launcher
184 * @param args command line arguments
|