Results 1 to 3 of 3
  1. #1
    sublixt's Avatar
    sublixt is offline Member
    Join Date
    Jul 2011
    Location
    somewhere
    Posts
    37
    Rep Power
    0

    Default Load image from jar

    I am trying to load an image from a jar file. Its the same one that my program is compiled to, but I cannot seem to get my images to load.

    just to show what I did.
    Java Code:
    private Image cover;
    
    public Main(){
        ...
        try{
            cover = new ImageIcon("cover.jpg").getImage();
        }catch(Exception e){}
    }
    
    public void paintComponent(Graphics g){
        ...
        g.drawImage(cover, 0, 40, null);
    }
    its not comming up with any errors just not showing the image
    Last edited by sublixt; 10-16-2011 at 10:42 PM.

  2. #2
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Load image from jar

    I second everything jammas states above, but also add that you shouldn't ignore exceptions:


    Java Code:
    private Image cover;
    
    public Main(){
        ...
        try{
            cover = new ImageIcon("cover.jpg").getImage();
        }catch(Exception e){} // *** !!!!!!!
    }

    its not comming up with any errors just not showing the image
    Since the above code ignores exceptions, you really don't know if there are no errors. At least put a printStackTrace within the catch block.

Similar Threads

  1. Error: cannot load image .
    By iswan in forum Java Applets
    Replies: 1
    Last Post: 09-29-2011, 03:26 AM
  2. Load image to jPanel
    By DJIT in forum AWT / Swing
    Replies: 7
    Last Post: 12-27-2010, 01:48 PM
  3. load image in the jsp page
    By maneuk in forum Eclipse
    Replies: 0
    Last Post: 04-09-2010, 10:23 PM
  4. Can not load image in SWT/Eclipse
    By janice in forum SWT / JFace
    Replies: 4
    Last Post: 10-03-2008, 05:53 PM
  5. Help with load image
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 08:16 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
  •