Page 1 of 2 12 LastLast
Results 1 to 20 of 35
  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Problematic Relative Source

    Hello! :)

    I am coding a game, and loading my sprite sheet with this statement:

    Java Code:
    BufferedImage bigImage = ImageIO.read(new File("../Space Invaders/resources/spritesheet.png"));
    Problematic Relative Source-shit.png

    However, when I have extracted the game it becomes extremely sensitive to changes of that source. For example, when I "placed" (I don't know the right word) the folder in a .zip file, the game couldn't load my sprite sheet anymore, even though I changed the source for the folder I was extracting it to. Same for changing the name of the folder or moving it elsewhere, the game won't load the sprites and consequently doesn't start at all. This applies even if you set everything back to normal again.

    How do I solve this? I want to be able to move the extracted game folder without having these issues.

    Thanks in advance! :D

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,656
    Rep Power
    5

    Default Re: Problematic Relative Source

    From where are you running the program? Also, the source file is probably not the problem. The class files are the one which are responsible for reading the image file.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Problematic Relative Source

    From here:

    Problematic Relative Source-shit.png

    Inside the folder:

    Problematic Relative Source-shit1.png

    with this code:

    Java Code:
    BufferedImage bigImage = ImageIO.read(new File("../Space Invaders/spritesheet.png"));

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,656
    Rep Power
    5

    Default Re: Problematic Relative Source

    If you're trying to read the image file from the jar file you may want to check out this tutorial.

    Retrieving Resources (The Java™ Tutorials > Deployment > Java Web Start)

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Problematic Relative Source

    I can't use that class since it's only applicable for ImageIcons. I have to use BufferedImages because, well, this is how I load my sprites:

    Java Code:
    	public void loadSprites(){
    		try{
    			BufferedImage bigImage = ImageIO.read(new File("../Space Invaders/resources/spritesheet.png"));
    			
    			sprites = new BufferedImage[16 * 16]; // BufferedImage array
    			
    			for(int x = 0; x < 16; x++){
    				for(int y = 0; y < 16; y++){
    					sprites[(x * 16) + y] = bigImage.getSubimage(y * 16, x * 16, 16, 16);
    				}
    			}
    		}catch(IOException e){
    			e.printStackTrace();
    		}
    	}

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Problematic Relative Source

    I have to use BufferedImages
    Read the API doc for the ImageIO class's read() method to see how it can be used to read resources via URLs
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,656
    Rep Power
    5

    Default Re: Problematic Relative Source

    Quote Originally Posted by Zelaine View Post
    I can't use that class since it's only applicable for ImageIcons. I have to use BufferedImages because, well, this is how I load my sprites:
    Did you take the time to read the example and understand what it was doing? ImageIcon has nothing to do with the getResource method of the ClassLoader class. The getResource and its sibling methods are part of the ClassLoader class. Please read up on them and understand what they do. Here is an extract from the documentation:

    "Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code."

    Sound familiar? It may not solve your problem but it's worth knowing.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Problematic Relative Source

    Thanks!

    But now, whatever source I enter, and I'm sure I am right, causes an Illegal Argument Exception saying: input == null.

    Java Code:
    ClassLoader cl = this.getClass().getClassLoader();
    BufferedImage bigImage = ImageIO.read(cl.getResource("../Space Invaders/resources/spritesheet.png"));

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,656
    Rep Power
    5

    Default Re: Problematic Relative Source

    Quote Originally Posted by Zelaine View Post
    Thanks!

    But now, whatever source I enter, and I'm sure I am right, causes an Illegal Argument Exception saying: input == null.

    Java Code:
    ClassLoader cl = this.getClass().getClassLoader();
    BufferedImage bigImage = ImageIO.read(cl.getResource("../Space Invaders/resources/spritesheet.png"));
    What happens if you just specify "resources/spritesheet.png" as your file name?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Problematic Relative Source

    It causes the same exception unfortunately.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Problematic Relative Source

    If the image file is in the jar file, the path should be the path to the file. There is no ../ relative directory change.

    Look in the jar file. What is the path to the image file?
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,656
    Rep Power
    5

    Default Re: Problematic Relative Source

    Quote Originally Posted by Zelaine View Post
    It causes the same exception unfortunately.
    Well nuts!! I'm going to write a small program and try this out myself. You may also want to try Norm's advice.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  13. #13
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Problematic Relative Source

    Quote Originally Posted by Norm View Post
    If the image file is in the jar file, the path should be the path to the file. There is no ../ relative directory change.

    Look in the jar file. What is the path to the image file?
    I just used that source while in my IDE, the image is only in the same folder as my jar file when I have extracted the game. Anyway, I tried what you said as well before but it didn't work. (I assume you mean "/spritesheet.png".)

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Problematic Relative Source

    Can the image be in the jar file? That is where it should be if the program doesn't need a user to configure the app by providing his own image file.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Problematic Relative Source

    That would be ideal, but I concluded it doesn't seem like it's possible from some searching and testing.

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,656
    Rep Power
    5

    Default Re: Problematic Relative Source

    I thought it was in the Jar file. Read this --> Lesson: Packaging Programs in JAR Files (The Java™ Tutorials > Deployment)

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  17. #17
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Problematic Relative Source

    Quote Originally Posted by Norm View Post
    Can the image be in the jar file? That is where it should be if the program doesn't need a user to configure the app by providing his own image file.
    Never mind my last comment, I found an option in Eclipse that lets one do this. However, the JAR file doesn't run when one opens it.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Problematic Relative Source

    Keep trying. There are dozens of other OPs that have gotten it to work.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Problematic Relative Source

    Now I got it to open using CMD, but it gives me an error saying it doesn't find the file, even though it worked in Eclipse and I've placed the image in the jar file.

  20. #20
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Problematic Relative Source

    Exactly where is the image stored when it is compiled into the jar? It is not in the same folder as it is when I ran the program in Eclipse, even though I got to choose exactly what resources to implement into the jar file.

Page 1 of 2 12 LastLast

Similar Threads

  1. help with second problematic kid
    By elecleoalune in forum New To Java
    Replies: 5
    Last Post: 04-03-2011, 11:04 AM
  2. is this IOException problematic?
    By ozzyman in forum New To Java
    Replies: 14
    Last Post: 03-31-2011, 06:34 PM
  3. Replies: 0
    Last Post: 08-03-2009, 08:04 AM
  4. minimum word length in search problematic
    By jocassid in forum Suggestions & Feedback
    Replies: 3
    Last Post: 07-01-2008, 04:24 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
  •