Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6479801

java.awt.font.LineBreakMeasurer code incorrect

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P5
    • Resolution: Fixed
    • Affects Version/s: 6
    • Fix Version/s: 9
    • Component/s: client-libs
    • Labels:
    • Subcomponent:
      2d
    • Resolved In Build:
      b167
    • CPU:
      x86
    • OS:
      windows_xp

      Backports

        Description

        A DESCRIPTION OF THE PROBLEM :
        The first code example doesn't work. Further the name pen is misleading.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
            public void paint(Graphics graphics) {
                
                Point2D pt2d = new Point2D.Float(10, 10);
                Graphics2D g2d = (Graphics2D)graphics;
                FontRenderContext frc = g2d.getFontRenderContext();
                
                AttributedString paragraphText = new AttributedString(text);
                paragraphText.addAttribute(TextAttribute.FONT, new Font("sans", Font.PLAIN, 10));
                LineBreakMeasurer measurer = new LineBreakMeasurer(paragraphText.getIterator(), frc);
                float wrappingWidth = getSize().width - 10;
                 
                while (measurer.getPosition() < paragraphText.getIterator().getEndIndex()) {
                    
                    TextLayout layout = measurer.nextLayout(wrappingWidth);
                    
                    pt2d.setLocation(pt2d.getX(),pt2d.getY()+ layout.getAscent());
                    float dx = layout.isLeftToRight() ?
                        0 : (wrappingWidth - layout.getAdvance());
                    
                    layout.draw(g2d, (float)pt2d.getX() + dx, (float)pt2d.getY());
                    pt2d.setLocation(pt2d.getX(), pt2d.getY() + layout.getDescent() + layout.getLeading());
                }
            }

        ACTUAL -
        public void paint(Graphics graphics) {

             Point2D pen = new Point2D(10, 20);
             Graphics2D g2d = (Graphics2D)graphics;
             FontRenderContext frc = g2d.getFontRenderContext();

             // let styledText be an AttributedCharacterIterator containing at least
             // one character

             LineBreakMeasurer measurer = new LineBreakMeasurer(styledText, frc);
             float wrappingWidth = getSize().width - 15;

             while (measurer.getPosition() < fStyledText.length()) {

                 TextLayout layout = measurer.nextLayout(wrappingWidth);

                 pen.y += (layout.getAscent());
                 float dx = layout.isLeftToRight() ?
                     0 : (wrappingWidth - layout.getAdvance());

                 layout.draw(graphics, pen.x + dx, pen.y);
                 pen.y += layout.getDescent() + layout.getLeading();
             }
         }

        URL OF FAULTY DOCUMENTATION :
        http://java.sun.com/javase/6/docs/api/java/awt/font/LineBreakMeasurer.html

          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  prr Philip Race
                  Reporter:
                  tyao Ting-Yun Ingrid Yao (Inactive)
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  3 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved:
                    Imported:
                    Indexed: