1

I'm currently using this way: Drawing text with outline in java To write a text with outline, and this to create underline, background color etc: http://www.java2s.com/Code/Java/2D-Graphics-GUI/TextAttributeUnderlineandstrikethrough.htm

But it doesn't show text attributes if there is outline, when i check AttributedString#getIterator().getAttributes() it looks right:

{java.awt.font.TextAttribute(strikethrough)=true, java.awt.font.TextAttribute(foreground)=java.awt.Color[r=255,g=255,b=255], java.awt.font.TextAttribute(font)=java.awt.Font[family=Impact,name=Impact,style=bolditalic,size=80], java.awt.font.TextAttribute(underline)=0}

So the attributes are added.

Also I'm using

createGlyphVector(<getFontRenderContext>, AttributedString#getIterator)

Writes the text with outline correctly, but doesn't show the attributes.

AlexAc
  • 73
  • 1
  • 13

1 Answers1

0

Instead of font.createGlyphVector use java.awt.font.TextLayout:

TextLayout textlayout = new TextLayout(attributes.getIterator(), graphics.getFontRenderContext());
// the position of the shape at the graphic
Shape shape = textlayout.getOutline(AffineTransform.getTranslateInstance(xOffset, yOffset));
graphics.setColor(outlineColor);
// Choose another width / strength of the stroke on demand
graphics.setStroke(new BasicStroke(font.getSize2D() / 10.0f));
// draw the outline / stroke
graphics.draw(shape);
// draw / fill the letters / text
graphics.setColor(textColor);
graphics.fill(shape);