Home » Newcomers » Newcomers » How do I import Images from a file, from Eclipse?(Trying to figure out how this works, help always appreciated :))
How do I import Images from a file, from Eclipse? [message #697540] |
Sun, 17 July 2011 01:44  |
Eclipse User |
|
|
|
Hey everyone, I was wondering if you could help me out, or at the very least, direct me to the correct forum. Because I feel like the question I have is very simple, I put it in this forum, but I am not at all sure if it belongs here. The question is about loading images. my code example for you all:
try{
Full_HP = new ImageIcon("Game_Images/Wood_fullHP.jpg");
this.setIcon(Full_HP);
// this class entends JLabel, so the line above sets the image to appear
}catch(Exception FileLoadException){
System.out.println("Failed to load at least one Wood_Block ImageIcon.");
}
//end code block
Now, I looked up the working dirrectory, and it shows that the directory feeds into my project folder. So, next to the src and bin files, I made another file called Game_Images that contains several JPEG images. I still CANNOT get my images to load. . .I've taken the Game_Images folder and tried sticking it into the src or bin files, but nothing works. Please, if this isn't enough, say something, and tell me what I need to include. ALSO, BE SPECIFIC WITH ADVICE, I AM NOT A MASTER AT JAVA OR ECLIPSE!!
Feel free to redirect me to a better forum if appropriate
|
|
|
(no subject) [message #697678 is a reply to message #697540] |
Sun, 17 July 2011 13:41  |
Eclipse User |
|
|
|
Keep in mind that you're loading using a file location. That's not
likely what you want. If that's what you do want, you need to specify
the full absolute path of the file. You've only specified a relative
path. Probably you want this to work when you've deployed it so then it
needs to be on your classpath. So you want to use the constructor that
relies on a URL. Class.getResource is helpful for producing such a
URL. Often people will put the icon in the same folder as the class
that loads it and use just the relative name of the file, which is then
resolve on the classpath to the icon in that package.
On 16/07/2011 10:44 PM, forums-noreply@eclipse.org wrote:
> Hey everyone, I was wondering if you could help me out, or at the
> very least, direct me to the correct forum. Because I feel like the
> question I have is very simple, I put it in this forum, but I am not
> at all sure if it belongs here. The question is about loading
> images. my code example for you all:
>
> try{
> Full_HP = new ImageIcon("Game_Images/Wood_fullHP.jpg");
>
> this.setIcon(Full_HP);
> // this class entends JLabel, so the line above sets the image
> to appear
>
> }catch(Exception FileLoadException){
> System.out.println("Failed to load at least one
> Wood_Block ImageIcon.");
>
> }
>
> //end code block
>
>
> Now, I looked up the working dirrectory, and it shows that the
> directory feeds into my project folder. So, next to the src and bin
> files, I made another file called Game_Images that contains several
> JPEG images. I still CANNOT get my images to load. . .I've taken the
> Game_Images folder and tried sticking it into the src or bin files,
> but nothing works. Please, if this isn't enough, say something, and
> tell me what I need to include. ALSO, BE SPECIFIC WITH ADVICE, I AM
> NOT A MASTER AT JAVA OR ECLIPSE!!
>
> Feel free to redirect me to a better forum if appropriate
>
>
>
|
|
|
Goto Forum:
Current Time: Wed Aug 27 10:49:23 EDT 2025
Powered by FUDForum. Page generated in 0.05595 seconds
|