gpt4 book ai didi

java - FontMetrics.stringWidth() 和 FontMetrics.getStringBounds() 有什么区别?

转载 作者:搜寻专家 更新时间:2023-10-31 19:30:38 25 4
gpt4 key购买 nike

我需要找到在屏幕上绘制的文本的宽度。 This thread建议 FontMetrics.stringWidth() 有时不如 FontMetrics.getStringBounds() 准确。

有谁知道这是否正确,如果正确,这种差异在什么情况下可见? FontMetrics.stringWidth() 似乎使用得更频繁,包括在 SwingUtilities.computeStringWidth() 中。此外,明显的优点是它不需要 Graphics 对象,但这本身与 Graphics 对象中定义的抗锯齿和类似设置的事实相结合可能解释了为什么 FontMetrics.getStringBounds() 可能更准确。

最佳答案

getStringBounds 有更多信息可以使用,因为它通过了 Graphics。它可以使用它来计算子像素字形放置并考虑抗锯齿对字形大小的影响。来自 FontRenderContext 的文档:

Anti-aliasing and Fractional-metrics specified by an application can also affect the size of a character because of rounding to pixel boundaries.

关于java - FontMetrics.stringWidth() 和 FontMetrics.getStringBounds() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7061060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com