Results 1 to 5 of 5
  1. #1
    Zman3359's Avatar
    Zman3359 is offline Member
    Join Date
    Jan 2011
    Posts
    42
    Rep Power
    0

    Default Getting a picture out of a jar?

    [SOLVED]

    Hello, I am trying to load a picture that is in a jar into my program. The current method I am using works fine if the picture is in the same folder or package as the main program but if the program is in com.main and the picture is in com.images I can't get it to work. Does anyone know how to do this? Here is my current method of getting the image:

    Java Code:
    ImageIcon imIc = new ImageIcon(this.getClass().getResource(imageName));
    this.pic = imIc.getImage();
    String imageName is usually "image.png" or whatever the name of the image is.

    Thanks for the help pbrockway2 :)
    Last edited by Zman3359; 02-02-2011 at 09:49 PM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Are you using imIc for anything? If not, just use ImageIO.read() which also takes a URL argument.

    Try using System.out.println() to print the URL returned by this.getClass().getResource(imageName) - the chances are you are looking in the wrong place for the image.

    Notice that the name argument of getResource(name) can start with a / in which case the name is interpreted in absolute terms. You might find that it is conceptually easier to deal with something like

    Java Code:
    this.getClass().getResource("/com/images/MyImage.jpg")

  3. #3
    Zman3359's Avatar
    Zman3359 is offline Member
    Join Date
    Jan 2011
    Posts
    42
    Rep Power
    0

    Thumbs up

    Nope im only using imIc to get the image, so could I do:
    Java Code:
    Image pic = ImageIO.read("/com/images/MyImage.png");


    EDIT: I anwsered my own question there :p . That doesn't work but this does:
    Java Code:
    this.pic = (Image)ImageIO.read(this.getClass().getResource("/com/images/MyImage.png"));
    Last edited by Zman3359; 02-02-2011 at 09:54 PM. Reason: Left out com xD

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Zman3359 View Post
    Nope im only using imIc to get the image, so could I do:
    Java Code:
    Image pic = ImageIO.read("/com/images/MyImage.png");


    EDIT: I anwsered my own question there :p . That doesn't work but this does:
    Java Code:
    this.pic = (Image)ImageIO.read(this.getClass().getResource("/images/MyImage.png"));
    yep, that's what was suggested in the first reply-- to use a URL not a String.

  5. #5
    Zman3359's Avatar
    Zman3359 is offline Member
    Join Date
    Jan 2011
    Posts
    42
    Rep Power
    0

    Default

    Yep I realized that when I went to implement it and it gave me an error :)

Similar Threads

  1. Picture Arrays
    By CiderApple in forum Java 2D
    Replies: 1
    Last Post: 12-14-2010, 10:24 PM
  2. My picture is out of bounds?
    By ThatPinkSock in forum New To Java
    Replies: 3
    Last Post: 10-29-2010, 01:40 AM
  3. How do you move a picture?
    By Addez in forum New To Java
    Replies: 6
    Last Post: 11-03-2009, 08:44 PM
  4. picture backround
    By safiya in forum NetBeans
    Replies: 1
    Last Post: 10-18-2008, 07:13 AM
  5. Picture help
    By deathnote11 in forum AWT / Swing
    Replies: 13
    Last Post: 06-03-2008, 05:46 PM

Posting Permissions

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