Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有没有办法估计比例字体使用的像素数?我正在编写在 SVG 中创建图像并将其转换为 Java 中的 PNG 的软件。在这张图片中,我使用的是具有比例字体(16 号)的文本。我有时可以在图片中放置 26 个字符,有时只能放置 19 个字符。这是因为“WWW”比“li1”占用更多空间。如何估计字符串需要的像素数?
看看FontMetrics类 - 特别是 stringWidth() 方法。
如果您有g用于绘画的图形对象
g
g.getFontMetrics(f).stringWidth(message)