0

So I have a playable game where there are images that are stored in my flashdrive. However, after exporting the jar file to my desktop, and unplugging my flashdrive, the images disappear in my game. If that is so, how are the people who play my game going to see my images if I put my jar file online for them to download? In my code I upload the images to my code like this:

static Image LimageFishy = new ImageIcon("F://Picture//Lfish.png").getImage();
static Image RimageFishy = new ImageIcon("F://Picture//Rfish.png").getImage();
Skillet
  • 237
  • 1
  • 4
  • 16

1 Answers1

0

This is a method you could use:

private ImageIcon createIcon(String path) {
    BufferedImage image = ImageIO.read(getClass().getResourceAsStream(path));
    ImageIcon icon = new ImageIcon(image);
    return icon;
}

Make sure that you put the image in the correct path though. For instance, if you have a package "images" and it contains a file "Rfish.png", then path needs to be /images/Rfish.png.

This is a great resource: Loading Images Using getResource

Notes:

  • the / at the start of the path; it is easily missed).
  • the names inside a jar are case-sensitve: Rfish.png isn't the same as rfish.png
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165