java - Background with getResourceAsStream("path") -
i having folder structure this.
project/src/folder/folder/folder/xyz.java /img/background.jpg
and want put background.jpg jpanel in xyz.java
. background. , i've done with
private imageicon createimageicon(string path, string description) { java.net.url imgurl = getclass().getresource(path); if (imgurl != null) { return new imageicon(imgurl, description); } else { system.err.println("couldn't find file: " + path); return null; } }
but problem here image has in same folder xyz.java
searched bit , found solved getresourceasstream("path")
can't bring enough knowlege solve this. important me because want run programm *.jar file on different operating systems.
my problem don't understand how getressourceasstream("path") should work different. found no tutorial fits me. seems people use different .jar files or applets. , don't know stream can do, getressourceasstream("path") starts (means how have write path). there lacks of knowlage...
edit: found solution don't have give total path. img = imageio.read(getclass().getresource("../../../../img/bg_1.jpg"));
didn't tested it, should run on other systems. problem is, can start program in eclipse. when export *.jar file, happens fine doesn't load picture. , shrinks window minimum though i've setpreferredsize. should maybe work stream solution out. tome bed.
edit: relative path works fine when put images package. not wanted, after 6h guessing tiny problem had bring fishys swim through ocean...
since haven't replied comment, i'll ask questions , make suggestions answer:
is enclosed in jar file? have tried passing path image or image directory on command line , read in command line parameter? , again, why using imageicons when you're displaying image not imageicon?
edit 1
regarding comments:
i using imageicon because ##java on freenode.net told me fits habits.
i have no idea of ##java nor "habits" fitting, current fact of situation using image , imageicon has nothing this, really, rid of it. simplify , use need.
my first solution that: img = imageio.read(new file("path")); needs full path , not url path know html.
the path requirements imageio.read no different you're trying do. fine addition solution.
the jar file sould run without extras on different os. need 1 of it.
you still haven't told if images in jar or not, , images in relation class files, key using resources -- loading resources relative location of class files.
Comments
Post a Comment