How do you draw a string centered vertically in Java?

前端 未结 4 527
名媛妹妹
名媛妹妹 2020-12-24 03:00

I know it\'s a simple concept but I\'m struggling with the font metrics. Centering horizontally isn\'t too hard but vertically seems a bit difficult.

I\'ve tried usi

4条回答
  •  醉话见心
    2020-12-24 03:41

    Not sure this helps, but drawString(s, x, y) sets the baseline of the text at y.

    I was working with doing some vertical centering and couldn't get the text to look right until I noticed that behavior mentioned in the docs. I was assuming the bottom of the font was at y.

    For me, the fix was to subtract fm.getDescent() from the y-coordinate.

提交回复
热议问题