Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Question about ImageIO (first time using it)  (Read 2422 times)
0 Members and 1 Guest are viewing this topic.
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Posted 2012-02-15 00:13:22 »

So with ImageIcon, you set up the file location like this - "/textures/Player.png"

However, I can't figure out how to do this with ImageIO. Something I am missing maybe?

Here's the code that won't work (this file is inside of the "res" folder, which is already inside my filesystem. The above ImageIcon example works, and that system is set up like this: res >> textures >> file)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
File spriteSheetLocation = new File("/spritesheet.png");
BufferedImage bigImg;
   
   public SpriteSheet(Main main)
   {
      try {
         bigImg = ImageIO.read(spriteSheetLocation);
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }


And a stack trace

javax.imageio.IIOException: Can't read input file!
   at javax.imageio.ImageIO.read(Unknown Source)
   at com.natekramber.main.SpriteSheet.<init>(SpriteSheet.java:19)
   at com.natekramber.main.Main.<init>(Main.java:41)
   at com.natekramber.main.Main.main(Main.java:169)

I know ra4king promotes ImageIO like crazy, maybe he can help Smiley

Thanks a bunch,

-Nathan

Check out my website!
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-02-15 00:18:36 »

That error occurs because it can't find the file.

Never use File when loading resources, use ImageIO.read(<ClassName>.class.getClassLoader().getResourceAsStream("res/spritesheet.png"));

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #2 - Posted 2012-02-15 00:24:18 »

Ah ok.

Now I have this -

1  
2  
3  
4  
5  
6  
try {
         bigImg = ImageIO.read(SpriteSheet.class.getClassLoader().getResourceAsStream("/spritesheet.png"));
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }


but that gives this -


Exception in thread "main" java.lang.IllegalArgumentException: input == null!
   at javax.imageio.ImageIO.read(Unknown Source)
   at com.natekramber.main.SpriteSheet.<init>(SpriteSheet.java:18)
   at com.natekramber.main.Main.<init>(Main.java:41)
   at com.natekramber.main.Main.main(Main.java:169)

The interwebz failed to give a quick answer, and I figure you will answer quicker anyways. Cheesy

Thanks,

-Nathan

p.s. It happened whether or not I put /res at the front of it.

Check out my website!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-02-15 00:24:46 »

input == null also means it can't find the file. Didn't you say it's in "res"?

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #4 - Posted 2012-02-15 00:28:06 »

Edited my previous post, but it was too late Tongue

I tried it with /res at the front first, and it didn't work as well. I've never had to put that in front before, so I thought maybe it would work without.

Here's a screenshot that shows my filesystem, as well as the whole class, and the error.

-Nathan

Check out my website!
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2012-02-15 00:31:22 »

With getClassLoader(), you don't need the leading forward slash since it automatically uses a path relative to the root of the project.

I actually just discovered this a couple days ago when MatthiasM pointed out that Class.getResource(AsStream) automatically remove the leading forward slash or (if it is absent) pre-pend the Class's path and then delegate that String to the ClassLoader's getResource(AsStream). Smiley

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #6 - Posted 2012-02-15 00:58:33 »

Updated with

bigImg = ImageIO.read(SpriteSheet.class.getClassLoader().getResourceAsStream("res/spritesheet.png"));

Still broken with "input == null!" error.

O':

-Nathan

Check out my website!
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2012-02-15 01:38:30 »

OH! You've added the separate "res" folder in the build path. This means all you need is "spritesheet.png" without any slashes Grin

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #8 - Posted 2012-02-15 02:20:13 »

Thanks Cheesy

Check out my website!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (33 views)
2014-11-22 12:13:56

xFryIx (73 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (65 views)
2014-11-07 19:57:14

TehJavaDev (69 views)
2014-11-03 22:04:50

BurntPizza (67 views)
2014-11-03 18:54:52

moogie (82 views)
2014-11-03 06:22:04

CopyableCougar4 (82 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!