Results 1 to 4 of 4
  1. #1
    ericmacau is offline Member
    Join Date
    Jul 2015
    Posts
    1
    Rep Power
    0

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

    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 have a look at the attached screen dump.
    Please help, how can I draw a line under the text with the same length?


    Java Code:
    .....
     
    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);
    Attached Thumbnails Attached Thumbnails LineBreakMeasurer calculate text length is not match the length of line!-screendump.png  

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: LineBreakMeasurer calculate text length is not match the length of line!

    There is an article about measuring text:

    https://docs.oracle.com/javase/tutor...uringtext.html
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: LineBreakMeasurer calculate text length is not match the length of line!

    Your code declares a Graphics2D g2d which is never initialized and two int variables paragraphStart and paragraphEnd which are never used. Also a nonexistent Font constructor (String, int, float), and 3 redundant code lines that get and then set the same Stroke to g2d.

    You need to re-read the API for Graphics#drawLine. The third parameter isn't the width of the line's bounding box.

    You could have got better help sooner if you had posted a SSCCE (Short, Self Contained, Correct (Compilable), Example).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: LineBreakMeasurer calculate text length is not match the length of line!

    Cross posted: https://community.oracle.com/thread/3768525

    Ignore the 'advice' given there.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. A program to calculate the average length
    By aminos in forum New To Java
    Replies: 3
    Last Post: 12-11-2013, 03:16 PM
  2. line length?
    By chins4tw in forum New To Java
    Replies: 3
    Last Post: 10-08-2013, 07:55 PM
  3. Length of an Array: length() or just length ?
    By Jack_Tauson_Sr in forum New To Java
    Replies: 8
    Last Post: 03-18-2013, 08:16 PM
  4. Text processing to calculate word length feature
    By rathinaprabu in forum Forum Lobby
    Replies: 3
    Last Post: 03-03-2012, 01:23 PM
  5. Can we set max line length, using iText library?
    By new_2_java in forum New To Java
    Replies: 0
    Last Post: 05-13-2011, 08:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •