I am trying to make a simple program that randomly picks a number between 1 and 52 and draws a card based on the number. The card will be displayed by JavaFX. However, whenever I run anything I get a whole slew of errors.
So far, my main thought is that my project cannot find the images. I have created a folder called "resources" and they are stored there, as well as referencing them directly in a folder on the desktop. Still no luck.
Here is the code:
(Imports removed for brevity)...
public class Draw5 extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) throws IOException {
// Create a list of card numbers
ArrayList<Integer> cards = getCards();
// Create a HBox pane
HBox pane = new HBox(5);
pane.setPadding(new Insets(5, 5, 5, 5));
// Add nodes to pane
for (int i = 0; i < 5; i++) {
pane.getChildren().add(new ImageView(new Image("C:\\Users\\Matthew\\Desktop\\card\\" +
cards.get(i) + ".png")));
}
// Create a scene and place it in the stage
Scene scene = new Scene(pane);
primaryStage.setTitle("Exercise_14_03"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
/** Returns a list with numbers 1-52 stored in random order */
private ArrayList<Integer> getCards() {
ArrayList<Integer> cards = new ArrayList<>();
for (int i = 0; i < 52; i++) {
cards.add(i + 1);
}
java.util.Collections.shuffle(cards);
return cards;
}
public static void main(String[] args) {
Application.launch(args);
}
}
Like I said, I get the funny feeling that the project cannot find the 52 .png images of the cards. If anyone has any suggestions please let me know.
For the record, the cards are randomly chosen, hence why the "cards.get(i) + ".png" "
Here are the errors that I am getting upon launch:
Exception in Application start method
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: Invalid URL: unknown protocol: c
at javafx.graphics/javafx.scene.image.Image.validateUrl(Unknown Source)
at javafx.graphics/javafx.scene.image.Image.<init>(Unknown Source)
at thibodeau14.Draw5.start(Draw5.java:31)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
... 1 more
Caused by: java.net.MalformedURLException: unknown protocol: c
at java.base/java.net.URL.<init>(Unknown Source)
at java.base/java.net.URL.<init>(Unknown Source)
at java.base/java.net.URL.<init>(Unknown Source)
... 12 more