Issue
I'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("resources/player/leftNO.png");
ImageView leftSideView = new ImageView(leftSide);
}
My directory is listed below:
- .idea
- out
- resources
- player
- leftNO.png
- rightNO.png
- player
- src
- sample
- Main.java
- Player.java
- sample
Anyone know why this is happening? Thanks in advance, even if you don'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.
Solution
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("/player/leftNO.png");
ImageView leftSideView = new ImageView(leftSide);
}
This will also allow you app to work from within a jar
Answered By - Tag Howard