使用graphics2D环绕/旋转图像上的文本

时间:2010-11-04 07:25:59

标签: java

void multiLine (int x, int y, String label, Graphics2D g) {

    AffineTransform fontAT = new AffineTransform();
    Font theFont = g.getFont();
    fontAT.rotate(-Math.PI / 2);
    Font theDerivedFont = theFont.deriveFont(fontAT);
    g.setFont(theDerivedFont);

    AttributedString attrStr = new AttributedString(label);
    // Get iterator for string:
    AttributedCharacterIterator characterIterator = attrStr.getIterator();

    // Get font context from graphics:
    FontRenderContext fontRenderContext = g.getFontRenderContext();

    // Create measurer:
    LineBreakMeasurer measurer = new LineBreakMeasurer(characterIterator,
            fontRenderContext);

    while (measurer.getPosition() < characterIterator.getEndIndex()) {
        TextLayout textLayout = measurer.nextLayout(200);
        y += textLayout.getAscent(); //Have tried changing y to x
        textLayout.draw(g, x, y);

        y += textLayout.getDescent() + textLayout.getLeading(); //Have tried changing y to x
    }
    g.setFont(theFont);

}

我希望这可以垂直打印,但不会,有关如何解决此问题的任何想法。

这会输出水平包装的文本。

修改:更改问题以正确反映我要实现的目标

1 个答案:

答案 0 :(得分:0)

减少包装宽度。 200太大了,所以不包装。如果您希望每个字符都在新行上,请将其设置为0.

TextLayout textLayout = measurer.nextLayout(0);