Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Libgdx | Scene2d | Set background color of table?

I am creating a menu (like the one is flappy bird, when you die it pops up over the play screen). I created a class that extends table, and I want to set the background of the table to white. Is there a way to do this?

like image 250
J Doe Avatar asked Aug 22 '16 14:08

J Doe


2 Answers

You can do it like this:

Pixmap bgPixmap = new Pixmap(1,1, Pixmap.Format.RGB565);
bgPixmap.setColor(Color.RED);
bgPixmap.fill();
textureRegionDrawableBg = new TextureRegionDrawable(new TextureRegion(new Texture(bgPixmap)));
Table table = new Table();
table.setBackground(textureRegionDrawableBg);

Remember to call dispose() on the texture and on the pixmap. `

like image 134
Simen Russnes Avatar answered Sep 22 '22 14:09

Simen Russnes


I see that the problem has already been solved, but there are others asking to see the code and I can't comment yet.

Here's an implementation of a similar solution, with the exception that a class will be made available for instantiation (so that the Table background color can be changed later easily):

https://www.snip2code.com/Snippet/2615417

BackgroundColor backgroundColor = new BackgroundColor("white_color_texture.png");
backgroundColor.setColor(2, 179, 228, 255); // r, g, b, a
table.setBackground(backgroundColor);

So, create an instance of the arbitrary BackgroundColor class (linked above) by giving the constructor the filename of a white PNG from your project resources (like what @Tenfour04 has mentioned in the comment above).

If you're not familiar with the latter part, then see the repo linked below, where an example of such PNG file can be found.

Now use the instance's setColor(red, green, blue, alpha) method, then pass the instance to the libGDX Table using the setBackground(Drawable drawable) method.

This is not meant to be a perfect solution for all — modify as needed.

Backup:

https://github.com/ronrihoo/libGDX-Table-Background-Color

like image 29
Ronald Rihoo Avatar answered Sep 21 '22 14:09

Ronald Rihoo