The Coder's Handbook   

Fonts

CREATING AND SETTING FONTS

For more detailed information, look at the Font and Graphics class in the Slick 2D Javadoc

Another solution involves using Unicode Fonts.  Here is a guide created by one of my students.  See Unicode Font Documentation

Creating a Font


import java.awt.Font;

import org.newdawn.slick.TrueTypeFont;


public class Game

{

public static TrueTypeFont textFont;

public static TrueTypeFont headingFont;


public static void loadFonts() 

{

textFont = new TrueTypeFont(new Font("Arial", Font.PLAIN, 14), false);

headingFont= new TrueTypeFont(new Font("OCR A Extended", Font.BOLD, 64), false);

}

}

Using a Font by Setting


public void render(Graphics g)

{

g.setFont(Game.textFont);

g.drawString("Hello", 50, 50);

}

Using a Font Directly


public void render(Graphics g)

{

Game.textFont.drawString(50, 50, "Hello");

}

TEXT ALIGNMENT

How To Center Text


int scoreX = 500;

int scoreY = 200;


public void render(Graphics g)

{

String message = "Score: " + getUserScore();

int w = Game.textFont.getWidth(message);

g.setFont(Game.textFont);

g.drawString(message, scoreX - w / 2, scoreY);

}