Results 1 to 4 of 4
  1. #1
    Reskaillev is offline Member
    Join Date
    Jul 2011
    Posts
    53
    Rep Power
    0

    Default Loading an image without intervention of user.dir (relative)

    I was playing with some images and found out that there was a problem..

    Now my file structure looks like this:

    Java Code:
    ---> Java Project
      |---> src
         |---> Package1
         |---> Images
    I'm loading some Images in some Inline JPanels:

    Java Code:
    		
    pane1 = new JPanel()
    		{
    			BufferedImage img = Afbeeldingsframe.createImage("src/Images/"+FIRST+".jpg"); //FIRST is a final String
    			
    			 public void paintComponent(Graphics g) 
    			 {
    			        g.drawImage(img, 0, 0, null);
    			 }
    		};
    And this is createImage():

    Java Code:
    	
    protected static BufferedImage createImage(String path) 
    	{
    		BufferedImage img = null;
    		try 
    		{
    		    img = ImageIO.read(new File(path));
    		} 
    		catch (IOException e) {System.out.println(e.getMessage());}
    		return img;
    	}
    Now I try to load the images by giving the path: "\Images\picture.jpg". Which doesn't work because my user.dir is set to \src
    So I set my path to: "src\Images\picture.jpg" which fixes the problem.

    Now I take my project and turn it into an executable .jar which has the following structure:

    Java Code:
    ---> Silly_Name.jar
         |---> Package1
         |---> Images
    Obviously my path is now: "Images\picture.jpg" and ain't "src\Images\picture.jpg" anymore! (because the structure is different)
    When I change the path in my .java the .jar works fine but the .java doesn't run anymore! (in eclipse) because
    the path is wrong here!

    How to fix this? Or how to make a relative path that always points to my picture.jpg?

    Thnx in advance,

    Reskaillev

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Loading an image without intervention of user.dir (relative)

    You can try the following snippet:

    Java Code:
    BufferedImage image = ImageIO.read(getClass().getResource("something.jpg"));
    Please replace something.jpg with the relative path to your image file.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Loading an image without intervention of user.dir (relative)

    Go through the Oracle tutorial on How to Use Icons, which covers this in more detail.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Reskaillev is offline Member
    Join Date
    Jul 2011
    Posts
    53
    Rep Power
    0

    Default Re: Loading an image without intervention of user.dir (relative)

    Thnx for responses XD

    by replacing it with the following it does the trick: (because it is static)
    Java Code:
    BufferedImage image = ImageIO.read(myClass.class.getResource("something.jpg"));
    Also read part about getResource() which was indeed the relative method I needed

Similar Threads

  1. Loading and Displaying a user chosen image
    By forwardbias in forum AWT / Swing
    Replies: 9
    Last Post: 03-18-2012, 09:54 AM
  2. Loading a image with Image and ImageIcon
    By cel0x in forum AWT / Swing
    Replies: 3
    Last Post: 02-15-2012, 11:38 AM
  3. Replies: 12
    Last Post: 04-14-2011, 02:58 PM
  4. Retriggering of report with out manual intervention
    By rejith003 in forum Advanced Java
    Replies: 0
    Last Post: 07-31-2008, 04:03 PM
  5. Loading An Image Help Please!
    By shaungoater in forum Java 2D
    Replies: 2
    Last Post: 01-09-2008, 09:14 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
  •