This discussion is archived
9 Replies Latest reply: May 27, 2010 3:38 AM by 807580 RSS

Image/File Path

807580 Newbie
Currently Being Moderated
hi there,

have a slight poblem regarding a file path, when running it (preview) in netbeans the following is fine
String path = "src\\Game\\images\\character\\man.gif";
However the distributed version no longer looks from src\\ its already in Game\\

So the dist version (jar file) can no longer display
man.gif
Any ideas?

Thanks

Edited by: stevanicus on May 27, 2010 12:11 AM
  • 1. Re: Image/File Path
    camickr Expert
    Currently Being Moderated
    Read the section from the Swing tutorial on [How to Use Icons|http://java.sun.com/docs/books/tutorial/uiswing/TOC.html] for better ways to read the icon.
  • 2. Re: Image/File Path
    807580 Newbie
    Currently Being Moderated
    Thanks,

    This is what i've got,
    URL path = Game.class.getResource("images/character/");
    getImg(path + "man.gif");
    getImg(path + "man1.gif");
    The URL path, is correct tho
  • 3. Re: Image/File Path
    793415 Pro
    Currently Being Moderated
    stevanicus wrote:
    ..This is what i've got,
    URL path = Game.class.getResource("images/character/");
    getImg(path + "man.gif");
    getImg(path + "man1.gif");
    Code snippets are next to useless. For example, this snippet fails to explain what getImg(String) does or how it does it(1). But please don't go on to post a bunch of other snippets you feel explains the problem. For better help sooner, post an SSCCE.

    1) My answers to your latest question would be different if the getImg(String) method was treating that String as a string representation of a File or URL, for instance. But either way I would advise you limit the getImg() method to accept either a File or URL as appropriate, rather than a String representing the same.
    The URL path, is correct tho
    How do you know that?(2) Where/how was it checked?

    2)
    - Do a..
    prompt> jar -tf the.jar
    .. & copy/paste the output here.
    - Check the URL returned from getResource(String) via System.out or System.err.
  • 4. Re: Image/File Path
    807580 Newbie
    Currently Being Moderated
    My getImg method is simply
    public Image getImage(String s){
            return Toolkit.getDefaultToolkit().getImage(s);
        }
    The method below, gettings the image from the method above, and shoves it into an arraylist called 'sprites'
     URL path = Game.class.getResource("images/character/");
         System.out.println(path);
         sprites.add(Image(path + "up.gif"));
         sprites.add(getImage(path + "ne.gif"));
    path is equal to this (which is right)
    file:/C:/Users/Stevanicus/Documents/NetBeansProjects/Game/build/classes/Game/images/character/
    Yet no image is displayed. (not even when i build it for preview)

    Thanks

    Btw
    jar -tf the.jar
    Doesnt work in comand prompt for me, for some strange reason (jar not a recogonised command)

    But using winrar, the structure is as follows.
    Game
         ->Classes
         ->Images (folder)
              ->gif images
  • 5. Re: Image/File Path
    793415 Pro
    Currently Being Moderated
    Which part of S-SC-C-E are you having trouble understanding? Is it the part where you need to click a link, and read the information at the other end?
  • 6. Re: Image/File Path
    thomas.behr Newbie
    Currently Being Moderated
    stevanicus wrote:
    public Image getImage(String s){
    return Toolkit.getDefaultToolkit().getImage(s);
    }
    I suggest you read the documentation for [Toolkit.getImage(String)|http://java.sun.com/javase/6/docs/api/java/awt/Toolkit.html#getImage(java.lang.String)]. Pay special attention to the parameter documentation. (The parameter name is a very good hint, too.) Looking at overloaded variants of this method might also be a good idea.

    Or simply follow the advice you were given in [reply #1|http://forums.sun.com/thread.jspa?messageID=10995551#10995551].
  • 7. Re: Image/File Path
    807580 Newbie
    Currently Being Moderated
    AndrewThompson64 wrote:
    Which part of S-SC-C-E are you having trouble understanding? Is it the part where you need to click a link, and read the information at the other end?
    Very helpful, thanks, the solution is just jumping out the screen at me
  • 8. Re: Image/File Path
    807580 Newbie
    Currently Being Moderated
    thomas.behr wrote:
    stevanicus wrote:
    public Image getImage(String s){
    return Toolkit.getDefaultToolkit().getImage(s);
    }
    I suggest you read the documentation for [Toolkit.getImage(String)|http://java.sun.com/javase/6/docs/api/java/awt/Toolkit.html#getImage(java.lang.String)]. Pay special attention to the parameter documentation. (The parameter name is a very good hint, too.) Looking at overloaded variants of this method might also be a good idea.

    Or simply follow the advice you were given in [reply #1|http://forums.sun.com/thread.jspa?messageID=10995551#10995551].
    Thanks,

    Yea got it working in the end... stupid typo haha.

    GIF instead of gif... strange because the preview compile displayed the images without a problem. the distribution build however, was far more picky
  • 9. Re: Image/File Path
    807580 Newbie
    Currently Being Moderated
    For anyone else with the same issue here is the solution
    //call method
    imageList.add(getImage(Main.class.getResource("images/character/up.GIF")));
    //main being the name of and actual class in the package
    //return image
    public Image getImage(URL s){
            return Toolkit.getDefaultToolkit().getImage(s);
        }