1
\$\begingroup\$

I want to draw a empty rectangle around my gamescreen, to show "borders" around. I've tried pixmap. Works perfectly fine, but i could not find the option of change stroke width of the rectangle.

I thougt about using texture, but i do not feel it's the best way. Due to there is no artwork into this border. It's just a thick line around the screen.

Remember i want to create the border once and not recreate it everytime i render.

Any suggestions?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You could use the ShapeRenderer to draw a border using line;

public class MyAndroidStudioSandboxGame extends Game {
    private ShapeRenderer shapeRenderer;

    @Override
    public void create () {
        shapeRenderer = new ShapeRenderer();
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    }

    private void renderBorder() {
        shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
        float w = Gdx.graphics.getWidth();
        float h = Gdx.graphics.getHeight();

        int lineWidth = 8;
        Gdx.gl20.glLineWidth(lineWidth);

        shapeRenderer.setColor(Color.RED);
        shapeRenderer.line(0, 0, w, 0);
        shapeRenderer.line(0, h, w, h);
        shapeRenderer.line(0, 0, 0, h);
        shapeRenderer.line(w, 0, w, h);
        shapeRenderer.end();                
    }
}

Or, if you want a more elaborate border you can use a NinePatch, and set the camera accordingly.

public class MyAndroidStudioSandboxGame extends Game {
        private SpriteBatch batch;
        private NinePatch border;
        private OrthographicCamera borderCamera;

        @Override
        public void create () {
            batch = new SpriteBatch();
            border = new NinePatch(new Texture("border.png"), 8,8,8,8);

            float borderWidthPercent = 0.1f; // Border width is 10%
            float borderW = (16) / (borderWidthPercent*2); // 16 is 8+8 for the two borders, multiply the percent by 2 because there's a border on each side
            float borderH = borderW * Gdx.graphics.getHeight() / Gdx.graphics.getWidth(); // Maintain the aspect ratio!

            // Set the camera to view an area that is sized to what it needs to be to show the border at the desired size
            borderCamera = new OrthographicCamera(borderW, borderH);
        }

        @Override
        public void render () {
            Gdx.gl.glClearColor(0, 0, 0, 1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
            batch.begin();
            batch.setProjectionMatrix(borderCamera.combined);
            border.draw(batch,
                    -borderCamera.viewportWidth/2.0f,
                    -borderCamera.viewportHeight/2.0f,
                    borderCamera.viewportWidth,
                    borderCamera.viewportHeight );
            batch.end();
        }
    }

Is the above example I've used this image; enter image description here Which is 20px by 20px with an 8px border.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .