TrueType Font Support
Slick has the capability to easily display True Type fonts on screen.
True Type Fonts are loaded and rendered using the org.newdawn.slick.TrueTypeFont
class.
import org.newdawn.slick.TrueTypeFont;
You can find the full javadoc to the TrueTypeFont class here.
Contents
Basic Usage[edit]
// initialise the font Font font = new Font("Verdana", Font.BOLD, 20); TrueTypeFont trueTypeFont = new TrueTypeFont(font, true); // render some text to the screen trueTypeFont.drawString(20.0f, 20.0f, "Slick displaying True Type Fonts", Color.green);
Additional Usage[edit]
Anti Aliasing[edit]
The second parameter to the TrueTypeFont constructor controls whether the font is antialiased or not.
Loading fonts from the classpath[edit]
There are a number of true type fonts available on any java system. For reference, see:
http://java.sun.com/j2se/1.5.0/docs/api/index.html?java/awt/Font.html
Most people will also want to load custom fonts for display. Below is a short example which loads a ttf font that has been included in the programs classpath.
import org.newdawn.slick.Color; import org.newdawn.slick.TrueTypeFont; import java.awt.Font; import java.io.BufferedInputStream; import java.io.IOException; public class TTFTest { public static void main(String[] args) throws LWJGLException, FontFormatException, IOException { Font font; TrueTypeFont trueTypeFont; // not included for brevity initGL(); Font startFont = Font.createFont(Font.TRUETYPE_FONT, new BufferedInputStream(TTFTest.class.getClassLoader().getResourceAsStream( "technoid.ttf"))); Font baseFont = startFont.deriveFont(Font.PLAIN, 20); trueTypeFont = new TrueTypeFont(baseFont, true); while (true) { GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); trueTypeFont.drawString(20.0f, 20.0f, "this is a test", Color.gray); Display.update(); if (Display.isCloseRequested()) { System.exit(0); } } } }
Using the TrueTypeFont Class Outside of Slick[edit]
You may use the TrueTypeFont class outside of the slick 2d engine. If you do this, keep in mind:
The TrueTypeFont class assumes you are using the TextureLoader (also included in slick-util) to load and bind gl textures. See:
If you use your own GL methods to bind textures, TrueTypeFont may not display correctly. To ensure TrueTypeFont works, insert a call to TextureImpl.bindNone()
before your drawstring calls. For example:
TextureImpl.bindNone(); trueTypeFont.drawString(20.0f, 20.0f, "this is a test", Color.gray);