Problem with object location after changing resolution

I whenever I change resolution and restart jME (and reshape nifty) all my nifty gui panels are placed correctly. However, I created some BitmapFont texts in an AppState to act as a clock/timer in my game:

[java]
AppSettings settings = new AppSettings(true);

	try {
        settings.load("com.animationSettings.Setting1");
    } catch (BackingStoreException e) {
        e.printStackTrace();
    }

            BitmapFont fnt = app.getAssetManager().loadFont("Interface/Fonts/Default.fnt");
        txt = new BitmapText(fnt, false);
	txt.setSize(fnt.getPreferredSize());
	txt.setLocalTranslation(0, settings.getHeight() - (settings.getHeight() / 13), 0); 
	txt.setText("Timer: "+ Double.valueOf(String.format(Locale.US, "%1$.2f", app.getCinematic().getTime())));
	
	// attach bitmap to gui node
       app.getGuiNode().attachChild(txt);

[/java]

but the problem is that when I update the resolution the txt BitmapFont doesn’t get updated accordingly. Is there any way I could update the setting on it without having to re-assign it a new setting?

The size of the text needs to be proportional to the size of the screen. fnt.getPreferedSize() return a fixed size.
If you designed the screen let’s say in 1280 x 720 you have to compute the size with something like : getPreferedSize/1280*newResWidth.
Note that your font will look a bit blurry after this. You’d better have a bitmap font that is nice and sharp with hi res and reduce the size when the res is lower.

Alternatively you can use a small, medium and large font and change this depending on resolution and avoid any up/down sampling. But you probably won’t notice too much difference than down sampling a high res font. Just letting it out there, as another solution

The issue is not the size of the characters of the text but its location. I am only changing from 950 X 1600 to 650 X 1000 so I can still see the text but in a wrong position.

screenshot

Image one is at resolution: {950, 1600}

imagine two is at resolution: {650, 1000}

IMG1:

IMG 2:

So whats the problem? The image stays at exactly the same pixel where you put it.

I want it to adjust according to the new settings after the resolution change

txt.setLocalTranslation(0, settings.getHeight() – (settings.getHeight() / 13), 0);

or in the example that I gave:

[java]txt.setLocalTranslation(settings.getWidth() / 2, settings.getHeight() /2, 0);[/java]

which would put it in the center of the screen

I only use one 64 font for all, and downsample it, works fine for me till size 8

the question is how do I make it move accordingly? I thought my code was doing so already but it’s not.

So whats the problem? The image stays at exactly the same pixel where you put it.

Well get the percentlocation you want it to be, and multiply by the screensize.

eg instead of at pixel 10,200
say at postion 5%,10%
if the screenresolution was 200x2000

but this is what I use:

[java]txt.setSize(fnt.getPreferredSize());
txt.setLocalTranslation(settings.getWidth() / 2, settings.getHeight() / 2), 0);[/java]

I am not giving absolute values, I am trying to put something in the middle of the screen but when I resize it stays in the same location.

Your settings.getWidth() may not be returning the values you think. Perhaps it’s the wrong settings object?

Man, I kind of hate to wade into this simple stuff that two minutes of debugging would solve…

System.out.println() the values you are setting.

Use Camera width and height instead of settings width and height since the camera cannot be wrong unless you are checking it too soon.

1 Like

You need to recalcuate then.

(There are reasone why most of us use any of the jme based gui librarys, as they do a relayouting in such cases automatically)

While I don’t want to discurage you about writing your own gui librarys, I should say that you can expect it to need around 2-3 years and a lot of rewirtes before the results are what you want them to be quality and performance wise.

And at the risk of stating the obvious: if you are resizing at runtime then you will also need to reposition the text at runtime.

this was the issue:

Use Camera width and height instead of settings width and height since the camera cannot be wrong unless you are checking it too soon.

thanks guys