[SOLVED] ClassCastException when creating images dynamically if using batch rendering in nifty

Hi,

I’m creating some content dynamically in my gui including images in the following way:

ImageCreator imgCreator=new ImageCreator();
imgCreator.setPaddingLeft("1px");
imgCreator.setPaddingRight("1px");
imgCreator.setPaddingTop("2px");
imgCreator.setPaddingBottom("0px");
imgCreator.setHeight("20px");
imgCreator.setWidth("20px");
imgCreator.setId("someID");
imgCreator.setFilename("asset_filename.png");
imgCreator.create(niftyDisplay.getNifty(), niftyDisplay.getNifty().getCurrentScreen(), parent);

This was working correctly untill I changed my nifty to use batch rendering because the GUI was too slow in android (6fps). Now I’m getting a ClassCastException in each screen having dynamically loaded images (both in desktop and android):

java.lang.ClassCastException: com.jme3.niftygui.RenderImageJme cannot be cast to de.lessvoid.nifty.render.batch.BatchRenderImage
  at de.lessvoid.nifty.render.batch.BatchRenderDevice.renderImage(BatchRenderDevice.java:428)
  at de.lessvoid.nifty.render.ScalingRenderDevice.renderImage(ScalingRenderDevice.java:115)
  at de.lessvoid.nifty.render.image.renderstrategy.ResizeStrategy.render(ResizeStrategy.java:28)
  at de.lessvoid.nifty.render.image.CompoundImageMode.render(CompoundImageMode.java:43)
  at de.lessvoid.nifty.render.NiftyImage.render(NiftyImage.java:74)
  at de.lessvoid.nifty.render.NiftyRenderEngineImpl.renderImage(NiftyRenderEngineImpl.java:265)
  at de.lessvoid.nifty.elements.render.ImageRenderer.render(ImageRenderer.java:36)
  at de.lessvoid.nifty.elements.Element.renderElement(Element.java:711)
  at de.lessvoid.nifty.elements.Element.render(Element.java:697)
  at de.lessvoid.nifty.elements.Element.renderInternalChildElements(Element.java:729)
  at de.lessvoid.nifty.elements.Element.renderChildren(Element.java:721)
  at de.lessvoid.nifty.elements.Element.render(Element.java:692)
  at de.lessvoid.nifty.elements.Element.renderInternalChildElements(Element.java:729)
  at de.lessvoid.nifty.elements.Element.renderChildren(Element.java:721)
  at de.lessvoid.nifty.elements.Element.render(Element.java:692)
  at de.lessvoid.nifty.elements.Element.renderInternalChildElements(Element.java:729)
  at de.lessvoid.nifty.elements.Element.renderChildren(Element.java:721)
  at de.lessvoid.nifty.elements.Element.render(Element.java:692)
  at de.lessvoid.nifty.screen.Screen.renderLayers(Screen.java:332)
  at de.lessvoid.nifty.Nifty.render(Nifty.java:407)
  at com.jme3.niftygui.NiftyJmeDisplay.postQueue(NiftyJmeDisplay.java:347)
  at com.jme3.renderer.RenderManager.renderViewPort(RenderManager.java:1092)
  at com.jme3.renderer.RenderManager.render(RenderManager.java:1137)
  at networkwebgames.railracer.Main.update(Main.java:589)
  at com.jme3.app.AndroidHarnessFragment.update(AndroidHarnessFragment.java:577)
  at com.jme3.system.android.OGLESContext.onDrawFrame(OGLESContext.java:336)
  at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1522)
  at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1239)

It seems that ImageCreator generates RenderImageJme and it’s incompatible with batch rendering. Is there any way to create the BatchRenderImage and create an image element with it or any other method to create images compatible with batch rendering?

Going back to default rendering method is not an option because of poor performance in mobile devices.

Thanks

We are using the batching rendering and this works, we also do some manipulation to the image but you can skip that part. Basically what we do is add an image to JME’s normal asset manager and pop it to Nifty like everything else.

Hi, thanks for the information :wink:

If I’m correctly understanding your code, you’re overwritting the image in element called “bottomBackgroundPanel” with the newly loaded image (having some changes in it), right?

About the bottomBackgroundPanel element, is it defined as an image or as a panel in the nifty xml file?

Finally, my project is oriented both android and desktop, so I would need to check platform to use either AWTLoader or any android loader like AndroidBufferImageLoader or AndroidNativeImageLoader. Also, how about if I ever think on exporting it to IOS… :face_with_raised_eyebrow:

I’ll try a similar aproach and get back to you with the results

PS: I really like the OpenKeeper project, I didn’t know about it until now! :scream::scream:

But in your case, if you don’t need to manipulate the image, you already can load it regularly. Right? No need for loaders.

NiftyImage niftyImage = nifty.createImage(“asset_filename.png”, true);

Yep. And yes it is a panel.

Sure, better aproach to use nifty.createImage instead… :man_facepalming:

I found out the problem I was facing… There was some old code I wrote long ago in which I was creating manually RenderImageJme instead of calling nifty.createimage. Just by changing it I got it all working.

Thanks :wink: