Results 1 to 15 of 15
Thread: how to load buffered imaages
- 07-26-2012, 11:48 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 24
- Rep Power
- 0
how to load buffered imaages
hi, i am kind of new to these java forums and i am not sure how to paste my source code... did i get it right??
anyhow, i am trying to load png images into buffered images - but it ain't working... am i doing something wrong? do i need to use classpath or anything like that?
thanks!
XML Code:package orbits; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JPanel; public class Test3 { public void pix() { try { ImageIcon image1 = new ImageIcon(getClass().getClassLoader() .getResource("src/orbits/resources/photos.png")); } catch (Exception e) { System.out.println("there's an error here"); { try { int width = 100; int height = 40; BufferedImage image2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); image2 = ImageIO.read(getClass().getResourceAsStream( "src/orbits/resources/photos.png")); } catch (Exception ee) { System.out.println("an error here"); } { System.out.println(System.getProperty("user.dir")); } } } } }
- 07-26-2012, 02:37 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: how to load buffered imaages
What does "it ain't working" mean?
You also ought to printStackTrace() for those exceptions, otherwise you'll not know the cause of the problem.Please do not ask for code as refusal often offends.
- 07-26-2012, 03:03 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 24
- Rep Power
- 0
Re: how to load buffered imaages
i meant to say that i have tried all manner of filepaths/urls/permutations but all to no avail. i have the printStackTrace() materials for you.
java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at orbits.Test3.pix(Test3.java:17)
at orbits.Main.main(Main.java:343)
there's an error here
java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at orbits.Test3.pix(Test3.java:37)
at orbits.Main.main(Main.java:343)
an error here
thanks for your reply :)
- 07-26-2012, 03:34 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: how to load buffered imaages
First off don't use getCLassLoader().
Just use the straight getResourceAsStream on the Class..um class.
Now, how are you running this?Please do not ask for code as refusal often offends.
- 07-26-2012, 03:51 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 24
- Rep Power
- 0
Re: how to load buffered imaages
i'm not sure what you are asking? i am doing this in the Main class...
Test3 dodo = new Test3();
dodo.pix();
my Main class is in the src folder on my machine. the src folder is in the orbits package.
thanks.
- 07-26-2012, 04:01 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: how to load buffered imaages
IDE, command line, jar file...there are lots of different ways this code could be run.
Please do not ask for code as refusal often offends.
- 07-26-2012, 04:05 PM #7
Member
- Join Date
- Mar 2012
- Posts
- 24
- Rep Power
- 0
Re: how to load buffered imaages
ok, i am using Eclipse Indigo.
- 07-26-2012, 04:39 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: how to load buffered imaages
Right, so either use a '/' at the start of the path (which will get it working in Eclipse) or, better, is to get Eclipse to copy that image to the build directory (or wherever it sticks the compiled .class files). The former is a hack and will probably not work outside of Eclipse.
Exactly how you do that with Eclipse (via Ant probably) is not something I can say offhand.Please do not ask for code as refusal often offends.
- 07-26-2012, 04:55 PM #9
Member
- Join Date
- Mar 2012
- Posts
- 24
- Rep Power
- 0
Re: how to load buffered imaages
if i take the (better) second option, any ideas on where the compiled .class files go? in the bin folder? should i put the image/image folder in there, too?
also, is there an ideal placement for the Main class? in the src folder, or not?
- 07-26-2012, 05:19 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: how to load buffered imaages
No, you should get Eclipse to build the project the way you want it to (which includes transferring resources like your image).
By default Eclipse will put class files into the bin folder.
And in that case the path (which I only just noticed) would be 'orbits/resources/photos.png'...no 'src' or 'bin'.
Try changing the path to that.
It's possible that'll work without any other faffing.Please do not ask for code as refusal often offends.
- 07-26-2012, 07:59 PM #11
Member
- Join Date
- Mar 2012
- Posts
- 24
- Rep Power
- 0
Re: how to load buffered imaages
sorry about the delay!
i think my project might need a complete rebuild... what is the src folder? to hold resource materials? should i move my Main class from the src folder to another placement?
thanks!
- 07-27-2012, 09:33 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: how to load buffered imaages
Eclipse projects have a basic structure of:
src - source folder containing your source files (.java) in their package structure.
bin - "binary" folder, containing the compiled files (.class) in their package structure.
When you run something it is running the compiled classes in the bin folder. That will be the root of the application when running (or should be), so that's where it starts to look for things referenced in getResource...().
In your case it would therefore be looking in "bin/src/orbits/resources/photos.png", which is obviously not right.Please do not ask for code as refusal often offends.
- 07-27-2012, 01:33 PM #13
Member
- Join Date
- Mar 2012
- Posts
- 24
- Rep Power
- 0
Re: how to load buffered imaages
back again...
i have restructured my project into seperate packages within the src folder/package - so is that the right thing to do?
so now i have many packages. ok, i am using this method/code:
thus, - galaxy - is the galaxy package and i inherit that by declaring stars. now, how i make all the variables in galaxy.GalaxySevenSystemOnePlanetOne availabe to stars without having to set every variable in galaxy.GalaxySevenSystemOnePlanetOne to public? can i en masse declare all variables as public? is there a better method than all of this?Java Code:galaxy.GalaxySevenSystemOnePlanetOne stars = new galaxy.GalaxySevenSystemOnePlanetOne(); System.out.println(stars.galaxy_seven_system_one_planet_one_name);
thank you for your help!
- 07-27-2012, 01:41 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: how to load buffered imaages
This seems to have nothing to do with your original problem...
Please do not ask for code as refusal often offends.
- 07-27-2012, 01:46 PM #15
Member
- Join Date
- Mar 2012
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
Extra Q needed between buffered socket read and buffered file write? (multithreaded)
By lark1313 in forum Threads and SynchronizationReplies: 0Last Post: 05-24-2012, 08:37 PM -
could not load load from location:/test_subreport.jasper
By jadeite100 in forum JavaServer Faces (JSF)Replies: 1Last Post: 03-27-2012, 11:43 AM -
Double Buffered
By hqt in forum Java 2DReplies: 2Last Post: 02-02-2012, 04:14 AM -
Buffered Images
By sgthale in forum New To JavaReplies: 2Last Post: 08-01-2011, 07:15 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks