Sign in to follow this  
KazeKun

[java] loading imgs stuff.....

Recommended Posts

ok, first im kind of newb,i´m learning java (to make games yay! XD), i read and finish a lot of tutorials and now i im modifiying some of the examples i got, im triying to load a image from any directory, but as far for now im still unsuseful,ive tryed...with URL objects, but when i put the return statment inside the try statment i got some error like "missing return statent", here is something ive tried: public static Image loadimg (){ try{ URL dir = new URL("c:\mi_img.png"); Image temp = Toolkit.getDefaultToolkit().getImage(dir); return temp; }catch(MalformedURLException er){System.out.println(".....¬¬");} } i also tried to put all the path of the image in the getImage method but that neither worked. that is something i ve tried so far..i just waant to know how to load images from any directory..., yes i know this is a verry newb question but if u can tellme ab out at least a tutorial to read....that will be it...thnx for reading me.. [Edited by - KazeKun on October 24, 2005 11:43:24 AM]

Share this post


Link to post
Share on other sites
If you're only loading the image from a directory on your hard disk (e.g. the C:\ drive), then you don't need to use an URL, you can simply pass a string to Toolkit.getDefaultToolkit.getImage(). From here

public static Image loadImg(String file) {
return Toolkit.getDefaultToolkit.getImage(file);
}


If, however, you're intent on using an URL, all you need to do to resolve the return issue is to move the declaration of tmp outside the try/catch block.

public static Image loadImg() {
Image tmp = null;
try {
// load your image here
}
catch( ... ) {
// ensure that tmp is null
}
return tmp;
}


<edit :: Also, remember in your filename declaration to use two '\', not just one. E.g.
String file = "C:\\mi_img.png";

Share this post


Link to post
Share on other sites
If you need an URL object pointing to your image file, use the following:


// non-static
public java.net.URL loadResource(String path) {
return getClass().getResource(path);
}

// static
public static java.net.URL loadResource(String path) {
return ThisClass.class.getResource(path);
}


Provided that you have a structure like the following...


- project directory
- src directory
- res directory


... you will have to specify a path like this:

String path = "/res/img_folder/img_file.jpg";


Hope this helps!

Son Of Cain

Share this post


Link to post
Share on other sites
you have the 2 choices, however note that if you use a full path, like "C://myfolder/img.png" the game may not work on someone else's computer unless they place your game in the same place as you did and have the same folder names. If you intend to give your game to other people, you should consider doing it relatively to you game (e.g. palce sound with your games and instead of starting at C start in your game's directory)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
yeah, youu´re rigth, i been thinking in that, im now triying to do something like "GetBaseDirectory()" or something like that but guess it will be enougth with just create the file structure, well i gues.., well thanks a lot for the advice ^^

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this