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

Discussion in 'Java' started by MaoXuePeng, Jan 10, 2005.

  1. MaoXuePeng

    MaoXuePeng Guest

    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.
     
    MaoXuePeng, Jan 10, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Karl Hungus

    Font class, external font files question

    Karl Hungus, Feb 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    534
    Karl Hungus
    Feb 4, 2004
  2. =?Utf-8?B?Tmlja3k=?=
    Replies:
    2
    Views:
    715
    Matt Berther
    Feb 20, 2005
  3. Michael Glavitsch
    Replies:
    1
    Views:
    769
    Roedy Green
    Jul 15, 2003
  4. gino
    Replies:
    3
    Views:
    3,989
    Mike Smith
    Jul 11, 2004
  5. mttc
    Replies:
    2
    Views:
    2,407
    Roedy Green
    Jul 3, 2009
Loading...

Share This Page