Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

LineBreakMeasurer calculate text length is not match the length of line!

ericmacau-830049Jul 16 2015 — edited Jul 16 2015

Hello,

I want to draw a line under a text. But the length that I calculate seems not equals to the length of the text. Therefore, when I draw a line under the text, the line is shorter than the text. Please see the attached image.

Please help, how can I draw a line under the text with the same length?

.....

Graphics2D g2d;

String theStr = "=====TEST==Produtos====";

AttributedString attrStr = new AttributedString(theStr);

attrStr.addAttribute(TextAttribute.FONT, new Font("Arial", Font.PLAIN, 12f));

AttributedCharacterIterator paragraph = attrStr.getIterator();

int paragraphStart = paragraph.getBeginIndex();

int paragraphEnd = paragraph.getEndIndex();

FontRenderContext frc = g2d.getFontRenderContext();

LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(paragraph, frc);

TextLayout layout = lineMeasurer.nextLayout(breakWidth);

layout.draw(g2d, 35f, 100f);

Stroke originalStroke = g2.getStroke();

Stroke lineStroke = originalStroke;

g2d.setStroke(lineStroke);

int lineWidth = (int)layout.getBounds().getWidth();

g2d.drawLine(35, 150, lineWidth, 150);

screendump.png

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 13 2015
Added on Jul 16 2015
3 comments
631 views