[source code] Android Development Tutorial - 75

+1 Bucky Roberts · February 26, 2015
package com.thenewboston.buckyblaster;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class MyGdxGame extends ApplicationAdapter implements InputProcessor {

    private SpriteBatch batch;
    private BitmapFont font;
    private int screenWidth, screenHeight;
    private String message = "Touch me";

    @Override
    public void create () {
        batch = new SpriteBatch();

        screenWidth = Gdx.graphics.getWidth();
        screenHeight = Gdx.graphics.getHeight();

        font = new BitmapFont();
        font.setColor(Color.GREEN);
        font.scale(5);

        Gdx.input.setInputProcessor(this);
    }

    @Override
    public void dispose() {
        batch.dispose();
        font.dispose();
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(1,1,1,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        BitmapFont.TextBounds textSize = font.getBounds(message);
        float x = screenWidth/2 - textSize.width/2;
        float y = screenHeight/2 + textSize.height/2;

        batch.begin();
        font.draw(batch, message, x, y);
        batch.end();
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        message = "Touch down at " + screenX + ", " + screenY;
        return true;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        message = "Touch up at " + screenX + ", " + screenY;
        return true;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        message = "Dragging at " + screenX + ", " + screenY;
        return true;
    }

    @Override
    public boolean keyDown(int keycode) {
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        return false;
    }

}

Post a Reply

Replies

Oldest  Newest  Rating
0 Keyvan Azad · July 8, 2016
Hello Guys

There is a Little bit change to this code. It will not work if you use below  code(It dose not work in libGDX 1.6):

BitmapFont.TextBounds  textSize = font.getBounds(message);
float x = screenWidth/2 - textSize.width/2;
float y = screenHeight/2 + textSize.height/2;

Instead of that use below code, so it will run on both emulator and your mobile:

GlyphLayout glyphLayout = new GlyphLayout();
glyphLayout.setText(font, message);


float x = screenWidth/2 - glyphLayout.width/2;
float y = screenHeight/2 + glyphLayout.height/2;



PS. remember add:
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
0 Keyvan Azad · July 9, 2016
Hello Again

The last correction:
font.scale(5);  must be changed with  font.getData().setScale(2);

With best regard Keyvan
  • 1

Java / Android Development

107,058 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator