0

I'm currently working on a poker application for fun and I've encountered another issue when trying to work with imageviews.

Right now my structure for my images is the following: JavaFXApplication1\src\images

that "images" folder contains two more folders, one being "cards" and the other "playerImages". Here is a screenshot of it also:project structure

Currently I am using a document relative path within scene builder to initially load the images for each of the cards and players. for each of the cards, the imagepath initially is "@..\images\cards\back.png" which is set in scenebuilder. this method works perfectly fine when launching the application. however, for the player images next to the cards, i have "@..\images\playerImages\player1.png" for example and this doesnt work for some reason.

In scenebuilder the "player1.png" image shows up where it should, but disappears when i launch everything.

I read through Images not showing on launch by the main, but showed on Scene builder but Im not not sure the solution for that is the same for me, as my images are in my source folder.

I tried using the fx:id and setting the image manually in my controller class, but was receiving an error stating that my input stream was null.

I also tried just adding "player1.png" to my root images folder and using that path, and that still didnt work. yet, when I change the player1 image path to anything in the cards folder it works. I dont understand why it works for the images in the cards folder but not in the playerImages folder. Both of those folders are in the same location.

I apologize if this was already answered somewhere else and i am too oblivious to see it, but any insight into this seemingly simple problem would be greatly appreciated.

user10113618
  • 23
  • 1
  • 1
  • 5

1 Answers1

0

if you have a similar project structure:

--main
  --java
    --src
    --resources
      --images
       --cards
         back.png
       --players
         player1.png

you can set images from resources in your java-code. But you need to use getResources()

For example:

@FXML
private ImageView imageView;

Image image = new Image(getClass().getResourceAsStream("/images/players/player1.png"));
imageView = new ImageView(image);

Of course directory "resources: shuild be marked as resources root

Egor
  • 1,334
  • 8
  • 22
  • Thanks for the response. I have been using this method originally actually. I dont know why, but it only seems to work with certain images. I downloaded a bunch of different pictures from google to use alternatively, and only one of them works. They are all .pngs but vary in size and width. I tried both bigger and smaller images from the one that does work and I cannot figure out why they arent working as well. What is so special about the ones that do work? – user10113618 Oct 02 '18 at 01:02
  • When I pass the path to an image that works the code executes and changes the picture, but when I try any of the other images I receive a **java.lang.NullPointerException: Input stream must not be null** error, yet the filename I am passign is correct, and in scenebuilder, the pictures are there. Only upon execution do they seem to disappear. – user10113618 Oct 02 '18 at 01:04
  • I think I figured it out. From within netbeans, after manually expanding the images folder, I noticed the filename that works is stored as "player1.png" where every other image is stored as "player#.PNG". I didnt know that whether the filename **extension** was in upper or lower case mattered, as I have never encountered this issue before. – user10113618 Oct 02 '18 at 01:16