3 Replies Latest reply on Jul 16, 2015 3:29 PM by rp0428

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




      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;



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

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