0

I was reading in images that were in the same folder as my programs like this

public void GetImages() { imgMonster = new ImageIcon(Astro_n_Monster.class.getResource("image.png")).getImage(); } 

but now I've moved them into a different folder but within the project folder still. I'm not quite sure how to read them anymore because I've tried stuff like, "H:NetBeansProjects\Game\Pictures\image.png" but that won't read it in. I need help with what to change within the ""

6
  • What is Astro_n_Monster class? Commented Dec 4, 2013 at 13:21
  • Some painting, some moving, some animating, everything I've been working on xD Commented Dec 4, 2013 at 13:24
  • what Exception is thrown Commented Dec 4, 2013 at 13:31
  • Is your image stored in the same package where your Astro_n_Monster class is placed or somewhere else ?? Commented Dec 4, 2013 at 13:31
  • There is no exception thrown yet and the image is in a folder within project but not the package. I was told to have only java files in the package so I cannot move them there. Commented Dec 4, 2013 at 13:33

4 Answers 4

1
imgMonster = new ImageIcon("Pictures/image.png").getImage(); 

You can call it without using getResource(). If you juse use this file path, it should work. Netbeans will start from the Game dir Which is your project root. From there it will follow your file path specified

Game (project root) Pictures image.png src build 
Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried H:\\NetBeansProjects\\Game\\Pictures\\image.png or H:/NetBeansProjects/Game/Pictures/image.png. The char "\" is only for escaping other characteres. And your code is wrong. It should be like this:

Image imgMonster = new ImageIcon("H:\\NetBeansProjects\\Game\\Pictures\\image.png").getImage(); 

2 Comments

Then there is something else wrong. Can you post all your code?
The only thing that was wrong line that I was reading the image from @Sergi and I think my code is too long to post anyways xD. I tried using all the forms of slashes
0

AnyClass.class.getResource("image.png") returns the absolute URL of that resource. But for that it must be located into the class path folder.

For your code to work, image.png should be available where the .class file of Astro_n_Monster generates.

1 Comment

Thanks for that but that is how I had images before I was told to move them out of the location of the class
0

Consider following package structure with simple solution for loading image from package

com |_ myCompany |_ images |_ image.png com |_ myCompany |_ javaClasses |_ Astro_n_Monster 

Now even your move your class Astro_n_Monster class along with images package you will get the output using following code

imgMonster = new ImageIcon(Astro_n_Monster.class.getResource("/com/myCompany/images/image.png")).getImage(); 

1 Comment

Wouldn't I just have "H:\\NetBeansProjects\\Game\\Pictures\\image.png" like @Sergi suggested? Because if so then that still doesn't work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.