Changing Terminal Color and Font in Lanterna 3 (on Windows)

1,617 views
Skip to first unread message

Kevin Jones

unread,
May 5, 2016, 1:26:18 PM5/5/16
to lanterna-discuss
I'm trying to do something simple (I think) which is to change the font and color of the text on a Lanterna terminal

My code looks something like this:

Terminal term = new DefaultTerminalFactory().createTerminal();
screen = new TerminalScreen(terminal);
term
.setForegroundColor(new TextColor.RGB(255, 0, 0));
screen.startScreen();

textGraphics = screen.newTextGraphics();
screen.clear();
textGraphics.putString(0, 0, "Press R to create new result, Q to quit");
screen
.refresh();

A Swing window is created and the text appears as 'white' (it's more like gray).

I've tried putting the call to setForgroundColor in various places to no avail.


I figure I'm missing something?

Any help would be appreciated.

Also, is it possible to change the font size?

Thanks

Kevin Jones

Martin Berglund

unread,
Jun 11, 2016, 5:10:40 AM6/11/16
to lanterna...@googlegroups.com
Hi Kevin,
Here's the trick, Lanterna works in three layers and you'll have to decide which layer your program will use. The lowest layer is Terminal, which provides some additional functionality for controlling the output and state of the terminal window. When you call a method on the Terminal interface, the changes (or change of state) will take effect immediately. The middle layer is Screen, which builds on top of Terminal. Screen is like a buffered bitmap image, it keeps a memory copy of what the content of the terminal window is and you can manipulate it as you like. The content won't be written down to the real terminal until you call the update method.

In your example, you are using the Screen layer, but you set the color on the terminal interface. Don't mix them like that, the layer above will assume the layer below is untouched. Instead, to get the color you want, just set it on the textGraphics object before calling .putString(..), or use a .putString(..) overload that allows you to specify the color.

Makes sense?

Martin

--
Det här meddelandet skickas till dig eftersom du prenumererar på gruppen "lanterna-discuss" i Google Grupper.
Om du vill sluta prenumerera på den här gruppen och inte längre få någon e-post från den skickar du ett e-postmeddelande till lanterna-discu...@googlegroups.com.
Fler alternativ finns på https://groups.google.com/d/optout.

Peter Holvenstot

unread,
Feb 21, 2017, 3:02:37 PM2/21/17
to lanterna-discuss
Hi Martin,

I'm having similar questions regarding font styling.  I'm building on the basic form submission example, using MultiWindowTextGUI.  However, I can't figure out how to change the font size or the scaling.  I am ending up with a very large GUI that covers most of my screen.

What I'm doing is something like this (I don't have access to the exact code right now):

Font font = Font.get("DejaVu Sans Mono", Font.PLAIN,  4);
AWTTerminalFontConfiguration cfg = AWTTerminalFontConfiguration.newInstance(font);

and then passing that to the TerminalBuilder.  It seems to accept it, but it doesn't change the font size at all.

I am running under Windows directly from the IDE, is that a problem here?  Perhaps the emulated Swing terminal doesn't support this?

If not, could you please provide a code sample for changing the font?

Martin Berglund

unread,
Mar 26, 2017, 9:45:22 AM3/26/17
to lanterna...@googlegroups.com
Hi Peter,
Can you try with SwingTerminalFontConfiguration instead of AWTTerminalFontConfiguration? I'm suspecting I found the issue, in case that works.

Thanks,
Martin

Om du vill sluta prenumerera på den här gruppen och inte längre få någon e-post från den skickar du ett e-postmeddelande till lanterna-discuss+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages