Problem with loading a picture

Hello,

i’ve tried loading a picture for my GUI using the HUD Tutorial,
but it always crashes with the following error:

com.jme3.asset.AssetLoadException: An exception has occured while loading asset: Common/MatDefs/Gui/Gui.j3md
Caused by: java.io.IOException: Material instances must be loaded via MaterialKey

Does anyone know what causes this?

Can you give a link to the tutorial and the relevant part of your code?
either the tutorial is outdated, or you do something wrong, but cannot tell from here.

Don’t know what you are doing, but one of these should work:

  • Material mat = new Material(application.getAssetManager(),“Common/MatDefs/Gui/Gui.j3md”);
  • assetManager.loadAsset(New MaterialKey(“Common/MatDefs/Gui/Gui.j3md”));
  • assetManager.loadMaterial(“Common/MatDefs/Gui/Gui.j3md”)

Can’t say if all should work, but I’m fairly certain the first should.

I used this Tutorial: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:hud. I will try your Solution tomorrow loopies, thanks ;).

Well, i’ve tried everything and nothing seems to work. Here’s my code for loading the picture at the moment:

 String name = "pic";

        Texture2D texture = (Texture2D) assetManager.loadTexture("Textures/frame.png");
        float width = texture.getImage().getWidth();
        float height = texture.getImage().getHeight();
        // Picture
        Picture picture = new Picture(name);
        final boolean useAlpha = true;
        Material mat = assetManager.loadAsset(new MaterialKey("Common/MatDefs/Gui/Gui.j3md"));

        picture.setTexture(assetManager, texture, useAlpha);
        picture.setMaterial(mat);

        picture.setWidth(width);
        picture.setHeight(height);

I’m getting the following error:

java.lang.IllegalArgumentException: Material parameter is not defined: Texture

Well, it seems like you kind of do things different than the tutorial so there are problems with your approach. Now that we can finally see the code… like, why do you set your own material?

No idea, I just want to load the picture. I think I read it somewhere and tried it after the Tutorial didn’t work. Would be awesome if someone could tell me how to properly load a picture. :slight_smile:

@Navalon said: No idea, I just want to load the picture. I think I read it somewhere.

Then why not just do what the tutorial does instead of change things?

The tutorial doesn’t work/is outdated…

@Navalon said: The tutorial doesn't work/is outdated...

Doesn’t work how?

This form of 20 questions gets really old really fast.

Make a Picture and give it the image. Put it in the guiNode. There really isn’t anything else to it.

It gives me the error which I wrote in my original post. This forum is as helpful as the stupid Tutorial… I can’t just guess what causes the error if there’s documentation for it. It’s not like I didn’t try to google it first. Do me a favor and read the whole post before giving me anymore of these answers.

I think that the problem is this line

Material mat = assetManager.loadAsset(new MaterialKey("Common/MatDefs/Gui/Gui.j3md"));

that should be

Material mat=new Material(assetManager,“Common/MatDefs/Gui/Gui.j3md”);

EDIT:
I read your code more carefully and the problem is that you don’t have to set a material at all (like you do in this line: picture.setMaterial(mat);).
Look at the source code here when you call setTexture it already creates the material.

@Navalon said: It gives me the error which I wrote in my original post. This forum is as helpful as the stupid Tutorial... I can't just guess what causes the error if there's documentation for it. It's not like I didn't try to google it first. Do me a favor and read the whole post before giving me anymore of these answers.

I can only comment on the code that you actually show us. And saying “I based it on the tutorial” and then showing us totally different code… it’s impossible for us to guess what might or might not have been done wrong.

So, yes, if all you post is “I got this problem”… then you will get a wide variety of less than helpful answers.

I’m done with this thread and will let other people help you instead. I’ve exceed my personal limit to back-and-forth. However, it would help other people if you actually post code with the problems, explain which version of JME you are using, etc… when in doubt, give TOO MUCH information… instead of hardly any information. Otherwise, the forum will be no better than googling.

@Riccardo said: I think that the problem is this line

Material mat = assetManager.loadAsset(new MaterialKey("Common/MatDefs/Gui/Gui.j3md"));

that should be

Material mat=new Material(assetManager,“Common/MatDefs/Gui/Gui.j3md”);

EDIT:
I read your code more carefully and the problem is that you don’t have to set a material at all (like you do in this line: picture.setMaterial(mat);).
Look at the source code here when you call setTexture it already creates the material.

Thank you for your answer. I tried to use exactly the code in the tutorial:

Picture pic = new Picture("HUD Picture");
pic.setImage(assetManager, "Textures/frame.png", true);
pic.setWidth(settings.getWidth()/2);
pic.setHeight(settings.getHeight()/2);
pic.setPosition(settings.getWidth()/4, settings.getHeight()/4);
guiNode.attachChild(pic);

It gives me the following error:

com.jme3.asset.AssetLoadException: An exception has occured while loading asset: Common/MatDefs/Gui/Gui.j3md
Caused by: java.io.IOException: Material instances must be loaded via MaterialKey

then I have tried pic.setTexture instead of setImage:

 Picture pic = new Picture("HUD Picture");
  TextureKey key = new TextureKey("Textures/frame.png", true);
  Texture2D tex = (Texture2D) assetManager.loadTexture(key);
  pic.setTexture(assetManager, tex, true);
  pic.setWidth(settings.getWidth() / 2);
  pic.setHeight(settings.getHeight() / 2);
  pic.setPosition(settings.getWidth() / 4, settings.getHeight() / 4);
  guiNode.attachChild(pic);

which gives me the same error. I hope you understand what I’m trying to say.

@Navalon said: Thank you for your answer. I tried to use exactly the code in the tutorial:
Picture pic = new Picture("HUD Picture");
pic.setImage(assetManager, "Textures/frame.png", true);
pic.setWidth(settings.getWidth()/2);
pic.setHeight(settings.getHeight()/2);
pic.setPosition(settings.getWidth()/4, settings.getHeight()/4);
guiNode.attachChild(pic);

It gives me the following error:

com.jme3.asset.AssetLoadException: An exception has occured while loading asset: Common/MatDefs/Gui/Gui.j3md
Caused by: java.io.IOException: Material instances must be loaded via MaterialKey

then I have tried pic.setTexture instead of setImage:

 Picture pic = new Picture("HUD Picture");
  TextureKey key = new TextureKey("Textures/frame.png", true);
  Texture2D tex = (Texture2D) assetManager.loadTexture(key);
  pic.setTexture(assetManager, tex, true);
  pic.setWidth(settings.getWidth() / 2);
  pic.setHeight(settings.getHeight() / 2);
  pic.setPosition(settings.getWidth() / 4, settings.getHeight() / 4);
  guiNode.attachChild(pic);

which gives me the same error. I hope you understand what I’m trying to say.

The code you have posted here is correct and it works properly with the latest stable release of jme3 sdk.
So, maybe the problem is with your installation or somewhere else in your code. Try to do a new SimpleApplication with just

 Picture pic = new Picture("HUD Picture");
  TextureKey key = new TextureKey("Textures/frame.png", true);
  Texture2D tex = (Texture2D) assetManager.loadTexture(key);
  pic.setTexture(assetManager, tex, true);
  pic.setWidth(settings.getWidth() / 2);
  pic.setHeight(settings.getHeight() / 2);
  pic.setPosition(settings.getWidth() / 4, settings.getHeight() / 4);
  guiNode.attachChild(pic);

in the simpleInitApp and check if the error persists.