From: Kevin Lamonte Date: Sat, 5 Aug 2017 15:28:42 +0000 (-0400) Subject: #16 expose columns, rows, font size X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=c447c6e5759db7ac9bbb05cc1b3618dd38230335;p=fanfix-jexer.git #16 expose columns, rows, font size --- diff --git a/README.md b/README.md index 6bf20a9..b4114f3 100644 --- a/README.md +++ b/README.md @@ -24,11 +24,11 @@ Jexer currently supports three backends: TCP socket. jexer.demos.Demo3 demonstrates how one might use a character encoding than the default UTF-8. -* Java Swing UI. The default window size for Swing is 80x25, which is - set in jexer.session.SwingSession. For the demo applications, this - is the default backend on Windows and Mac platforms. This backend - can be explicitly selected for the demo applications by setting - jexer.Swing=true. +* Java Swing UI. The default window size for Swing is 80x25 and 20 + point font; this can be changed in the TApplication(BackendType) + constructor. For the demo applications, this is the default backend + on Windows and Mac platforms. This backend can be explicitly + selected for the demo applications by setting jexer.Swing=true. Additional backends can be created by subclassing jexer.backend.Backend and passing it into the TApplication diff --git a/src/jexer/TApplication.java b/src/jexer/TApplication.java index d735e48..3907a9d 100644 --- a/src/jexer/TApplication.java +++ b/src/jexer/TApplication.java @@ -635,6 +635,12 @@ public class TApplication implements Runnable { switch (backendType) { case SWING: + // The default SwingBackend is 80x25, 20 pt font. If you want to + // change that, you can pass the extra arguments to the + // SwingBackend constructor here. For example, if you wanted + // 90x30, 16 pt font: + // + // backend = new SwingBackend(this, 90, 30, 16); backend = new SwingBackend(this); break; case XTERM: diff --git a/src/jexer/backend/SwingBackend.java b/src/jexer/backend/SwingBackend.java index c881b9a..acca7fa 100644 --- a/src/jexer/backend/SwingBackend.java +++ b/src/jexer/backend/SwingBackend.java @@ -46,14 +46,31 @@ public final class SwingBackend extends Backend { private SwingTerminal terminal; /** - * Public constructor. + * Public constructor. The window will be 80x25 with font size 20 pts. * * @param listener the object this backend needs to wake up when new * input comes in */ public SwingBackend(final Object listener) { + this(listener, 80, 25, 20); + } + + /** + * Public constructor. + * + * @param listener the object this backend needs to wake up when new + * input comes in + * @param windowWidth the number of text columns to start with + * @param windowHeight the number of text rows to start with + * @param fontSize the size in points. Good values to pick are: 16, 20, + * 22, and 24. + */ + public SwingBackend(final Object listener, final int windowWidth, + final int windowHeight, final int fontSize) { + // Create a screen - SwingScreen screen = new SwingScreen(); + SwingScreen screen = new SwingScreen(windowWidth, windowHeight, + fontSize); this.screen = screen; // Create the Swing event listeners diff --git a/src/jexer/io/SwingScreen.java b/src/jexer/io/SwingScreen.java index 24b007a..ee8467d 100644 --- a/src/jexer/io/SwingScreen.java +++ b/src/jexer/io/SwingScreen.java @@ -308,8 +308,10 @@ public final class SwingScreen extends Screen { * Public constructor. * * @param screen the Screen that Backend talks to + * @param fontSize the size in points. Good values to pick are: 16, + * 20, 22, and 24. */ - public SwingFrame(final SwingScreen screen) { + public SwingFrame(final SwingScreen screen, final int fontSize) { this.screen = screen; setDOSColors(); @@ -342,13 +344,13 @@ public final class SwingScreen extends Screen { getContextClassLoader(); InputStream in = loader.getResourceAsStream(FONTFILE); Font terminusRoot = Font.createFont(Font.TRUETYPE_FONT, in); - Font terminus = terminusRoot.deriveFont(Font.PLAIN, 20); + Font terminus = terminusRoot.deriveFont(Font.PLAIN, fontSize); setFont(terminus); gotTerminus = true; } catch (Exception e) { e.printStackTrace(); // setFont(new Font("Liberation Mono", Font.PLAIN, 24)); - setFont(new Font(Font.MONOSPACED, Font.PLAIN, 24)); + setFont(new Font(Font.MONOSPACED, Font.PLAIN, fontSize)); } pack(); @@ -742,16 +744,24 @@ public final class SwingScreen extends Screen { /** * Public constructor. + * + * @param windowWidth the number of text columns to start with + * @param windowHeight the number of text rows to start with + * @param fontSize the size in points. Good values to pick are: 16, 20, + * 22, and 24. */ - public SwingScreen() { + public SwingScreen(final int windowWidth, final int windowHeight, + final int fontSize) { + try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { - SwingScreen.this.frame = new SwingFrame(SwingScreen.this); + SwingScreen.this.frame = new SwingFrame(SwingScreen.this, + fontSize); SwingScreen.this.sessionInfo = new SwingSessionInfo(SwingScreen.this.frame, - frame.textWidth, - frame.textHeight); + frame.textWidth, frame.textHeight, + windowWidth, windowHeight); SwingScreen.this.setDimensions(sessionInfo.getWindowWidth(), sessionInfo.getWindowHeight()); diff --git a/src/jexer/session/SwingSessionInfo.java b/src/jexer/session/SwingSessionInfo.java index 07e7b22..dcba27d 100644 --- a/src/jexer/session/SwingSessionInfo.java +++ b/src/jexer/session/SwingSessionInfo.java @@ -133,13 +133,17 @@ public final class SwingSessionInfo implements SessionInfo { * @param frame the Swing Frame * @param textWidth the width of a cell in pixels * @param textHeight the height of a cell in pixels + * @param windowWidth the number of text columns to start with + * @param windowHeight the number of text rows to start with */ public SwingSessionInfo(final Frame frame, final int textWidth, - final int textHeight) { + final int textHeight, final int windowWidth, final int windowHeight) { - this.frame = frame; - this.textWidth = textWidth; - this.textHeight = textHeight; + this.frame = frame; + this.textWidth = textWidth; + this.textHeight = textHeight; + this.windowWidth = windowWidth; + this.windowHeight = windowHeight; } /**