3

I am developing a little game where I use some pictures for the sprites etc etc. It works just fine when I load it from the disc like this

Image image = new Image("C:\\AppleMan.png");

but how can I load it from a foloder within the project. I am using eclipse as IDE and the language is Java :)

I took a screenshot of a sample project so you can see how I have importet the picture

Le Image

So I want to load the picture from that resource folder like this pseudo code

Image image = getResource("Resources/AppleMan.png");

but that simply doesn't work.

Any help appreciated :)

chrs
  • 5,906
  • 10
  • 43
  • 74
  • Possible duplicate - http://stackoverflow.com/questions/7014123/reading-an-image-in-netbeans/7014177#7014177 – mre Feb 08 '12 at 16:30

2 Answers2

2

1) You should add Resources folder to classpath

2) You should locate file absolutely, i.e. "/Resources/AppleMan.png"

P.S.

3) Sorry, also note that getResource returns URL: http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getResource%28java.lang.String

Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
-3

You're passing a relative path when you were passing a absolute path before. Add the path of the Eclipse workspace.

For example, if your workspace is at C:\Workspace, you need to put

Image image = getResource("C:\\Workspace\\HowToGetResources\\Resources\\AppleMan.png");
m0skit0
  • 25,268
  • 11
  • 79
  • 127