1

My Code

package main;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.media.AudioClip;
import javafx.scene.text.Font;
import javafx.stage.Stage;


public class Main extends Application {
    public void start(Stage primaryStage) throws Exception {
        Font font = Font.loadFont( Main.class.getClassLoader().getResourceAsStream("resources/TRON.TTF"), 10);

        //ImageView image = new ImageView(new Image("file:outside/bell.png"));

       ImageView image = new ImageView(new Image("resources/images/redseven.png"));

       //This works when file is outside src folder
        //AudioClip audio = new AudioClip("file:sounds/win.wav");

        //doesn't work
       //AudioClip audio = new AudioClip("file:resources/sounds/win.wav");

        //doesn't work
        AudioClip audio = new AudioClip("resources/sounds/win.wav");

        audio.play();



        VBox pane = new VBox();
        Label label = new Label("Text");
        label.setStyle("-fx-font-family:'Tron'; -fx-font-size: 50px;-fx-text-fill: red");

        pane.getChildren().addAll(label, image);

        Scene scene = new Scene(pane, 500, 500);
        primaryStage.setScene(scene);
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

My Project directory

enter image description here

Output

Caused by: java.lang.IllegalArgumentException: uri.getScheme() == null! uri == 'resources/sounds/win.wav'

The image url's work when they are outside src folder and inside src folder. When they are outside I have to include "file:" before the url as shown in the code.

Audioclip irls work when they are file is outside src. But doesn't work when it's inside.

EDIT

Doing this works. But I'm not really sure why?

AudioClip audio = new AudioClip("file:src/resources/sounds/win.wav");

I added the src folder also to the url and then added "file:" infront of it. Why doesn't it work with relative path? The image is loaded without any problem.

Enzio
  • 799
  • 13
  • 32
  • Usually you use `SomeClassInstance.getReource().toExternalForm()` for resources. Some IDEs seem to mess this up when resources are not properly added though... – fabian Nov 19 '17 at 10:17
  • @fabian can you please give an example of how to get resources using that method? – Enzio Nov 19 '17 at 10:26

0 Answers0