因为我在LWJGL项目上工作,所以显示文本时遇到了问题。 从理论上讲,我想迭代(扩展)ascii表并保存ArrayList中所有字符的每个纹理。
但我的问题是在创建char图像时它不会获得正确的尺寸。尺寸似乎是1(宽度)x 3(高度) 代码:
private static BufferedImage createCharImage(Font font, char c) {
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = image.createGraphics();
// As you see, I set the font size to 100 for testing
font.awtFont.deriveFont(100);
graphics.setFont(font.awtFont);
FontMetrics fontMetrics = graphics.getFontMetrics();
graphics.dispose();
// Here the problem appears that the dimensions seem to be w: 1 and h: 3
Vector2i charDimensions = new Vector2i(fontMetrics.charWidth(c), fontMetrics.getHeight());
if (charDimensions.x == 0) {
return null;
}
image = new BufferedImage(charDimensions.x, charDimensions.y, BufferedImage.TYPE_INT_ARGB);
graphics = image.createGraphics();
graphics.setFont(font.awtFont);
graphics.setPaint(java.awt.Color.WHITE);
graphics.drawString(String.valueOf(c), 0, fontMetrics.getAscent());
graphics.dispose();
return image;
}
是的,有人可以帮助我吗?
或者,如果你有另一个想法来解决这个问题,请告诉我
答案 0 :(得分:1)
代码中的行font.awtFont.deriveFont(100);
目前除了浪费时间之外什么也没做,只需创建一个新字体,然后立即丢弃它并使用原始字体。
Derive the new font然后使用它:
....
Font bloodyHuge = font.awtFont.deriveFont(100);
graphics.setFont(bloodyHuge);
...