Wrting BtimapText to a texture on quad

How would you write some strings to a quad procedurally? I make my Strings into a BitmapText, but how would you get this onto the quad? I thought maybe convert it to an image, then convert that to a texture somehow, but I’m not sure how. Texture class is abstract… Seems strange to me.

I tried:

[java]
BitmapText espText = new BitmapText(guiFont, false);
espText.setText(espanol);

    cardMat.setTexture("Text", assetManager.loadTexture((TextureKey) espText.getKey()));[/java] 

but it seems like the asset key the BitmapText returns is null… It shouldn’t even have an asset key should it?

I’m confused :-?

Spatials have asset keys because Spatials can be loaded from assets. (The fact that this is broken for BitmapText not withstanding).

Asset does not equal texture. So, yes, you are confused. You can’t just take one value out of something and force-jam it into another. No more so than if you tried to open a text file in MS paint. (which is almost an exact analogy in this case)

To your original problem, you should look into the ImagePainter plugin. I think it allows text to be rendered. Also, you could do it with Java2D on a BufferedImage and then convert it but ImagePainter will be a much more direct way.

1 Like

Thank you, I will take a look

I can’t use the image that imagepainter has painted onto though to create a texture because the texture class is abstract :expressionless:
So I can’t make an object to it then use texture.setImage() like in the javadoc. Or do I have to load a texture in then use that object & modify it… Seems like a bad way to do it thought to me.

The javadoc will tell you what subclasses of Texture you can instantiate. The proper one should be obvious once you see it.

Keep the javadoc open at all times. Use it. Learn it. Will save you countless hours of random searching.

ImagePainter will automatically create an Image for you if you want. You can then wrap it into a texture easily enough. There is even an example of doing just that in the ImagePainter test app thing.

Yes the javadocs are useful, but to me they don’t always fully explain how to use the lib. Maybe I missed the javadoc chapter of ‘java for dummies’, but I always found an example worked better. anyways, I get an outofmemory on this line

[java]painter.paintTextArea(0, 0, 1, 1, guiFont, espanol, ImagePainter.TextHAlign.Center, ImagePainter.TextVAlign.Top, type, ImagePainter.BlendMode.NORMAL);[/java]

SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,6,main]
java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2734)
at java.util.ArrayList.ensureCapacity(ArrayList.java:167)
at java.util.ArrayList.add(ArrayList.java:351)
at com.zero_separation.plugins.imagepainter.ImagePainter.paintTextArea(ImagePainter.java:1358)

@javagame said: Yes the javadocs are useful, but to me they don't always fully explain how to use the lib.

Well, one thing that you can count on is that it is completely impossible to ever instantiate an abstract class. So when you find one (like Texture) then you must know that there is some concrete subclass somewhere. JME cannot be magically creating raw Texture objects so it must be creating a subclass.

Javadocs are very good about showing this information.

Texture2d

@javagame said: Yes the javadocs are useful, but to me they don't always fully explain how to use the lib. Maybe I missed the javadoc chapter of 'java for dummies', but I always found an example worked better. anyways, I get an outofmemory on this line

[java]painter.paintTextArea(0, 0, 1, 1, guiFont, espanol, ImagePainter.TextHAlign.Center, ImagePainter.TextVAlign.Top, type, ImagePainter.BlendMode.NORMAL);[/java]

SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,6,main]
java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2734)
at java.util.ArrayList.ensureCapacity(ArrayList.java:167)
at java.util.ArrayList.add(ArrayList.java:351)
at com.zero_separation.plugins.imagepainter.ImagePainter.paintTextArea(ImagePainter.java:1358)

That’s odd, I’ll try and remember to take a look at it this evening. If I don’t feel free to poke me over the weekend :slight_smile:

@Zarch thank you! thats very kind :slight_smile:

Do you always get the same error in the same place?

How large is your text?

The relevant line of code is just adding an int into a list of Integers - with one int per line of text. Perhaps something in the text is confusing it so the search never finishes somehow?

@zarch shouldn’t the size of the text area be defined with the third and fourth arguments to the method call?
Anyways, I tried changing from

[java]painter.paintTextArea(0, 0, 1, 1, guiFont, espanol, ImagePainter.TextHAlign.Center, ImagePainter.TextVAlign.Top, type, ImagePainter.BlendMode.NORMAL);[/java]

To

[java]painter.paintTextLine(0, 0, 100, 100, guiFont, espanol, ImagePainter.TextHAlign.Center, ImagePainter.TextVAlign.Top, type, ImagePainter.BlendMode.NORMAL);[/java]

And I get an even more strange exception

java.lang.NoSuchMethodError: com.jme3.texture.image.ImageRaster.getWidth()I
at com.zero_separation.plugins.imagepainter.ImagePainter.paintSubImage(ImagePainter.java:832)
at com.zero_separation.plugins.imagepainter.ImagePainter.paintTextLine(ImagePainter.java:1295)

I think there must be some inconsistency between the source provided with the plugin and the actual compiled code

EDIT: what I mean by that is the line the stack trace is referring to says
if (xLimit > imageRaster.getWidth()) {

Perhaps you are using an old version of JME.

Have you considered just rendering the text off screen and applying the texture to the quad?

@t0neg0d said: Have you considered just rendering the text off screen and applying the texture to the quad?

Technically that’s what he’s doing. I think I almost know what you mean but I fail to see how it is easier than just drawing the text to an image.

@pspeed said: Technically that's what he's doing. I think I almost know what you mean but I fail to see how it is easier than just drawing the text to an image.

Hehe… One works in minutes… the other requires a thread of the course of a week?

@t0neg0d said: Hehe... One works in minutes... the other requires a thread of the course of a week?

Rendering a scene to a texture is proposed as an alternative to a simple image write because the poster is running an old version of JME and so it is incompatible with the simpler way? Seriously? You think that way is actually going to be easier for the OP to figure out?

Priceless. :slight_smile:

@pspeed said: Rendering a scene to a texture is proposed as an alternative to a simple image write because the poster is running an old version of JME and so it is incompatible with the simpler way? Seriously? You think that way is actually going to be easier for the OP to figure out?

Priceless. :slight_smile:

Why would they need to figure it out? There is a working example in test. Unless they don’t know how to use CTRL+C/CTRL+V, it sounds like this would be the more straight-forward approach.

@t0neg0d said: Why would they need to figure it out? There is a working example in test. Unless they don't know how to use CTRL+C/CTRL+V, it sounds like this would be the more straight-forward approach.

Heheh… “Hi, sorry the four lines of code from this working example is failing for you because you don’t have the latest JME… maybe you could try cutting and pasting 20 lines of less efficient code from a completely different working example and debug that instead. It’s ‘simpler’.” lol

OP, please just upgrade your JME and try again. If you are missing ImageRaster.getWidth() then you must be running and old JME. Ignore the extra noise… they way you are going is the simplest way. You just need to get your environment sorted out.