Java- Write Text onto Image, then Write to Output File

前端 未结 3 1450
萌比男神i
萌比男神i 2021-01-13 01:33

I have an image on top of which I would like to write text that has multiple lines, is center-aligned, and dynamic (variable width). I\'ve tried using the drawString

3条回答
  •  半阙折子戏
    2021-01-13 02:06

    You should use a drawString().

    To properly center a text you need to calculate the width for a given font:

    Font font = getFont();
    FontMetrics metrics = getFontMetrics( font );
    int width = metrics.stringWidth( theString );
    

    And remember to add -Djava.awt.headless=true if you want to call in on server (without GUI).

提交回复
热议问题