Skip to content
Ken Fehling edited this page Sep 27, 2013 · 5 revisions

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 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) {
		//Draw 'Hello, world!' at 32,32
		g.drawString("Hello, world!", 32, 32);
		//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);
	}

Clone this wiki locally