0

I'm making a java game using Eclipse. When I export to a runnable jar and try to run the game on different computer the images aren't visible. After checking the web and stack overflow for similar problems I think it has something to do with my using ImageIcon instead of ImageIO. However, I'm not sure how to change my code so that I'm uploading the images with ImageIO instead of ImageIcon.

Here is the method that uses ImageIcon to load the images

void loadImage() {

    ImageIcon earth_image_icon = new ImageIcon("earth2.png");
    earth = earth_image_icon.getImage();

    ImageIcon sun_image_icon = new ImageIcon("sun2.png");
    sun = sun_image_icon.getImage(); 

    ImageIcon asteroid_image_icon = new ImageIcon("asteroid.png");
    asteroid = asteroid_image_icon.getImage();

    ImageIcon bg_image_icon = new ImageIcon("bg_pr.png");
    background = bg_image_icon.getImage();

    ImageIcon shipA_image_icon = new ImageIcon("ship_alpha.png");
    ship_on_asteroid = shipA_image_icon.getImage();

    ImageIcon ship_image_icon = new ImageIcon("ship_beta.png");
    ship_no_thrust = ship_image_icon.getImage();

    ImageIcon shipL_image_icon = new ImageIcon("ship_betaL.png");
    ship_left_thrust = shipL_image_icon.getImage();

    ImageIcon shipR_image_icon = new ImageIcon("ship_betaR.png");
    ship_right_thrust = shipR_image_icon.getImage();

    ImageIcon shipU_image_icon = new ImageIcon("ship_betaU.png");
    ship_up_thrust = shipU_image_icon.getImage();

    ImageIcon shipD_image_icon = new ImageIcon("ship_betaD.png");
    ship_down_thrust = shipD_image_icon.getImage();

    ImageIcon leftarrow_image_icon = new ImageIcon("leftarrow.png");
    leftarrow = leftarrow_image_icon.getImage();

    ImageIcon rightarrow_image_icon = new ImageIcon("rightarrow.png");
    rightarrow = rightarrow_image_icon.getImage();

    ImageIcon downarrow_image_icon = new ImageIcon("downarrow.png");
    downarrow = downarrow_image_icon.getImage();

    ImageIcon uparrow_image_icon = new ImageIcon("uparrow.png");
    uparrow = uparrow_image_icon.getImage();

}

And here is one of the methods that draws the image onto the JPanel as an example

void drawEarth(Graphics g) {

    g.drawImage(earth, earth_x_coordinate, earth_y_coordinate, this);
    Toolkit.getDefaultToolkit().sync();
}

How do I convert to using ImageIO? I've checked out the Oracle documentation but I'm getting lost trying to sort it out and I'm very new to programming and java at that.

Update It's been suggested that this might be the solution my particular problem but I tried the answers given on this post and they didn't work for my case.

Community
  • 1
  • 1
Algebra is Awesome
  • 273
  • 1
  • 2
  • 9
  • Where are the images stored in relationship to the source? – MadProgrammer Mar 19 '15 at 01:32
  • The Images are stored in the project folder not the source folder. I have, however, tried storing the images in the source folder as well as putting the images in a resource folder and adding that folder to the build path. – Algebra is Awesome Mar 19 '15 at 01:33
  • If the images are stored in the project folder, you will need to make sure they are included along with the jar(s) and reside within the context of the execution context (as the search path will be relative from that point). If they are included in the "jar" (bundled/embedded), you will need to use something like `getClass().getResource("...")` to load them. In this case, the path should be an absolute path to the images from the root of the source directory (excluding the `src` folder) – MadProgrammer Mar 19 '15 at 01:36
  • Either case you will need to use something like `ImageIO.read(new File("earth2.png"));` if the images are externalised or `ImageIO.read(getClass().getResource("/earth2.png"));` if they are bundled... – MadProgrammer Mar 19 '15 at 01:37
  • The images are externalized (in the project folder but not the source folder) where in the options for making a runnable jar do I make sure they are included? – Algebra is Awesome Mar 19 '15 at 01:42
  • Not really a user of eclipse, but either place them in the `src` folder or make a `resources` folder and put them in there (but make sure `resources` is included within the build-path) – MadProgrammer Mar 19 '15 at 01:45
  • When I do either of those the images don't show up even when I run it inside of eclipse. – Algebra is Awesome Mar 19 '15 at 01:47
  • Well, the only think I can tell you is, make sure the path to the images is correct and that the images are where you expect them to be and that the you are re-compiling/building the program first before you export it (and you're running the right exported Jar) – MadProgrammer Mar 19 '15 at 01:50
  • possible duplicate of [Loading resources using getClass().getResource()](http://stackoverflow.com/questions/2343187/loading-resources-using-getclass-getresource) – Harald K Mar 19 '15 at 09:50

2 Answers2

1

You will need to include the image resources in your compiled jar. An easy way of doing this if you are using an IDE like eclipse is to create a res or img folder and put your files in there. (Use the methods given here.)

In that case, you probably won't need to use ImageIO. However, if you still want to use ImageIO (which is recommended because of its wider support for formats and better exception handling, as mentioned in the comments), you can do the following:

Icon foo = new ImageIcon(ImageIO.read(getClass().getResource("foo.png")))

Community
  • 1
  • 1
k_g
  • 4,333
  • 2
  • 25
  • 40
  • `ImageIO` reads more image types (out of the box) and throws a nice `IOException` when it all goes wrong, which is a nice way to track down issues ;) – MadProgrammer Mar 19 '15 at 01:49
0

I created a new project in eclipse and copied the class files into the new projects src folder. I then made a resource folder, added it to the build path, created an images package in that resource folder and copied the images into that images package. For some reason this all worked. Thanks everyone for your help

Algebra is Awesome
  • 273
  • 1
  • 2
  • 9