How to output a String on multiple lines using Graphics

后端 未结 5 1894
清酒与你
清酒与你 2020-12-28 09:39

My Program overrides public void paint(Graphics g, int x, int y); in order to draw some stings using g.drawString(someString, x+10, y+30);

5条回答
  •  时光说笑
    2020-12-28 10:11

    Incrementally build your string, one word at a time, using Epaga's method to find the length of your string. Once the length is longer than your rectangle, remove the last word and print. Repeat until you run out of words.

    It sounds like a bad algorithm, but for each line, it's really O(screenWidth/averageCharacterWidth) => O(1).

    Still, use a StringBuffer to build your string!

提交回复
热议问题