Results 1 to 4 of 4
- 04-01-2012, 10:51 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 53
- Rep Power
- 0
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:
I'm loading some Images in some Inline JPanels:Java Code:---> Java Project |---> src |---> Package1 |---> Images
And this is createImage():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); } };
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 \srcJava 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; }
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:
Obviously my path is now: "Images\picture.jpg" and ain't "src\Images\picture.jpg" anymore! (because the structure is different)Java Code:---> Silly_Name.jar |---> Package1 |---> Images
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
- 04-02-2012, 05:14 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 697
- Rep Power
- 6
Re: Loading an image without intervention of user.dir (relative)
You can try the following snippet:
Please replace something.jpg with the relative path to your image file.Java Code:BufferedImage image = ImageIO.read(getClass().getResource("something.jpg"));Website: Learn Java by Examples
- 04-02-2012, 07:00 AM #3
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-02-2012, 01:27 PM #4
Member
- Join Date
- Jul 2011
- Posts
- 53
- Rep Power
- 0
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)
Also read part about getResource() which was indeed the relative method I neededJava Code:BufferedImage image = ImageIO.read(myClass.class.getResource("something.jpg"));
Similar Threads
-
Loading and Displaying a user chosen image
By forwardbias in forum AWT / SwingReplies: 9Last Post: 03-18-2012, 08:54 AM -
Loading a image with Image and ImageIcon
By cel0x in forum AWT / SwingReplies: 3Last Post: 02-15-2012, 10:38 AM -
Java Applet loading image; render as you get image?
By ea25 in forum New To JavaReplies: 12Last Post: 04-14-2011, 01:58 PM -
Retriggering of report with out manual intervention
By rejith003 in forum Advanced JavaReplies: 0Last Post: 07-31-2008, 03:03 PM -
Loading An Image Help Please!
By shaungoater in forum Java 2DReplies: 2Last Post: 01-09-2008, 08:14 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks