Importing a Resource

I am trying to use a star image in a program, but I can’t figure out how to import it into my project so I can just say “star.jpg” instead of a whole long file name (which doesn’t even work sometimes).

How can I do this?

Here’s my source line -

ImageIcon ii = new ImageIcon(this.getClass().getResource("star.jpg"));

That should work if star.jpg is somewhere in your classpath. “images/star.jpg” will work if the images/ dir is in your classpath, and so on.

If you are using an IDE like Eclipse or Netbeans, your “src” folder is the root so any paths beginning with a forward slash (or backslash on windows) will start from there. Relative paths will be relative to the Class that you are using.