Java – How to set the size of a tiled drawable

How to set the size of a tiled drawable… here is a solution to the problem.

How to set the size of a tiled drawable

I have a

table that should have a checkerboard background. For this, I use tiled drawables like this :

TiledDrawable boardBg;
boardBg = new TiledDrawable(menuSkin.getTiledDrawable("boardBg"));
boardTable.setBackground(boardBg);

Then each graph block has a size of the drawable by default, such as 64px. What do I have to do to make each graph block bigger? I tried

boardBg.setMinWidth(500);
boardBg.setMinHeight(500);

But this has no effect.

Solution

TiledDrawable class does not support scaling I created a class that extends TiledDrawable and can be scaled. It’s best if you don’t want to shrink beyond 0.1, at that scale it will become a performance consumer (depending on the size of the texture). So use caution when zooming out. Note that I didn’t think about when the scale is set to zero, so might create a method setScale that throws an error when that happens. Use ScaledTiledDrawable:: like this

ScaledTiledDrawable scaledTiledDrawable = new ScaledTiledDrawable( new TextureRegion( texture ) );
scaledTiledDrawable.getScale().set( 0.5f, 0.5f );

This is the class:

public class ScaledTiledDrawable extends TiledDrawable {

private Vector2 scale = new Vector2();
    private Affine2 transform = new Affine2();
    private Matrix4 matrix = new Matrix4();
    private Matrix4 oldMatrix = new Matrix4();

public ScaledTiledDrawable() {

super();
    }

public ScaledTiledDrawable( TextureRegion region ) {

super( region );
    }

public ScaledTiledDrawable( TextureRegionDrawable drawable ){

super( drawable );
    }

public Vector2 getScale() {

return scale;
    }

@Override
    public void draw( Batch batch, float x, float y, float width, float height ) {

oldMatrix.set( batch.getTransformMatrix() );
        matrix.set( transform.setToTrnScl( x, y, scale.x, scale.y ) );

batch.setTransformMatrix( matrix );

super.draw( batch, 0, 0, width / scale.x, height / scale.y );

batch.setTransformMatrix( oldMatrix );
    }
}

Related Problems and Solutions