0

For example, if I'm using this way for getting the images:

        InputStream imgpacman2up = Tablero.class.getResourceAsStream("pacmanup1.png");
        BufferedImage pacman2upImg = ImageIO.read(imgpacman2up);
        pacman2arriba = new ImageIcon(pacman2upImg).getImage();

How would the route of the image ("In this case "pacmanup1.png") would be modified if the images are saved in a folder from another package of the project?

Thanks!

Rodrigo Peniche
  • 21
  • 1
  • 1
  • 7

3 Answers3

0

Quoting javadoc of getResourceAsStream():

Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:

  • If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
  • Otherwise, the absolute name is of the following form:

    modified_package_name/name 
    

    Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').

Andreas
  • 154,647
  • 11
  • 152
  • 247
0

Supposing that your image is in the package aaa.bbb.ccc use the following /aaa/bbb/ccc.

Dakshinamurthy Karra
  • 5,353
  • 1
  • 17
  • 28
0

If it is within some folder named "resources" in src, then the below code accesses the image from your java class.

InputStream imgpacman2up = SampleTable.class.getResourceAsStream("/resources/pacmanup1.png");
        BufferedImage pacman2upImg = ImageIO.read(imgpacman2up);
        pacman2arriba = new ImageIcon(pacman2upImg).getImage();

If it is within some other package then the below code accesses the image from your java class.

InputStream imgpacman2up1 = SampleTable.class.getResourceAsStream("/com/abc/ab/pacmanup1.png");
    BufferedImage pacman2upImg1 = ImageIO.read(imgpacman2up1);
    pacman2arriba1 = new ImageIcon(pacman2upImg1).getImage();
S.B
  • 678
  • 1
  • 5
  • 11