Why are images in my IntelliJ IDEA project accessible to some Java classes but aren't to others in JavaFX?

huangapple go评论73阅读模式
英文:

Why are images in my IntelliJ IDEA project accessible to some Java classes but aren't to others in JavaFX?

问题

我正在使用JavaFX制作一个简单的2D游戏。我将一些资源(只是一堆文件夹和图像)加载到项目的一个单独目录中,然后去创建另一个Java类来处理玩家需要的所有内容(移动、按键、加载玩家资源)。但是,没有任何图像正在加载。有人知道是为什么吗?我知道引发问题的代码如下:

```java
class Player
{
    public Player() throws FileNotFoundException 
    {
        System.exit(-1);
    }

    Image leftSide = new Image(new FileInputStream("resources/player/leftNO.png"));
    ImageView leftSideView = new ImageView(leftSide);
}

我的目录结构如下:

  • .idea
  • out
  • resources
    • player
      • leftNO.png
      • rightNO.png
  • src
    • sample
      • Main.java
      • Player.java

有人知道这是为什么吗?即使您没有答案,提前感谢您的帮助。

编辑:我还忘记提到,当我尝试在Main.java类中打开图像时,它能够正常加载。


<details>
<summary>英文:</summary>

I&#39;m working on a simple 2D game in JavaFX, and I loaded some of my assets (just a bunch of folders and images) into my project into their own directory, and went to make another Java class for all the stuff the player needs (movement, keypress, loading player assets), and none of the images are loading. Does anyone know why? I have the code I know is causing the problem below.

class Player
{
public Player() throws FileNotFoundException
{
System.exit(-1);
}

Image leftSide = new Image(new FileInputStream(&quot;resources/player/leftNO.png&quot;);
ImageView leftSideView = new ImageView(leftSide);

}

My directory is listed below:

- .idea
- out
- resources
    - player
        - leftNO.png
        - rightNO.png
- src
    - sample
        - Main.java
        - Player.java

Anyone know why this is happening? Thanks in advance, even if you don&#39;t have an answer.

EDIT: Also, I forgot to mention that when I try to open the image in the Main.java class, it loads fine.

</details>


# 答案1
**得分**: 2

当你说“不加载”时,是指它抛出异常还是空白?

根据情况,这可能起作用:

将你的资源目录标记为源文件夹,然后尝试:
```java
class Player
{
    public Player() throws FileNotFoundException 
    {
        System.exit(-1);
    }

    Image leftSide = new Image(getClass().getResourceAsStream("/player/leftNO.png"));
    ImageView leftSideView = new ImageView(leftSide);
}

这也将允许你的应用在jar包内运行。

英文:

When you say not loading, do you mean that it throws an exception or is blank?
Depending on which, this could work:

Mark your resources directory as a source folder and try

class Player
{
    public Player() throws FileNotFoundException 
    {
        System.exit(-1);
    }

    Image leftSide = new Image(getClass().getResourceAsStream(&quot;/player/leftNO.png&quot;);
    ImageView leftSideView = new ImageView(leftSide);
}

This will also allow you app to work from within a jar

huangapple
  • 本文由 发表于 2020年4月10日 03:12:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/61128561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定