在 Java Graphics2D 中获取字符串边界时出现问题



我想画一个字符串。 首先,我使用 getStringBounds 获取字符串边界:getStringBounds(String, FontRenderContext)

然后,我使用DrawString来绘制字符串。我不知道getStringBounds("x"和"y")的返回值是什么。有人可以帮我吗?

谢谢沙迪。

例如,对于一个字符串,它返回:X=0,Y=-11,宽度=20,长度=17。x 和 y 是什么意思?

对于大多数图形方法,您可以指定绘图的上/左位置。例如

g.drawImage(image, 0, 0, null);

但是,在绘制文本时,请指定绘图的下/左位置。因此,在您的情况下,您将使用:

g.drawString("string", 0, 11);

一般来说,我想你会在 drawString() 方法中使用 Y 值的绝对值。

在这里找到。

返回指定 FontRenderContext 中指定字符串的边界。边界用于布局字符串。

Parameters:
   str - the specified String
   frc - the specified FontRenderContext
Returns:
   a Rectangle2D that is the bounding box of the specified String in the specified FontRenderContext.

最新更新