Results 1 to 7 of 7

Thread: Paths And Such

  1. #1
    han003 is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default Paths And Such

    Hi!

    Currently working on making a chess application, and I'm using images and sounds too, but...

    Whenever I transfer my project to my laptop I have to reconfigre the path my images and songs-.-"

    Can anyone explain/show me to add images to a jar file or a package or something so that the program automatically finds the files for me?

    Huge thanks!

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Package the resource files into your jar or somewhere relative to your bin files, then use Class.getResource() to get the URL to the file (linux/unix like paths where a starting '/' is the root of the jar, and without the starting slash is relative to the calling class) - you can then use something like ImageIO.read to load the image

  3. #3
    han003 is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by doWhile View Post
    Package the resource files into your jar or somewhere relative to your bin files
    Could you please show me how to do that, or direct me to a website?
    Thanks a lot!

    EDIT: I'm using netbeans
    Last edited by han003; 03-23-2011 at 07:44 PM.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    It all depends upon how you are creating your project, so no. Instead, I recommend you try google or search these forums based upon the advice I gave you above

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Netbeans jars things up automatically.
    You just need to have an images (or resources, or whatever you want to call it) directory under your src directory and plonk them all in there. Then use the jar that it dumps in the dist directory.

  6. #6
    han003 is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Question Package

    Ok, so I have my pictures in this package...

    How do I use them in my code?:confused:
    Get them as an Image (preferably), or an ImageIcon


  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    So they're now in your chess.jar, because Netbeans will do this on a build.
    You will refer to them as getResourceAsStream, usually.
    The location will be /chess/Pics/filename.

    ETA; The resulting stream can then be used instead of a file. Not knowing how you are doing it currently I can't say how to change it.

Similar Threads

  1. Paths
    By N00Bie in forum New To Java
    Replies: 5
    Last Post: 02-22-2011, 11:48 PM
  2. Paths in different os's
    By hardcorebadger in forum New To Java
    Replies: 7
    Last Post: 01-28-2011, 06:04 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
  •