Results 1 to 15 of 15
  1. #1
    such orb is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default 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"));
    
    				}
    			}
    		}
    
    	}
    
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default 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.

    ** This space for rent **

  3. #3
    such orb is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default 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 :)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default 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.

    ** This space for rent **

  5. #5
    such orb is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default 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.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default 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.

    ** This space for rent **

  7. #7
    such orb is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Re: how to load buffered imaages

    ok, i am using Eclipse Indigo.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default 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.

    ** This space for rent **

  9. #9
    such orb is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default 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?

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default 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.

    ** This space for rent **

  11. #11
    such orb is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default 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!

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default 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.

    ** This space for rent **

  13. #13
    such orb is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default 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:

    Java Code:
    galaxy.GalaxySevenSystemOnePlanetOne stars = new galaxy.GalaxySevenSystemOnePlanetOne();
    		System.out.println(stars.galaxy_seven_system_one_planet_one_name);
    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?

    thank you for your help!

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default 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.

    ** This space for rent **

  15. #15
    such orb is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Re: how to load buffered imaages

    my apologies, i thought i would club it all together. i know my original question was about loading images.

    i'll make a new thread.

Similar Threads

  1. Replies: 0
    Last Post: 05-24-2012, 08:37 PM
  2. could not load load from location:/test_subreport.jasper
    By jadeite100 in forum JavaServer Faces (JSF)
    Replies: 1
    Last Post: 03-27-2012, 11:43 AM
  3. Double Buffered
    By hqt in forum Java 2D
    Replies: 2
    Last Post: 02-02-2012, 04:14 AM
  4. Buffered Images
    By sgthale in forum New To Java
    Replies: 2
    Last Post: 08-01-2011, 07:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •