Finally managed to properly set a background for my table :). Had to learn about ninepatch a little but it finally worked out, here is step by step tutorial how to set table background in libgdx with ninepatch.
Ninepatch is simply a png image that has some black pixels in its borders. These black pixels tell us how the image will be stretched.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
public class TextScrollPane { private Table table; private Label label; public TextScrollPane(float width, float height){ BitmapFont font = new BitmapFont(); table = new Table(); table.setWidth(width); table.setHeight(height); table.setBackground(new NinePatchDrawable(getNinePatch(("ng.9.png")))); label = new Label("Bla bla bla bla", new Label.LabelStyle(font, Color.WHITE) ); label.setWrap(true); table.add(label).expand().top().left(); } public Label getLabel() { return label; } public Table getTable() { return table; } private NinePatch getNinePatch(String fname) { // Get the image final Texture t = new Texture(Gdx.files.internal(fname)); // create a new texture region, otherwise black pixels will show up too, we are simply cropping the image // last 4 numbers respresent the length of how much each corner can draw, // for example if your image is 50px and you set the numbers 50, your whole image will be drawn in each corner // so what number should be good?, well a little less than half would be nice return new NinePatch( new TextureRegion(t, 1, 1 , t.getWidth() - 2, t.getHeight() - 2), 10, 10, 10, 10); } } |