How to draw shapes in libgdx table?

I want to draw something like a graph (math function), and want to insert it into table as a cell. I try to insert shaperenderer into table, but it is not working. How i can do this? Thank you!

I try to use something like that:

Table table = new Table();

ShapeRenderer shapeRen = new ShapeRenderer();
shapeRen.begin(ShapeType.Filled);
shapeRen.setColor(1,1,1,1);
shapeRen.rect(0,0,50,50);
shapeRen.end();

table.add(shapeRen);

I know, this is not right. =) Can you help me to make something like that?

Upvotes: 2

Views: 628

Answers (1)

c'Bam
c'Bam

Reputation: 11

You can't really add a shapeRenderer to a table as it is not an actor, but if you really wanna use it within a table, i guess you could do something like the following.

public class MyGdxGame extends ApplicationAdapter {
    Table table;
    ShapeRenderer shapeRen;
    Stage stage;

    @Override
    public void create () {
        stage = new Stage();
        table = new Table();
        table.setDebug(true);
        table.row().pad(20).size(200, 50);
        table.add();
        table.row().pad(20).size(200, 50);
        table.add();
        table.setPosition(Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2);

        stage.addActor(table);
        shapeRen = new ShapeRenderer();
    }

    @Override
    public void render () {
        stage.draw();
        drawShapes();
    }

    public void drawShapes() {
        shapeRen.begin(ShapeRenderer.ShapeType.Filled);
        shapeRen.setColor(1,1,1,1);
        shapeRen.rect(table.getX() - table.getPrefWidth()/2 + table.getCells().get(0).getPadLeft(),table.getY() + table.getPrefHeight()/2 - table.getCells().get(0).getPrefHeight() - table.getCells().get(0).getPadTop(),50,50);
        shapeRen.end();
    }
}

The output will look like this: Result in desktop

Upvotes: 1

Related Questions