Skip to content

Instantly share code, notes, and snippets.

@ruimaranhao
Created November 23, 2021 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruimaranhao/fbadf0c1f687fed78751f09027c2ffac to your computer and use it in GitHub Desktop.
Save ruimaranhao/fbadf0c1f687fed78751f09027c2ffac to your computer and use it in GitHub Desktop.
How to use another font in Laterna.
public class Application {
public static void main(String[] args) throws IOException, FontFormatException, URISyntaxException {
new Application().run();
}
private void run() throws IOException, FontFormatException, URISyntaxException {
URL resource = getClass().getClassLoader().getResource("square.ttf");
File fontFile = new File(resource.toURI());
Font font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);
DefaultTerminalFactory factory = new DefaultTerminalFactory();
Font loadedFont = font.deriveFont(Font.PLAIN, 25);
AWTTerminalFontConfiguration fontConfig = AWTTerminalFontConfiguration.newInstance(loadedFont);
factory.setTerminalEmulatorFontConfiguration(fontConfig);
factory.setForceAWTOverSwing(true);
factory.setInitialTerminalSize(new TerminalSize(40, 20));
Terminal terminal = factory.createTerminal();
((AWTTerminalFrame)terminal).addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
e.getWindow().dispose();
}
});
Screen screen = new TerminalScreen(terminal);
screen.setCursorPosition(null); // we don't need a cursor
screen.startScreen(); // screens must be started
screen.doResizeIfNecessary(); // resize screen if necessary
screen.setCharacter(10, 10, TextCharacter.fromCharacter('C')[0]);
screen.refresh();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment