There is no font characteristic in java.text.LineBreakMeasure class?

M

MaoXuePeng

Hello, erveryone!
I want to draw mutil-paragraphy strings in a image. My code is:
Font font = new Font("宋体",Font.PLAIN,15);
String str_test =
"看看效果吧,怎样,还行不?不行的话再修改罗!!";
java.text.AttributedString att_str = new
java.text.AttributedString(str_test);
FontRenderContext context = g2.getFontRenderContext();
TextLayout layout = new TextLayout(str_test,font,context);
float begin_x = 40f;
float begin_y = 50f;
System.out.println(g2.getFont());
layout.draw(g2,40f,50f);//Can draw with correct font
java.text.AttributedCharacterIterator it = att_str.getIterator();
java.awt.font.LineBreakMeasurer measure =
new java.awt.font.LineBreakMeasurer(it,context);
float x=0,y=0;
float formatWidth = image.getWidth() - begin_x;
int begin = it.getBeginIndex();
int end = it.getEndIndex();
measure.setPosition(begin);
//g2.setFont(font);
System.out.println(g2.getFont());
while(measure.getPosition()<end)
{
layout = measure.nextLayout(formatWidth);
//No font infomation? Why?

if(!layout.isLeftToRight())
{
x = formatWidth - layout.getAdvance();
}
else
{
x = 0;
}
y += layout.getAscent();
layout.draw(g2,x,y);
y += layout.getDescent() + layout.getLeading();
}
javax.imageio.ImageIO.write(image,"png",new
java.io.File("str_test.png"));
}

It seems that "layout = new TextLayout(...)" can retain the font
information and draw, but "layout = measure.nextLayout(..)" can
not(measure is a instance of LineBreakMeasure), Why?
Can somebody help me?

Thanks advance!
MaoXuePeng.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top