Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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  
  Problems with loading images  (Read 3625 times)
0 Members and 1 Guest are viewing this topic.
Offline Giovanni

Junior Member




chop!


« Posted 2012-02-03 23:29:38 »

To load my images I have decided ot use the class BufferedImage. To make things a little easier I have created a method which looks like this:

   
1  
2  
3  
4  
5  
6  
7  
8  
9  
private BufferedImage loadImage(String path)
   {
      try
      {
          img = ImageIO.read(new File(path));
          return img;
      }catch (IOException e){}
      return null;
   }


Using this code does however output me an error (@ line: 15). The line on which this error (NullPointerException) appears looks like this:

1  
g.drawImage(game.loadImage("/textures/dot.png"), 0, 0, null);




I am still learning this all, but I am pretty sure that the problem is related to my project settings since I have tried to load images on a bunch of different ways but the result was always the same.

This is my .classpath file:

1  
2  
3  
4  
5  
6  
7  
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
   <classpathentry kind="src" path="src"/>
   <classpathentry kind="src" path="textures"/>
   <classpathentry kind="con" path=". . ."/>
   <classpathentry kind="output" path="bin"/>
</classpath>


I have created the folder "textures" with the help of Eclipse and set it as a project folder.

And yes, this folder does contain the file "dot.png".



Thanks in advance!
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-02-04 00:38:30 »

File takes an absolute path. It is better to do this:
1  
ImageIO.read(getClass().getResource("/textures/dot.png"));


Note that using a leading forward slash indicates the working directory.

Also, it is not smart to read an image every time you draw it. That will cause a lot of slow down plus it is a waste of memory. Store the BufferedImage in an instance variable once.

Offline GabrielBailey74
« Reply #2 - Posted 2012-02-04 01:15:37 »

This is how i'm rendering Images that don't need any transparency.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
   public static Image Sprite;
   public static ImageIcon player_2 = new ImageIcon("sprites/player/player_2.png");

   public static void renderPlayer(Graphics g) {
      final Graphics2D g2d = (Graphics2D) g;
      //Define that the Image: 'Sprite' is now = our players Image
     Sprite = player_2.getImage();
      //Than draw the Sprite defined above.
     g2d.drawImage(Sprite, PlayerEntity.absX, PlayerEntity.absY, null);
   }


Hope it helps some.

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

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-02-04 01:17:08 »

Please do not use ImageIcon. Use ImageIO.read, as the OP correctly did.

Offline Cero
« Reply #4 - Posted 2012-02-04 01:31:46 »

Never ever used these lines, even once.
1  
getClass().getResource

just saying =P

Offline GabrielBailey74
« Reply #5 - Posted 2012-02-04 01:39:10 »

Never ever used these lines, even once.
1  
getClass().getResource

just saying =P

Same for me, I find it difficult, or over 50% of the time it just returns null/cannot find.

Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2012-02-04 02:04:09 »

getClass().getResource(String) and getResourceAsStream(String) use the class's location to look for resources. Appending a forward slash at the beginning makes it look at the root of the application package hierarchy.

For example, lets say the class MyClass is under com.mygame.mypackage. If the file you want is "image.png", you put it under com/mygame/mypackage/ and do MyClass.class.getResource("image.png") or MyClass.class.getResource("/com/mygame/mypackage/image.png").

This mostly depends on the classloader used but according to experience, this works with Jar files, applets, and webstart.

oww typing on a touch screen phone is painful Grin

Offline Cero
« Reply #7 - Posted 2012-02-04 02:33:00 »

the file you want is "image.png", you put it under com/mygame/mypackage/
yeah but who would do that, putting ressources/assets in the same folder source files/bytecode is

Jar files, applets, and webstart.
Yes, basically, only useful in certain situations, like having, what they call a "fat jar", meaning a jar which actually also contains the ressources.
In that case its the only (?) way to access the stuff.

Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2012-02-04 02:44:44 »

Alright then if you have a dedicated resources folder, let's call it "res", you put it under your "src" folder in Eclipse and then you do getResource("/res/" + myFileName);

Offline JESTERRRRRR

Senior Member


Medals: 7
Exp: 1 year



« Reply #9 - Posted 2012-02-04 03:45:18 »

I was overwhelmed with the multitude of image loading methods when I started.. ImageIO.read(this.getClass().getResource("/images/sprite.PNG")); solved all my problems haven't looked back since
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #10 - Posted 2012-02-04 03:56:20 »

I always put res folder along with bin and src Wink

Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #11 - Posted 2012-02-04 04:01:33 »

It's best to put "res" under "src" because it makes exporting to JAR file easier Smiley

Offline ReBirth
« Reply #12 - Posted 2012-02-04 04:08:41 »

But I have experience where eclipse failed in exporting, so I have open the jar with 7z and add the res. Forget how it came exactly.

Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


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

You have to set the settings properly. They're quite confusing at first. Also, you should make a "jardesc" file (2nd screen in the exporting process). This will save the exporting settings in a *filename*.jardesc file and allow you to automate all future exportings.

Offline BoBear2681

JGO Coder


Medals: 18



« Reply #14 - Posted 2012-02-04 04:45:48 »

Jar files, applets, and webstart.
Yes, basically, only useful in certain situations, like having, what they call a "fat jar", meaning a jar which actually also contains the ressources.
In that case its the only (?) way to access the stuff.

It works with loose files as well, not just those situations above.  For example, if I have test.png in the same folder as a class file (in the default package in this particular example), I can do this to display it:

1  
panel.add(new JLabel(new ImageIcon(getClass().getResource("/test.png"))));


Offline Cero
« Reply #15 - Posted 2012-02-06 19:03:44 »

Alright then if you have a dedicated resources folder, let's call it "res", you put it under your "src" folder

res is not source. it has no business in the src folder

src holds .java files only
bin only class files
everything else is accessed from the root.

example:
root has src, bin, cfg, music, sounds, img, maps, bla

alternatively you can group stuff

we use a "content" folder for everything fix and maps are seperate and so are configs and logs

when dealing with a big game you should really structure your stuff

Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #16 - Posted 2012-02-07 00:35:15 »

Resources are source Wink

Offline sproingie

JGO Kernel


Medals: 201



« Reply #17 - Posted 2012-02-07 00:45:34 »

Behold the One True Layout:
1  
2  
3  
4  
5  
6  
7  
src/
    main/
        java/
        resources/
    test/
        java/
        resources/

Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #18 - Posted 2012-02-07 00:51:11 »

I am ra4king and I wholeheartedly agree with the above post (except I would rename resources to res because it makes me feel better Grin)

Offline sproingie

JGO Kernel


Medals: 201



« Reply #19 - Posted 2012-02-07 00:58:09 »

I would rename resources to res because it makes me feel better

Shocked How darest thou besmirch the One True Layout?!   Angry

Seriously, that's the maven defaults (and sbt, and buildr, and gradle).  It's not hard to change it in the config, but the defaults mean not having to write any of that config.  What's really handy though is the separate directory per language, useful when you mix in groovy or jruby or scala...

Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #20 - Posted 2012-02-07 01:24:53 »

Sweetness Grin

Offline Cero
« Reply #21 - Posted 2012-02-07 01:26:28 »

Behold the One True Layout:
1  
2  
3  
4  
5  
6  
7  
src/
    main/
        java/
        resources/
    test/
        java/
        resources/




thats so funny; I mean whats the point in having a src folder when EVERYTHING is in there
you could just not have it, put everything in the root

yeah maven, ok, whatever

anyway, you can do it however you want, just if anyone is curious, here is our current folder structure (only opened content because is has MANY sub folders)

http://img844.imageshack.us/img844/7304/29974753.gif

Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #22 - Posted 2012-02-07 01:54:42 »

How messy and ugly. I still prefer Sprongie's One True Layout:)

Offline Cero
« Reply #23 - Posted 2012-02-07 02:18:21 »

How messy and ugly.



=D

on a serious note: even if you like fat jars, which I dont,
you seriously wouldn't want a 800mb jar containing EVERYTHING, now would you ?
apart from being horrible you couldn't even swap or change files anymore, without replacing the whole thing.
Remember I'm talking in big-games-terms here, not your 5 min casual games with 10 images, 5MB total.

Offline BoBear2681

JGO Coder


Medals: 18



« Reply #24 - Posted 2012-02-07 05:39:09 »

What does project layout have to do with how you deploy things?
Offline gbeebe

Senior Member


Medals: 5
Projects: 1



« Reply #25 - Posted 2012-02-07 05:56:26 »

@ReBirth & Giovanni, perhaps your problem is you aren't refreshing when adding new images to the source?  After I save an image, either new or edited, to my /src/res/ directory... In order to see the changes I have to right click on my project name(listing on the left side of eclipse window) and choose Refresh.  This will copy everything in the src/* to the bin/*.  Your resources should then be available to the build, and when exported the last Refresh will be included in the jar file.
Offline xsvenson
« Reply #26 - Posted 2012-02-07 10:57:57 »

... or You could make the workspace auto-refresh (Properties -> General -> Workspace -> "Refresh automatically")

“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
Offline Cero
« Reply #27 - Posted 2012-02-07 16:27:49 »

What does project layout have to do with how you deploy things?

its the same. difference is, then there are no src or bin folders, but a jar at the root
all paths remain the same, never using this weird getResource

Note: This is not how I think things have to be done, but I'm sure I don't follow some weird java conventions here. So what you are looking at is a project structure, in which someone just does what he thinks makes sense, not following standards.

Offline BoBear2681

JGO Coder


Medals: 18



« Reply #28 - Posted 2012-02-07 17:48:02 »

What does project layout have to do with how you deploy things?

its the same. difference is, then there are no src or bin folders, but a jar at the root


I was being a little pedantic, but my point was that this doesn't have to be true.  Note sproingie (most likely) doesn't ship his unit tests with his actual product, yet they are contained in the source folder via src/test/**.  Project layout doesn't have to map 100% to jar contents.  You can have multiple source folders, like the example above (src/main and src/test) and only ship one of them, for example.  You could also put only the class files into your jar, and use getResource() to refer to any images in resources/ subfolders outside of that jar.

Quote
all paths remain the same, never using this weird getResource

But like I mentioned in my earlier post, getResource() will work with both flat files and jar contents.  Same "path" and everything.
Offline ReBirth
« Reply #29 - Posted 2012-02-08 03:41:20 »

@gbeebe but I don't want copy of my res. Like Cero said if the res is 100GB of size, copy is not.

That's why put it together with bin and src is easier for me.

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.

pw (26 views)
2014-07-24 01:59:36

Riven (25 views)
2014-07-23 21:16:32

Riven (20 views)
2014-07-23 21:07:15

Riven (22 views)
2014-07-23 20:56:16

ctomni231 (51 views)
2014-07-18 06:55:21

Zero Volt (46 views)
2014-07-17 23:47:54

danieldean (37 views)
2014-07-17 23:41:23

MustardPeter (40 views)
2014-07-16 23:30:00

Cero (57 views)
2014-07-16 00:42:17

Riven (55 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!