[SOLVED] Center BitmapText

I have been having a lot of luck with JME. BitmapText has thrown me for a bit of a loop

Here is a picture showing what I’m working with:

I’m interested in centering the yellow text on the card. Here is a code snippet:

BitmapFont font = assetManager.loadFont("Interface/Fonts/Default.fnt");
countText = new BitmapText(font, false);
countText.setLocalTranslation(0, 0, (size / 2) + 1);

I just want the text centered on the node it’s attached to. I’ve checked the tutorials and many forum threads. I’ve tried using BitmapText’s getLineWidth() & getLineHeight(). I’ve tried using BitmapText’s.setBox() and setAlignment(Align.Center). Neither have produced the results I expected. Can anyone provide an example of how you’d center the bitmap text in this example?

I actually also tried getLineWidth but I think the Problem comes into play when using a scale other than 1. Try it with a scale of 1.

1 Like

Set the text position to the lower left corner of the card. Then set the text box size of the BitmapText to the full size of the card. Then set the alignment to center.

1 Like

Thank you for both of your responses. I tried Darkchaos’s suggestion first and it seemed to do the trick. getLineWidth() and getLineHeight() seem to not take the scale into account. This is unintuitive to me but might have been done for a particular reason. Perhaps I might suggest another set of methods getScaledLineWidth() getScaledLineHeight()?

Here is the proof:

Here is the working change:

float xTranslation = -countText.getLineWidth() * countText.getLocalScale().getX() / 2;
float yTranslation = countText.getLineHeight() * countText.getLocalScale().getY() / 2;
countText.setLocalTranslation(xTranslation, yTranslation, 0);