what is the correct method to loads images

hello,
i used to do this when i want to load an image into the game


String path = "D:/My Graphics/hero.png";
ImageIcon ii = new ImageIcon(path);
Image image = ii.getImage();

but when i tried to export the project into a Runnable Jar and test it in another pc, everything disappeared, and it make sense cause the other computers doesn’t have that path
so i did some research and tried this :



String img = "graphics/hero.png";
Image image;
URL path = this.getClass().getResource(img);
if (path != null) {
	        ImageIcon ii = new ImageIcon(path, "hero");
	        image = ii.getImage();
	    } else {
	        System.err.println("Couldn't find file: " + path);
	        
	    }


it always print the error and i don’t get my image

this is a screen of my work space

http://s22.postimg.org/ieyqokbe9/Capture.png

thank you

BufferedImage img = ImageIO.read(YourClass.class.getResourceAsStream("/some/resource.png"));

This will load image from classpath (from inside the jar the application is run)

If you use eclipse, you should select “graphics” folder and add it as source file. (you then can refer to them with path as “/image.png”)
later, if you change something in graphics folder, don’t forget to refresh workspace in eclipse.

this doesn’t give me any exception errors, but i don’t get my image in the Board, i also added the graphic folder as a source file

This should work, this will work if your using applet or not.


BufferedImage bImage;

try {
			URL url = this.getClass().getResource("graphics/hero.png");
			this.bImage = ImageIO.read(url);
		} catch (IOException e) {
			e.printStackTrace();
		}

^
thank you !!!