Search code examples
libgdx

Drawing Rectangle images-LibGdx


In my first LibGdx Project,I want to draw some rectangles.

I am not looking for shape rendering purpose.I am aiming to implement a function like what fillRect() in j2me do.I have to draw filled rectangles and need to manipulate it(changing size,rotating.. etc). When I google about it, always getting shapeRenderer related things only.

Please mention how can I draw and manipulate my own images.


Solution

  • Draw Rectangle by using Pixmap.

    Texture texture=getPixmapTexture(Color.WHITE);
    
    Sprite sprite=new Sprite(texture); //Used for drawing 2D sprites.
    //or
    Image image=new Image(texture);  //2D scene graph node.
    
    public static Texture getPixmapTexture(Color color){
        return new Texture(PixmapBuilder.getPixmapRectangle(1, 1, color));
    }     
    
    public static Pixmap getPixmapRectangle(int width, int height, Color color){
           Pixmap pixmap=new Pixmap(width, height, Pixmap.Format.RGBA8888);
           pixmap.setColor(color);
           pixmap.fillRectangle(0,0, pixmap.getWidth(), pixmap.getHeight());
           return pixmap;
    }