使用强制等宽终端字体 Java AWT
我想使用 Terminal
界面来显示程序中读入的字符串(在 Windows 上,平台独立性并不重要)。
如果我使用 Font font = Font.decode("Terminal-10");
它不是等宽的。
如果我使用 Font font = new Font(Font.MONOSPACED, Font.PLAIN, 11); 一切都很好,但看起来像 Courier。
如果我使用(可能是我误解了 API 文档),
HashMap attr = new HashMap<TextAttribute,TextAttribute>();
attr.put("FAMILY", Font.MONOSPACED);
attr.put("FONT", Font.decode("Terminal-10"));
font = new Font(attr);
它只是使用默认值(一些 12 点普通字体)。如何正确设置终端字体的属性?我正在 Graphics2D
上绘制字符串,并使用 ImageIO
保存它们。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来 Java AWT 根本不支持位图字体 - 从我在 Font 类 - 它仅处理
TRUETYPE_FONT
和TYPE1_FONT
。 Terminal 是一种位图字体,因此没有简单的方法可以使用 AWT 的字体机制来使用它。我目前正在尝试通过滚动我自己的简单位图字体引擎来解决类似的问题,但在 这个问题。
Looks like you Java AWT doesn't support bitmap fonts at all - judging from what I see in Font class - it handles only
TRUETYPE_FONT
andTYPE1_FONT
. Terminal is a bitmap font, so there's no easy way to use it using AWT's Font machinery.I'm currently trying to solve a similar problem with rolling my own simple bitmap font engine with limited success in this question.