[Slick2d] Problem loading a TiledMap in a Applet

Hi everyone!

I’m working on the game using .tmx TiledMap. The standalone version works without a problem. When I try to run my game as an applet, it fails to load the tileset image ( /img/tiles2et.png) of the TiledMap (/map/empty.tmx) :

Starting real applet
Done loading
Mon Jun 11 18:37:12 EDT 2012 INFO:Starting display 800x600
Mon Jun 11 18:37:12 EDT 2012 INFO:Use Java PNG Loader = true
WARNING: Found unknown Windows version: Windows 7
Attempting to use default windows plug-in.
Loading: net.java.games.input.DirectAndRawInputEnvironmentPlugin
Mon Jun 11 18:37:12 EDT 2012 INFO:Found 3 controllers
Mon Jun 11 18:37:12 EDT 2012 INFO:0 : Logitech USB Headset
Mon Jun 11 18:37:12 EDT 2012 INFO:1 : Logitech USB Keyboard
Mon Jun 11 18:37:12 EDT 2012 INFO:2 : Logitech USB Keyboard
Mon Jun 11 18:37:12 EDT 2012 ERROR:access denied ("java.io.FilePermission" ".\img\..\img\tiles2et.png" "read")
java.security.AccessControlException: access denied ("java.io.FilePermission" ".\img\..\img\tiles2et.png" "read")
	at java.security.AccessControlContext.checkPermission(Unknown Source)
	at java.security.AccessController.checkPermission(Unknown Source)
	at java.lang.SecurityManager.checkPermission(Unknown Source)
	at java.lang.SecurityManager.checkRead(Unknown Source)
	at java.io.File.exists(Unknown Source)
	at org.newdawn.slick.util.FileSystemLocation.getResourceAsStream(FileSystemLocation.java:52)
	at org.newdawn.slick.util.ResourceLoader.getResourceAsStream(ResourceLoader.java:61)
	at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:169)
	at org.newdawn.slick.Image.<init>(Image.java:196)
	at org.newdawn.slick.tiled.TileSet.<init>(TileSet.java:113)
	at org.newdawn.slick.tiled.TiledMap.load(TiledMap.java:447)
	at org.newdawn.slick.tiled.TiledMap.<init>(TiledMap.java:122)
	at zis.MapGenerator.generateEmptyMap(MapGenerator.java:34)
	at zis.MapGenerator.<init>(MapGenerator.java:26)
	at zis.PlayState.init(PlayState.java:72)
	at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:171)
	at org.newdawn.slick.AppletGameContainer$Container.initApplet(AppletGameContainer.java:272)
	at org.newdawn.slick.AppletGameContainer$ContainerPanel.initGL(AppletGameContainer.java:229)
	at org.newdawn.slick.AppletGameContainer$ContainerPanel.start(AppletGameContainer.java:216)
	at org.newdawn.slick.AppletGameContainer$1.run(AppletGameContainer.java:92)
Mon Jun 11 18:37:12 EDT 2012 ERROR:Failed to parse tilemap
org.newdawn.slick.SlickException: Failed to parse tilemap
	at org.newdawn.slick.tiled.TiledMap.load(TiledMap.java:480)
	at org.newdawn.slick.tiled.TiledMap.<init>(TiledMap.java:122)
	at zis.MapGenerator.generateEmptyMap(MapGenerator.java:34)
	at zis.MapGenerator.<init>(MapGenerator.java:26)
	at zis.PlayState.init(PlayState.java:72)
	at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:171)
	at org.newdawn.slick.AppletGameContainer$Container.initApplet(AppletGameContainer.java:272)
	at org.newdawn.slick.AppletGameContainer$ContainerPanel.initGL(AppletGameContainer.java:229)
	at org.newdawn.slick.AppletGameContainer$ContainerPanel.start(AppletGameContainer.java:216)
	at org.newdawn.slick.AppletGameContainer$1.run(AppletGameContainer.java:92)
Caused by: java.security.AccessControlException: access denied ("java.io.FilePermission" ".\img\..\img\tiles2et.png" "read")
	at java.security.AccessControlContext.checkPermission(Unknown Source)
	at java.security.AccessController.checkPermission(Unknown Source)
	at java.lang.SecurityManager.checkPermission(Unknown Source)
	at java.lang.SecurityManager.checkRead(Unknown Source)
	at java.io.File.exists(Unknown Source)
	at org.newdawn.slick.util.FileSystemLocation.getResourceAsStream(FileSystemLocation.java:52)
	at org.newdawn.slick.util.ResourceLoader.getResourceAsStream(ResourceLoader.java:61)
	at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:169)
	at org.newdawn.slick.Image.<init>(Image.java:196)
	at org.newdawn.slick.tiled.TileSet.<init>(TileSet.java:113)
	at org.newdawn.slick.tiled.TiledMap.load(TiledMap.java:447)
	... 9 more

I check multiple time and the two file are in the ZIS.jar. I also try sign my jar and I still get the same result.
I try running loading the map using Stream and I get the same error:

 // Without Stream
map = new TiledMap( "map/empty.tmx", true);
// With Stream
map = new TiledMap(  Thread.currentThread().getContextClassLoader().getResourceAsStream("map/empty.tmx"), "img/");
// or
map = new TiledMap(  Thread.currentThread().getContextClassLoader().getResourceAsStream("map/empty.tmx"));

Applet Demo

Source

Thanks you in advance.

Do this: /img/…
Don’t put a ‘.’ at the beginning.

Thanks you for the reply.

I change the stream line to :

map = new TiledMap(  Thread.currentThread().getContextClassLoader().getResourceAsStream("map/empty.tmx"), "/img/");

And I edit empty.tmx and change the path to the tileset image :

<image source="tiles2et.png" width="200" height="200"/>

I didn’t put any ‘.’ in any file and I get this error in the Java console.

Demo applet

Why do you have “/img/” there as a parameter. I don’t know what the parameters of TiledMap are, but I don’t think you should use that. Put the full path in the getResourceAsStream parameter.

Thanks you again for the quick reply.

What do you mean the full path? “map/empty.tmx” is the full path to the map. The problem seems that slick can’t load the tileset image. But empty.tmx seems to have been loaded otherwise the error log won’t mention “tiles2et.png”.

The second parameter ("/img/") is optional and is only the location which we can find tileset images.

Then what is that “./img/tiles2et.png” file? The code you are showing me doesn’t show you loading that, unless that is automatically loaded by the TiledMap. If that is so, then remove the “/img/” parameter.

The TiledMap automatically loads the tileset image. If I remove the “/img/” and I change empty.tmx to:

<image source="/img/tiles2et.png" width="200" height="200"/>

I still get the same error log. I think that the TiledMap doesn’t use a stream when loading the tileset image and thus the error.
I found in the Slick2d source the line responsible for loading the tileset image:

 Image image = new Image(tilesLocation+"/"+ref,false,Image.FILTER_NEAREST,trans);

Is it possible to use TiledMap in a applet?

When you pass a string to a Slick Image/Sound/etc. it will work like so:

  • Search the classpath:
...getClassLoader().getResource(cpRef);
  • If that fails, search the file system (hence your error).
  • If that fails, throw a RuntimeException. (not really the best thing Slick could do…)

So your problem is because Slick can’t find the image in the classpath based on the tilesLocation + “/” + ref path.

Ideally TiledMap should allow you to set the image yourself. I am not familiar with TiledMap (liam has been handling changes) so I’d have to look into it a bit before making changes.

You could get rid of the security exception like so:

ResourceLoader.removeAllResourceLocations();
ResourceLoader.addResourceLocation(new ClasspathLocation());

(this won’t really fix your problem, since it’s clear Slick can’t find the file in the classpath…)

Ok I will create my own map format and use it instead of TiledMap.

Thanks for all the replies!

Mother of God… ::slight_smile:

No need to create your own format. Just look into why Slick can’t find the file. Could be as simple as moving it into a folder “img/”.

If all else fails, modify the TiledMap source.

Sorry my english is terrible. I don’t mean to said that I want to create a map “format”. My game uses procedural generation for the generation of map. I can easily replace TiledMap by a simple multidimensional array_list. The support of TiledMap in a applet is not that important for my game project.
I can still modify TiledMap like you said and make it use a stream. Maybe that would fix my error.