Unable to load picture from resource

enter image description hereSo I am trying to load a image file from a resource so that when I export my application into a jar file, it could be used.

Now I have read the documentation here: http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html

But I still don’t get it… How can I implement this in my code?

Heres a picture so you could also see my application tree:
Image

I can’t change the getImage to getResources as it needs something and I dont know what.

EDIT: this is how the image line looks like right now: Image pic = Toolkit.getDefaultToolkit().getImage("src/klassid/Pictures/mario.png");

This will load it from resources.

CODE:

  package klassid;

import java.awt.Toolkit;
import java.awt.Image;
import java.awt.Graphics;
import java.net.URL;



public class Hero {
    static Main main;
    java.awt.Image pic = javax.imageio.ImageIO.read(Hero.class.getClassLoader().getResourceAsStream("Pictures/mario.png"));
    //Image pic = Toolkit.getDefaultToolkit().getImage(this.getClass().getClassLoader().getResource("klassid/Pictures/mario.png"));
    double height = 0, speed = 4;
    public static final double gravity = 9.81;
    double x = 25;
    int y, a;
    boolean left = false, right = false, up = false;

    public Hero(int x, int y){
        this.x = x;
        this.y = y;
    }

    public void render(Graphics g, Main pohiKlass){
        g.drawImage(pic, (int) (x), (int) (500-(height*100)), 50, 50, pohiKlass);
    }

    public void the_jump() {
        long previous = 0, start = 0;

        while(true){
            start = System.nanoTime();  
            if(previous != 0 && up){
                double delta = start - previous;

                height = (height + (delta/1000000000) * speed);        
                speed -= (delta/1000000000)  * gravity;
                y = (int) (500-(height * 100));
                //System.out.println(y);
            }  
            if(left)
                x-= 3;
            if(right)
                x+= 3; 
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(height < 0){
                height = 0;
                speed = 4; 
                up = false;
            }      
            previous = start;
        }
    }
    public void liigu(int i){
        if(i == 38 || i == 87)
            up=true;
        if(i == 37 || i == 65)
            left=true;
        if(i == 39 || i == 68)
            right=true;
    }
    public void liigu2(int i){
        if(i == 37 || i == 65)
            left=false;
        if(i == 39 || i == 68)
            right=false;
    }
    public void getY(){
        a = y;
    }
}


3 Solutions
Solution

You should try moving your ‘Pictures’ folder by putting it directly into the Project folder.

Your folder layout should look something like this.

-Kodune-mang

       -src
          -klassid

      -Pictures
          -mario.png

You can reference the ‘mario.png’ image by using the path.

Pictures/mario.png

Since the file path system in java (or at least eclipse) will automatically start in the main project directory. It will be able to view the ‘src’ folder and its sub components, as well as the new ‘Pictures’ folder and its sub components.

Solution

Load it as a resource, why not?:

java.awt.Image pic = javax.imageio.ImageIO.read(Hero.class.getClassLoader().getResourceAsStream("klassid/Pictures/mario.png"));

Solution

The Toolkit.getImage() have 2 methods, you should use the second one with URL as parameter and use your ClassLoader to get the resource and pass it on

  // I am assuming that your picture is in klassid/Pictures folder inside the jar file
  Image pic = Toolkit.getDefaultToolkit().getImage(this.getClass().getClassLoader().getResource("klassid/Pictures/mario.png"));

I like to do thing step by step, so, first test the class loader to see if you can get the image separate from the Toolkit by:

URL imgUrl = this.getClass().getClassLoader().getResource("klassid/Pictures/mario.png");

and simply do the System.out.println(this.getClass().getClassLoader().getResource(“”)) to see the root path and follow that to your image/resources. One thing that you should understand about getResource() is that it is as a mean to get resource at runtime relative to the class path. In eclipse is what you set either bin or bin/classes. And in the .jar file it is the top of the file structure.