Results 1 to 4 of 4
- 04-01-2012, 11: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:
Java Code:---> Java Project |---> src |---> Package1 |---> Images
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); } };
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; }
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
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, 06:14 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 762
- Rep Power
- 14
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"));
Website: Learn Java by Examples
- 04-02-2012, 08: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.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 04-02-2012, 02: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)
Java 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, 09:54 AM -
Loading a image with Image and ImageIcon
By cel0x in forum AWT / SwingReplies: 3Last Post: 02-15-2012, 11:38 AM -
Java Applet loading image; render as you get image?
By ea25 in forum New To JavaReplies: 12Last Post: 04-14-2011, 02:58 PM -
Retriggering of report with out manual intervention
By rejith003 in forum Advanced JavaReplies: 0Last Post: 07-31-2008, 04:03 PM -
Loading An Image Help Please!
By shaungoater in forum Java 2DReplies: 2Last Post: 01-09-2008, 09:14 AM
Bookmarks