javalibgdxbitmap-fonts

how to change fontsize using gdx BitmapFont


I created a font in java with BitmapFont

private BitmapFont font = new BitmapFont(Gdx.files.internal("HARLOWSI.TTF"));

now, I want to change the size of the font,

I tried the method font.setScale(scale); which appears in the BitmapFont methods,

it didn't work, so I looked for solutions online, I was told to try font.getData().setScale(scale);

but this does not work either.

I also tried this, but I get an error on the parameter.size line. "Syntax error on token size, VariableDeclaratorID expected after this token"

package com.mygdx.game;

import java.lang.reflect.Parameter;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;

public class Points {
    FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("HARLOWSI.TTF"));
    FreeTypeFontParameter parameter = new FreeTypeFontParameter();
    parameter.size = 12; //here's where I get an error
    private String name;
    private int quantity;
    private Texture icon;
    private BitmapFont font = new BitmapFont();
    
    public Points(String name, int quantity, Texture icon) {
        super();
        this.name = name;
        this.quantity = quantity;
        this.icon = icon;
        this.font = font;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getQuantity() {
        return quantity;
    }
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
    public Texture getIcon() {
        return icon;
    }
    public void setIcon(Texture icon) {
        this.icon = icon;
    }
    
    public void add(int added) {
        this.setQuantity(this.quantity+added);
    }
    public void substract(int substracted) {
        this.add(-substracted);
    }
    
    public void show(SpriteBatch batch) {
        batch.draw(this.icon, 10, 890-this.icon.getHeight());
        this.font.draw(batch, "Points : " + String.valueOf(this.quantity) , 20+this.icon.getWidth(),890-this.icon.getHeight());
    }
    
    public void show(SpriteBatch batch,int x, int y) {
        batch.draw(this.icon, x, y);
        this.font.draw(batch, "Points : " + String.valueOf(this.quantity) , 20+this.icon.getWidth(),890-this.icon.getHeight());
    }
    
}

Solution

  • One way is to use the FreeTypeFontGenerator, documentation, it allows you to generate a BitmapFont at a certain size.

    That way you can generate several instances of the BitmapFont, one for each size you need in your game.

    In your case the problem comes from you doing this

    parameter.size = 12;
    

    outside of the constructor (or method), it is a syntax error to do anything but declare variables there.

    Move that line into your constructor instead;

    public Points(String name, int quantity, Texture icon) {
        super();
        this.name = name;
        this.quantity = quantity;
        this.icon = icon;
        this.font = font;
        
        parameter.size = 12;
    }