Skip to content
Thomas Cashman edited this page May 30, 2015 · 1 revision

At the core of the mini2Dx library is the Graphics class. This class handles all rendering and associated functions. This class can be called during your render method. The following shows some examples of common render operations. For a full list of functions available, check the Graphics Javadoc.

Note that when rendering, the origin (0,0) is at the top-left corner of the screen.

Drawing a Rectangle

	public void render(Graphics g) {
		//Draw a rectangle at 32,32 with width and height of 64 pixels
		g.drawRect(32, 32, 64, 64);
	}

Filling a Rectangle

	public void render(Graphics g) {
		//Set the color to red
		g.setColor(Color.RED);
		//Fill a red rectangle at 32,32 with width and height of 64 pixels
		g.fillRect(32, 32, 64, 64);
	}

Drawing text

	public void render(Graphics g) {
		//Draw 'Hello, world!' at 32,32
		g.drawString("Hello, world!", 32, 32);
	}

Drawing a sprite

	Sprite sprite;

	public void initialise() {
		//Load the sprite from an image
		sprite = new Sprite(new Texture(Gdx.files.internal("example.png")));
		sprite.setPosition(24, 24);
		//Mini2Dx's origin (0,0) is in the top-left corner of the screen
		//So we need to flip the sprite vertically
		sprite.flip(false, true);
	}

	public void render(Graphics g) {
		//Draw the sprite
		g.drawSprite(sprite);
	}

Rotating rendering

	public void render(Graphics g) {
		//Rotate the canvas 90 degrees for any rendering call after this
		g.rotate(90f);
		//Draw 'Hello, world!' at 32,32 on the rotated canvas - top left corner of screen
		g.drawString("Hello, world!", 32, 32);
		//Undo the rotation so future rendering is rendering normally
		g.rotate(-90f);
	}

Scaling rendering

	public void render(Graphics g) {
		//Double the scale of everything
		g.scale(2f, 2f);
		//Draw 'Hello, world!' at 32,32 - due to scaling it renders at 64,64 and twice the normal size
		g.drawString("Hello, world!", 32, 32);
		//Undo the scaling
		g.scale(0.5f, 0.5f);
	}
Clone this wiki locally