ImageIcon to Image in a jar file?

Discussion in 'Java' started by tiewknvc9, Aug 14, 2006.

  1. tiewknvc9

    tiewknvc9 Guest

    hi!

    I am trying to get an image from an Imageicon within a jar file.

    Outside of the jar file this is pretty easy,
    myImage = myImageIcon.getImage();

    to get from a string to an image obviously we do the following
    URL url = Image.class.getResource(strFile);

    that is not what I am asking though...

    My function receives an ImageIcon, and I have to convert it to an Image
    within the jar file...

    Any ideas?
     
    tiewknvc9, Aug 14, 2006
    #1
    1. Advertising

  2. tiewknvc9

    tiewknvc9 Guest

    I tried this:

    myImage = ImageIO.read(getClass().getResource(myImageIcon.toString()));

    but it doesnt work because the toString method breaks in the jars
    execution...



    tiewknvc9 wrote:
    > hi!
    >
    > I am trying to get an image from an Imageicon within a jar file.
    >
    > Outside of the jar file this is pretty easy,
    > myImage = myImageIcon.getImage();
    >
    > to get from a string to an image obviously we do the following
    > URL url = Image.class.getResource(strFile);
    >
    > that is not what I am asking though...
    >
    > My function receives an ImageIcon, and I have to convert it to an Image
    > within the jar file...
    >
    > Any ideas?
     
    tiewknvc9, Aug 14, 2006
    #2
    1. Advertising

  3. tiewknvc9

    tiewknvc9 Guest

    I also tried simply

    myImage = myImageIcon.getImage();

    but that causes a crash too...

    tiewknvc9 wrote:
    > hi!
    >
    > I am trying to get an image from an Imageicon within a jar file.
    >
    > Outside of the jar file this is pretty easy,
    > myImage = myImageIcon.getImage();
    >
    > to get from a string to an image obviously we do the following
    > URL url = Image.class.getResource(strFile);
    >
    > that is not what I am asking though...
    >
    > My function receives an ImageIcon, and I have to convert it to an Image
    > within the jar file...
    >
    > Any ideas?
     
    tiewknvc9, Aug 14, 2006
    #3
  4. tiewknvc9

    Guest

    Hi,

    I always use the resource for a jar or for files. First make sure that
    you copy your image files to your compiled sources location and then
    reference your images like this. Always reference your resources that
    way if you are going to eventually distribute them in a jar and it will
    save to time in the end.

    String location = "my/package/for/images/myimage.gif";
    ImageIcon icon = null;
    URL iconURL = SomeClassInJar.class.getClassLoader().getResource(
    location);
    if (iconURL != null) {
    icon = new ImageIcon(iconURL);
    }

    Better yet, centralize this code in an ImageUtilities class to save
    more time.

    Cheers,

    Dan Andrews

    - - - - - - - - - - - - - - - - - - - - - - - -
    Ansir Development Limited www.ansir.ca
    - - - - - - - - - - - - - - - - - - - - - - - -



    tiewknvc9 wrote:
    > hi!
    >
    > I am trying to get an image from an Imageicon within a jar file.
    >
    > Outside of the jar file this is pretty easy,
    > myImage = myImageIcon.getImage();
    >
    > to get from a string to an image obviously we do the following
    > URL url = Image.class.getResource(strFile);
    >
    > that is not what I am asking though...
    >
    > My function receives an ImageIcon, and I have to convert it to an Image
    > within the jar file...
    >
    > Any ideas?
     
    , Aug 14, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil...

    How to close ImageIcon file

    Phil..., Oct 9, 2003, in forum: Java
    Replies:
    1
    Views:
    502
    William Brogden
    Oct 9, 2003
  2. Arnold Peters
    Replies:
    0
    Views:
    586
    Arnold Peters
    Jan 5, 2005
  3. muttley
    Replies:
    0
    Views:
    2,730
    muttley
    Oct 20, 2005
  4. cyberco
    Replies:
    4
    Views:
    3,772
    Roedy Green
    Feb 14, 2006
  5. Arnold Peters
    Replies:
    0
    Views:
    670
    Arnold Peters
    Jan 5, 2005
Loading...

Share This Page