2017-01-04 2 views
-1

JavaFX BlueJ 프로젝트가 있습니다. 당신은 내가 같은 디렉토리에 Gui.javamap.png 파일이 있는지 볼 수 있듯이 다음은 간단한 디렉토리 구조BlueJ 이미지를 찾을 수 없음

Project Structure

입니다.

Exception in Application start method 
java.lang.RuntimeException: Exception in Application start method 
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182) 
    at com.sun.javafx.application.LauncherImpl$$Lambda$7/22058848.run(Unknown Source) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found 
    at javafx.scene.image.Image.validateUrl(Image.java:1099) 
    at javafx.scene.image.Image.<init>(Image.java:608) 
    at src.Gui.start(Gui.java:46) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863) 
    at com.sun.javafx.application.LauncherImpl$$Lambda$57/5668924.run(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326) 
    at com.sun.javafx.application.PlatformImpl$$Lambda$53/812813.run(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295) 
    at com.sun.javafx.application.PlatformImpl$$Lambda$55/19277439.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294) 
    at com.sun.javafx.application.PlatformImpl$$Lambda$54/26973244.run(Unknown Source) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101) 
    at com.sun.glass.ui.win.WinApplication$$Lambda$43/21354624.run(Unknown Source) 
    ... 1 more 
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found 
    at javafx.scene.image.Image.validateUrl(Image.java:1091) 
    ... 16 more 

이미지를 찾을 수 없습니다 다음은 내 코드

@Override 
public void start(Stage primaryStage) throws Exception { 

    primaryStage.setTitle("Dragon Castle"); 

    Game game = new Game(); 
    TextArea logArea = new TextArea(); 

    VBox vbox = new VBox(10); 

    // Create maps 

    Canvas canvas = new Canvas(770, 630); 

    Image map = new Image("map.png"); // Here it is giving error 

    GraphicsContext gc = canvas.getGraphicsContext2D(); 

    // Insert controls 

    VBox controlsVbox = new VBox(5); 
    controlsVbox.setAlignment(Pos.CENTER); 

    HBox buttonHboxR1 = new HBox(5); 
    buttonHboxR1.setAlignment(Pos.CENTER); .... Other code 

오류입니다. 나는 이클립스에서 같은 코드를 시도했다. 또한 map.png을 자원 폴더에 넣었지만 여전히 BlueJ에서 작동하지 않습니다. Image documentation에서

답변

0

: 당신은 간단한 경로를 통과하기 때문에

All URLs supported by URL can be passed to the constructor. If the passed string is not a valid URL, but a path instead, the Image is searched on the classpath in that case.

따라서는 Image 생성자는 클래스 패스에 그 상대를 해결하려고 시도합니다. 스택 추적을 보면 Gui 클래스는 src (정말로 ???)이라는 패키지에있는 것으로 보이며 이미지가 동일한 패키지에 있습니다. 따라서 이미지 경로는 src/map.png이어야합니다.

Image map = new Image(getClass().getResource("map.png").toExternalForm()); 
+0

이 여전히 당신에게 오류를 제공하는 경우, 빌드를 확인해야합니다 :

현재 클래스와 같은 패키지에서 이미지를 검색하는 더 좋은 방법은 리소스로의 클래스 객체에서 URL을 얻는 것입니다 폴더 (원본 폴더 아님)에는 실제로 이미지가 예상 위치에 있습니다. BlueJ에 대한 지식이 없으므로 어떻게 구성 할 지 모르겠습니다. –

+0

음, 코드를 사용해 보았습니다. 작동하지 않습니다. 'bin' 폴더 (모든 클래스 파일들)와'map.png'가 있습니다. – Junaid

+0

@Junaid 그래서'bin'에'src/Gui.class'와'src/map.png'가 있습니까? 스크린 샷의 같은 폴더에'.java' 파일과'.class' 파일이있는 이유를 모르겠습니다. –