⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drawing multiple lines of text.htm

📁 一个关于java字体设计的程序!
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0077)http://java.sun.com/docs/books/tutorial/2d/textandfonts/linebreakmeasure.html -->
<HTML><HEAD><TITLE>Drawing Multiple Lines of Text</TITLE>
<META content="text/html; charset=gb2312" http-equiv=Content-Type>
<SCRIPT language=JavaScript><!-- hidefunction openWin(term) {  url="../../information/glossary.html#" + term;  myWin= window.open(url, "Glossary",              "width=400,height=150,scrollbars=yes,status=no,toolbar=no,menubar=no");  myWin.focus();}//--></SCRIPT>

<META content="MSHTML 5.00.3813.800" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff link=#000099><B><FONT size=-1>The Java</FONT><SUP><FONT 
size=-2>TM</FONT></SUP> <FONT size=-1>Tutorial</FONT></B> <BR>
<TABLE width=550 summary="layout">
  <TBODY>
  <TR>
    <TD align=left vAlign=center><A 
      href="http://java.sun.com/docs/books/tutorial/2d/textandfonts/fontselection.html" 
      target=_top><IMG align=center alt="Previous Page" border=0 height=26 
      src="Drawing Multiple Lines of Text.files/PreviousArrow.gif" width=26></A> 
      <A href="http://java.sun.com/docs/books/tutorial/2d/TOC.html#textandfonts" 
      target=_top><IMG align=center alt="Lesson Contents" border=0 height=26 
      src="Drawing Multiple Lines of Text.files/TOCIcon.gif" width=26></A> <A 
      href="http://java.sun.com/docs/books/tutorial/2d/images/index.html" 
      target=_top><IMG align=center alt="Next Page" border=0 height=26 
      src="Drawing Multiple Lines of Text.files/NextArrow.gif" width=26></A></TD>
    <TD align=middle vAlign=center><FONT size=-1><A 
      href="http://java.sun.com/docs/books/tutorial/index.html" 
      target=_top>Start of Tutorial</A> &gt; <A 
      href="http://java.sun.com/docs/books/tutorial/2d/index.html" 
      target=_top>Start of Trail</A> &gt; <A 
      href="http://java.sun.com/docs/books/tutorial/2d/textandfonts/index.html" 
      target=_top>Start of Lesson</A> </FONT></TD>
    <TD align=right vAlign=center><FONT size=-1><A 
      href="http://java.sun.com/docs/books/tutorial/search.html" 
      target=_top>Search</A> <BR><A 
      href="http://java.sun.com/docs/books/tutorial/forms/sendusmail.html">Feedback 
      Form</A> </FONT></TD></TR></TBODY></TABLE><IMG align=bottom alt="" height=8 
src="Drawing Multiple Lines of Text.files/blueline.gif" width=550 
NATURALSIZEFLAG="3"> <BR><FONT size=-1><B>Trail</B>: 2D Graphics 
<BR><B>Lesson</B>: Working with Text and Fonts </FONT>
<H2>Drawing Multiple Lines of Text </H2>
<BLOCKQUOTE>If you have a paragraph of styled text that you would like to fit 
  within a specific width, you can use <CODE>LineBreakMeasurer</CODE>, which 
  allows styled text to be broken into lines that fit within a particular visual 
  advance. As you learned in the <A 
  href="http://java.sun.com/docs/books/tutorial/2d/display/index.html">Displaying 
  Graphics with Graphics2D</A> trail, a <CODE>TextLayout</CODE> object 
  represents unchangeable, styled character data, but it also allows access to 
  layout information. The <CODE>getAscent</CODE> and <CODE>getDescent</CODE> 
  methods of <CODE>TextLayout</CODE> return information about the font that is 
  used to position the lines in the component. The text is stored as an 
  <CODE>AttributedCharacterIterator</CODE> so the font and point size attributes 
  can be stored with the text. </BLOCKQUOTE>
<H3>Example: LineBreakSample</H3>
<BLOCKQUOTE>The following applet positions a paragraph of styled text within a 
  component, using <CODE>LineBreakMeasurer</CODE>, <CODE>TextLayout</CODE> and 
  <CODE>AttributedCharacterIterator</CODE>. 
  <P>
  <P>
  <CENTER><A 
  href="http://java.sun.com/docs/books/tutorial/2d/textandfonts/LineBreakSample.html" 
  target=_blank><IMG align=bottom alt="Click this figure to run the applet." 
  height=206 src="Drawing Multiple Lines of Text.files/LineBreak.gif" width=408 
  NATURALSIZEFLAG="3"></A><BR><EM>This is a picture of the applet's GUI. To run 
  the applet, click the picture. The applet will appear in a new browser 
  window.</EM></CENTER>
  <P></P>The complete code for this program is in <A 
  href="http://java.sun.com/docs/books/tutorial/2d/textandfonts/example-1dot2/LineBreakSample.java" 
  target=_blank><FONT 
  color=#bb000f><CODE>LineBreakSample.java</CODE></FONT></A><A 
  href="http://java.sun.com/docs/books/tutorial/2d/textandfonts/example-1dot2/LineBreakSample.java" 
  target=_blank><IMG align=absMiddle alt=" (in a .java source file)" border=0 
  height=11 src="Drawing Multiple Lines of Text.files/sourceIcon.gif" 
  width=11></A>. 
  <P>The following code creates an iterator with the string 
  <CODE>vanGogh</CODE>. The start and end of the iterator is retrieved and a new 
  <CODE>LineBreakMeasurer</CODE> is created from the iterator. 
  <BLOCKQUOTE><PRE>AttributedCharacterIterator paragraph = vanGogh.getIterator();
paragraphStart = paragraph.getBeginIndex();
paragraphEnd = paragraph.getEndIndex();
        
lineMeasurer = new LineBreakMeasurer(paragraph,
                            new FontRenderContext(null, false, false));
</PRE></BLOCKQUOTE>The size of the window is used to determine where the line 
  should break and a <CODE>TextLayout</CODE> object is created for each line in 
  the paragraph. 
  <BLOCKQUOTE><PRE>Dimension size = getSize();
float formatWidth = (float) size.width;    
float drawPosY = 0;
lineMeasurer.setPosition(paragraphStart);
        
while (lineMeasurer.getPosition() &lt; paragraphEnd) {    
   TextLayout layout = lineMeasurer.nextLayout(formatWidth);

   // Move y-coordinate by the ascent of the layout.
   drawPosY += layout.getAscent();
        
   /* Compute pen x position.  If the paragraph is
      right-to-left, we want to align the TextLayouts
      to the right edge of the panel. 
    */
   float drawPosX;
   if (layout.isLeftToRight()) {
       drawPosX = 0;
   }
   else {
     drawPosX = formatWidth - layout.getAdvance();
   }
            
   // Draw the TextLayout at (drawPosX, drawPosY). 
   layout.draw(graphics2D, drawPosX, drawPosY);
            
   // Move y-coordinate in preparation for next layout.
   drawPosY += layout.getDescent() + layout.getLeading();
}
</PRE></BLOCKQUOTE></BLOCKQUOTE><IMG align=bottom alt="" height=8 
src="Drawing Multiple Lines of Text.files/blueline.gif" width=550 
NATURALSIZEFLAG="3"> <BR>
<TABLE width=550 summary="layout">
  <TBODY>
  <TR>
    <TD align=left vAlign=center><A 
      href="http://java.sun.com/docs/books/tutorial/2d/textandfonts/fontselection.html" 
      target=_top><IMG align=center alt="Previous Page" border=0 height=26 
      src="Drawing Multiple Lines of Text.files/PreviousArrow.gif" width=26></A> 
      <A href="http://java.sun.com/docs/books/tutorial/2d/TOC.html#textandfonts" 
      target=_top><IMG align=center alt="Lesson Contents" border=0 height=26 
      src="Drawing Multiple Lines of Text.files/TOCIcon.gif" width=26></A> <A 
      href="http://java.sun.com/docs/books/tutorial/2d/images/index.html" 
      target=_top><IMG align=center alt="Next Page" border=0 height=26 
      src="Drawing Multiple Lines of Text.files/NextArrow.gif" width=26></A></TD>
    <TD align=middle vAlign=center><FONT size=-1><A 
      href="http://java.sun.com/docs/books/tutorial/index.html" 
      target=_top>Start of Tutorial</A> &gt; <A 
      href="http://java.sun.com/docs/books/tutorial/2d/index.html" 
      target=_top>Start of Trail</A> &gt; <A 
      href="http://java.sun.com/docs/books/tutorial/2d/textandfonts/index.html" 
      target=_top>Start of Lesson</A> </FONT></TD>
    <TD align=right vAlign=center><FONT size=-1><A 
      href="http://java.sun.com/docs/books/tutorial/search.html" 
      target=_top>Search</A> <BR><A 
      href="http://java.sun.com/docs/books/tutorial/forms/sendusmail.html">Feedback 
      Form</A> </FONT></TD></TR></TBODY></TABLE>
<P><FONT size=-1><A 
href="http://java.sun.com/docs/books/tutorial/information/copyright.html">Copyright</A> 
1995-2004 Sun Microsystems, Inc. All rights reserved. </FONT></P></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -