Giovanni
JGO n00b  Posts: 22
|
 |
«
on:
2012-02-03 17: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!
|
|
|
|
|
ra4king
JGO Kernel      Posts: 3160 Medals: 196
I'm the King!
|
 |
«
Reply #1 on:
2012-02-03 18: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.
|
|
|
|
GabrielBailey74
Full Member   Posts: 157 Medals: 2
Owner of Elite Demons R.I.P
|
 |
«
Reply #2 on:
2012-02-03 19: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; Sprite = player_2.getImage(); g2d.drawImage(Sprite, PlayerEntity.absX, PlayerEntity.absY, null); } |
Hope it helps some.
|
|
|
|
Games published by our own members! Go get 'em!
|
|
ra4king
JGO Kernel      Posts: 3160 Medals: 196
I'm the King!
|
 |
«
Reply #3 on:
2012-02-03 19:17:08 » |
|
Please do not use ImageIcon. Use ImageIO.read, as the OP correctly did.
|
|
|
|
Cero
JGO Neuromancer     Posts: 1050 Medals: 18
|
 |
«
Reply #4 on:
2012-02-03 19:31:46 » |
|
Never ever used these lines, even once. just saying =P
|
|
|
|
GabrielBailey74
Full Member   Posts: 157 Medals: 2
Owner of Elite Demons R.I.P
|
 |
«
Reply #5 on:
2012-02-03 19:39:10 » |
|
Never ever used these lines, even once. just saying =P Same for me, I find it difficult, or over 50% of the time it just returns null/cannot find.
|
|
|
|
ra4king
JGO Kernel      Posts: 3160 Medals: 196
I'm the King!
|
 |
«
Reply #6 on:
2012-02-03 20: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 
|
|
|
|
Cero
JGO Neuromancer     Posts: 1050 Medals: 18
|
 |
«
Reply #7 on:
2012-02-03 20: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.
|
|
|
|
ra4king
JGO Kernel      Posts: 3160 Medals: 196
I'm the King!
|
 |
«
Reply #8 on:
2012-02-03 20: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);
|
|
|
|
JESTERRRRRR
Jr. Member   Posts: 63 Medals: 1
|
 |
«
Reply #9 on:
2012-02-03 21: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! Go get 'em!
|
|
ReBirth
JGO Wizard     Posts: 1279 Medals: 19
|
 |
«
Reply #10 on:
2012-02-03 21:56:20 » |
|
I always put res folder along with bin and src 
|
Follow me, your mastah, on TWITTAH!
|
|
|
ra4king
JGO Kernel      Posts: 3160 Medals: 196
I'm the King!
|
 |
«
Reply #11 on:
2012-02-03 22:01:33 » |
|
It's best to put "res" under "src" because it makes exporting to JAR file easier 
|
|
|
|
ReBirth
JGO Wizard     Posts: 1279 Medals: 19
|
 |
«
Reply #12 on:
2012-02-03 22: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.
|
Follow me, your mastah, on TWITTAH!
|
|
|
ra4king
JGO Kernel      Posts: 3160 Medals: 196
I'm the King!
|
 |
«
Reply #13 on:
2012-02-03 22: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.
|
|
|
|
BoBear2681
Full Member   Posts: 238 Medals: 8
|
 |
«
Reply #14 on:
2012-02-03 22: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")))); |
|
|
|
|
|
Cero
JGO Neuromancer     Posts: 1050 Medals: 18
|
 |
«
Reply #15 on:
2012-02-06 13: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
|
|
|
|
ra4king
JGO Kernel      Posts: 3160 Medals: 196
I'm the King!
|
 |
«
Reply #16 on:
2012-02-06 18:35:15 » |
|
Re sources are source 
|
|
|
|
sproingie
JGO Strike Force    Posts: 899 Medals: 55
|
 |
«
Reply #17 on:
2012-02-06 18:45:34 » |
|
Behold the One True Layout: 1 2 3 4 5 6 7
| src/ main/ java/ resources/ test/ java/ resources/ |
|
|
|
|
|
ra4king
JGO Kernel      Posts: 3160 Medals: 196
I'm the King!
|
 |
«
Reply #18 on:
2012-02-06 18: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  )
|
|
|
|
sproingie
JGO Strike Force    Posts: 899 Medals: 55
|
 |
«
Reply #19 on:
2012-02-06 18:58:09 » |
|
I would rename resources to res because it makes me feel better
 How darest thou besmirch the One True Layout?!  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...
|
|
|
|
|
ra4king
JGO Kernel      Posts: 3160 Medals: 196
I'm the King!
|
 |
«
Reply #20 on:
2012-02-06 19:24:53 » |
|
Sweetness 
|
|
|
|
Cero
JGO Neuromancer     Posts: 1050 Medals: 18
|
 |
«
Reply #21 on:
2012-02-06 19: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
|
|
|
|
ra4king
JGO Kernel      Posts: 3160 Medals: 196
I'm the King!
|
 |
«
Reply #22 on:
2012-02-06 19:54:42 » |
|
How messy and ugly. I still prefer Sprongie's One True Layout:)
|
|
|
|
Cero
JGO Neuromancer     Posts: 1050 Medals: 18
|
 |
«
Reply #23 on:
2012-02-06 20: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.
|
|
|
|
BoBear2681
Full Member   Posts: 238 Medals: 8
|
 |
«
Reply #24 on:
2012-02-06 23:39:09 » |
|
What does project layout have to do with how you deploy things?
|
|
|
|
|
gbeebe
Full Member   Posts: 145 Medals: 5
|
 |
«
Reply #25 on:
2012-02-06 23: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.
|
|
|
|
|
xsvenson
JGO n00b  Posts: 31
|
 |
«
Reply #26 on:
2012-02-07 04: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
|
|
|
Cero
JGO Neuromancer     Posts: 1050 Medals: 18
|
 |
«
Reply #27 on:
2012-02-07 10: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.
|
|
|
|
BoBear2681
Full Member   Posts: 238 Medals: 8
|
 |
«
Reply #28 on:
2012-02-07 11: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. 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.
|
|
|
|
|
ReBirth
JGO Wizard     Posts: 1279 Medals: 19
|
 |
«
Reply #29 on:
2012-02-07 21: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.
|
Follow me, your mastah, on TWITTAH!
|
|
|
|