Java – How to get a stage at the center of libgdx?

How to get a stage at the center of libgdx?… here is a solution to the problem.

How to get a stage at the center of libgdx?

My image size is 720×480 and the stage size is the same. The boolean parameter (whether to stretch the stage) is left false in order to maintain the aspect ratio, now the problem I’m having is showing black areas on both sides of the screen I want my scene to be centered. The link below references a file that shows the problem more clearly, the image is aligned with the bottom left corner, I want it centered so that the black area is equally split at the top and bottom.

What is the solution to this problem?

http://badlogicgames.com/forum/viewtopic.php?f=11&t=3398

Solution

You must specify the X and Y coordinates as follows

X = (Gdx.graphics.getWidth() - sprite.getRegionWidth()) / 2.0f;
Y = (Gdx.graphics.getHeight() - sprite.getRegionHeight()) / 2.0f, sprite.getRegionWidth()/2.0f;

You must calculate the center of the image. In this way, you can place the image in the center of the screen. Also here, I use sprites to display images.

Related Problems and Solutions