Israelg99 Israelg99 - 1 year ago 143
Java Question

libGDX - make border around an image

Can I make a border around an image?
I have lots of blocks in 2D, and when the player hovers with the mouse on one of them I want to display the border around the texture / image.

Thats how I draw the block actually ( I don't think its relevant, but maybe it will help ) :

batch.draw([mapPos].TEXTURE, (mapPosX * Block.WIDTH), (mapPosY * Block.HEIGHT), Block.WIDTH, Block.HEIGHT);

Is it possible with code or should I make a separate image with the texture and border around it?
Any ideas?

Answer Source

You could try using a ShapeRenderer to draw the border first, then batch.draw over it. This is done purely in code, without using a texture. The code below adds a blue border.

In render() add the following after your batch.end().

batch.end(); // Add the following after this line

sr.setColor(new Color(0,0,1,0));
sr.rect((mapPosX * Block.WIDTH), (mapPosY * Block.HEIGHT), Block.WIDTH, Block.HEIGHT));

Of course, you need to initialize ShapeRenderer in your Screen or ApplicationListener implementation. Just do it in the code where you declare and initialized batch.

In your Game class:

SpriteBatch batch; //Put the following below this line
ShapeRenderer sr;

In your constructor :

batch = new SpriteBatch(); //Put the following below this line
sr = new ShapeRenderer();

edit: i have rewrote the function so that you can draw the shape after drawing the texture batch.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download