Drawing shapes
The Saxion Game App provides a simple set of methods to draw basic shapes such as rectangles, circles, lines, and text. All drawing is done inside the render(float delta) method, and shapes are always drawn after clearing the screen.
Filled vs Outlined shapes
There are two types of shape drawing:
| Type | Description |
|---|---|
| Filled | Draws a solid shape using a fill color. |
| Outlined | Draws only the border (outline) of a shape. |
Before drawing, you choose the mode by using:
GameApp.startShapeRenderingFilled(); // For solid shapes
GameApp.startShapeRenderingOutlined(); // For outlines only
GameApp.endShapeRendering(); // Always finish drawing
Drawing methods
Rectangles
You can draw a rectangle from the bottom-left corner by using the method drawRect(x, y, width, height, colorName).
In addition, we have also a drawRectCentered(centerX, centerY, width, height, colorName). Here you will have to define the center point of the rectangle.
// Draw a rectangle on the middle of the screen
GameApp.drawRectCentered(getWorldWidth()/2, getWorldHeight()/2, 200, 100, "red");
Rounded rectangles
You can draw rectangles with rounded corners using the drawRoundedRect() methods.
Note: Rounded rectangles only work properly in filled mode. Outline mode will not render the corners correctly (because rounded rectangles are not an existing shape in LibGDX and created using various existing shapes and arcs).
Here (50, 50) is the bottom-left corner, 200x100 is the size, and 20 is the radius of the corners.
In addition, you can also draw a rounded rectangle using the center point. This will center the rectangle on the specified coordinates:
Circles
You can draw a circle with a given center point (x, y) and radius by using the method drawCircle(x, y, radius, colorName).
GameApp.drawCircle(300, 150, 50, "blue");
// There is also a possibility to set the number of segments, this will help making the circle smoother
GameApp.drawCircle(300, 150, 50, 80, "blue");
Ellipses
An ellipse is like a stretched or squashed circle. You define it using a position, width, and height. If width and height are equal, it becomes a circle — but ellipses let you create oval shapes. You can draw them by using the method drawEllipse(x, y, width, height, String colorName). You have to define the bottom-left coordinate to draw them.
In addition, we have also a drawEllipseCentered(centerX, centerY, width, height, colorName). Here you will have to define the center point of the ellipse.
// Draw an ellipse on the middle of the screen
GameApp.drawEllipseCentered(getWorldWidth()/2, getWorldHeight()/2, 200, 100, "blue");
Lines
Drawing lines can be done by using the drawLine(x1, y1, x2, y2, colorName) method.
Points
You can draw a single pixel point. Useful for testing or debugging, using the method drawPoint(x, y, colorName).
Polygon
To draw a polygon, you first have to determine the points and store them in an array. After that you can call the drawPolygon() method with the correct parameters.
// Define a simple pentagon
float[] pentagonVertices = {
300, 150,
350, 200,
330, 250,
270, 250,
250, 200
};
// Draw outline
GameApp.setLineWidth(2);
GameApp.startShapeRenderingOutlined();
GameApp.drawPolygon(pentagonVertices, "black");
GameApp.endShapeRendering();
Line thickness
When drawing outlined shapes, you can adjust the thickness of the border using:
This must be called before startShapeRenderingOutlined().
Important to remember
- You have to clear the screen first
- Start and end each shape rendering section
- Use named colors for clarity
- Outlines require
startShapeRenderingOutlined() - Line width must be set before drawing outlines
- Rounded rectangles can only be drawn in the filled mode