Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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  
  Loading Images from a project's directory  (Read 7968 times)
0 Members and 1 Guest are viewing this topic.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 114
Projects: 4
Exp: 3 years



« Posted 2012-03-13 03:56:11 »

Very simple,

1  
2  
3  
4  
5  
6  
File file = new File("images/img.png"); //needs to be an "image" folder in the project folder
try {
   Image img = ImageIO.read(new File(file.toURI()));
} catch (Exception e) {
   
}
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #1 - Posted 2012-03-13 04:31:02 »

Don't ever use File to load resources:
1  
BufferedImage image = ImageIO.read(getClass().getClassLoader().getResourceAsStream("images/img.png"));

getResourceAsStream(String) searches from the working directory, aka the bin folder in Eclipse/Netbeans or the root of the JAR file if run as stand-alone/applet.

Offline Cero
« Reply #2 - Posted 2012-03-13 05:08:26 »

Don't ever use File to load resources

Why ?

1  
Image img = ImageIO.read(new File("images/img.png"));

I always use this.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie
« Reply #3 - Posted 2012-03-13 05:12:28 »

Try it in an applet, or webstart.

Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #4 - Posted 2012-03-13 05:15:04 »

File will not work when the resource is in a JAR file. Also, File uses a system dependent way for resolving relative paths. It is best to use a getResource<AsStream>(String) because it has 1 set behavior for all systems plus it can also return a URL/InputStream for resources inside ZIP/JAR files Smiley

Offline Fokusas

Senior Member


Medals: 3
Projects: 1



« Reply #5 - Posted 2012-03-13 11:33:20 »

Maybe anyone know why eclipse spits java.lang.NullPointerException then using getResource(String) or getResourceAsStream(String). Then runing from jar everthing works perfectly.
1  
WorldIcon=new ImageIcon(getClass().getResource("img/world.gif"))
Offline Cero
« Reply #6 - Posted 2012-03-13 14:48:17 »

my question was of course rhetorical, I just wanted you guys to make it clear.
Since I never use Applets or Webstarts and in fact only use relative file paths; so "never" seems harsh =D

Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #7 - Posted 2012-03-13 15:24:29 »

Maybe anyone know why eclipse spits java.lang.NullPointerException then using getResource(String) or getResourceAsStream(String). Then runing from jar everthing works perfectly.
1  
WorldIcon=new ImageIcon(getClass().getResource("img/world.gif"))

Using getClass().getResource<AsStream>(String) without a leading forward slash makes it look relative to that Class's package.

Offline gimbal

JGO Coder


Medals: 25



« Reply #8 - Posted 2012-03-19 11:40:04 »

I tend to support both filesystem loading and classpath loading myself; gives freedom between "development mode" (in which I can set a working directory for the project in the IDE and switch that around when I'm experimenting) and "deployment mode" (all resources in the game jar). Something like:

1  
2  
3  
4  
5  
6  
7  
8  
9  
InputStream is = null;

File fp = new File("somepath/somefile.png");

if(fp.isFile()){
  is = new FileInputStream(fp);
} else {
  is = getClass().getClassLoader().getResourceAsStream("somepath/somefile.png");
}


Insert exception handling and resource closing code. As an added "bonus" this strategy gives me the opportunity to override resources in the jar without having the replace them, which helps to debug problems with game resources in a more "live environment" - for example I can instruct someone that is testing to put a piece of game script or whatever in a specific place to see if that changes anything, without having to provide an updated jar first.
Offline sproingie
« Reply #9 - Posted 2012-03-19 16:56:55 »

for example I can instruct someone that is testing to put a piece of game script or whatever in a specific place to see if that changes anything, without having to provide an updated jar first.

Putting that directory in the classpath before the jars has exactly the same effect with resource loading.  I do this all the time for config files in my own apps.

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

Innocent Bystander





« Reply #10 - Posted 2012-03-20 17:20:47 »

For Jar files I always use:
1  
Image i = new ImageIcon("directory").getImage();

It works with a jar file but the image folder has to be in the directory of the jar and not inside it..
Offline sproingie
« Reply #11 - Posted 2012-03-20 17:35:33 »

That's because the String argument to ImageIcon always names a file.  It also has a version that takes a URL, so you can say

1  
Image i = new ImageIcon(getClass().getResource(path)).getImage();


Mind you I'd check that getClass().getResource(path) wasn't null first.

Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #12 - Posted 2012-03-20 18:43:28 »

I am strongly against the usage of ImageIcon because it's an ugly hack that internally just does:
1  
Toolkit.getDefaultToolkit().getImage(...);


I advise you use javax.imageio.ImageIO:
1  
ImageIO.read(URL);

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.

xsi3rr4x (54 views)
2014-04-15 18:08:23

BurntPizza (52 views)
2014-04-15 03:46:01

UprightPath (66 views)
2014-04-14 17:39:50

UprightPath (49 views)
2014-04-14 17:35:47

Porlus (66 views)
2014-04-14 15:48:38

tom_mai78101 (90 views)
2014-04-10 04:04:31

BurntPizza (150 views)
2014-04-08 23:06:04

tom_mai78101 (246 views)
2014-04-05 13:34:39

trollwarrior1 (204 views)
2014-04-04 12:06:45

CJLetsGame (211 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!