开发者

problems getting string Bounds in Java Graphics2D

I want to draw a string. at first, I get the string bounds using getStringBounds: getStringBounds(String, FontRenderContext)

开发者_运维技巧

and then, I use DrawString to draw the string. I do not know what the returned vales of getStringBounds ("x" and "y") are. Can anybody please help me?

Thanks, Shadi.


for example, for one string it returns: X=0, Y=-11, width=20, length=17. What do x and y mean?

With most Graphics methods you specify the top/left location for the drawing. For example

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

However, when you draw text you specify the bottom/left location for the drawing. So in your case you would use:

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

In general terms I guess you would use the absolute value of the Y value in the drawString() method.


Found here.

Returns the bounds of the specified String in the specified FontRenderContext. The bounds is used to layout the String.

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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜