0

I am working on trying make a GUI in JavaFX and i cant add any images because i keep getting errors.Everytime I add an image in my Image object it keeps sending this error. I tried using file: with my path but it ends up not displaying the image at all.I am using Intellij IDE with the latest Java and JavaFX SDK here is my code:

package com.example.new_project;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.image.ImageView;
import javafx.scene.shape.Circle ;
import javafx.scene.shape.Line ;
import javafx.scene.shape.Polygon ;
import javafx.scene.shape . Rectangle ;
import javafx.scene.text . Font ;
import javafx.scene.text.Text ;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        Group root = new Group();
      Scene scene = new Scene(root,600,600,Color.rgb(38, 50, 56));
      Image icon = new Image("logo-Resize.png");
      stage.getIcons().add(icon);
      stage.setFullScreen(false);
      stage.setTitle("DEEZ");
  Text text = new Text();
  text.setText("Welcome Human");
  text.setX(100);
  text.setY(75);
  text.setFont(Font.font("Poppins",30));
  text.setFill(Color.WHITESMOKE);

  Image image = new Image("src/icon.svg");
  ImageView imageView = new ImageView(image);
  imageView.setX(200);
  imageView.setY(250);

  root.getChildren().add(text);
  root.getChildren().add(imageView);
  stage.setScene(scene);
  stage.show();
}

public static void main(String[] args) {

    launch();
}

}

here is the error i keep getting:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
    at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1107)
    at javafx.graphics/javafx.scene.image.Image.<init>(Image.java:617)
    at com.example.new_project/com.example.new_project.HelloApplication.start(HelloApplication.java:27)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    ... 1 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
    at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1099)
    ... 11 more
Exception running application com.example.new_project.HelloApplication

Process finished with exit code 1
  • I don't see any support for SVG in [`Image`](https://openjfx.io/javadoc/17/javafx.graphics/javafx/scene/image/Image.html), but you can set the content like [this](https://stackoverflow.com/a/32116938/230513). – trashgod Sep 28 '22 at 22:42

0 Answers0