📄 style.html
字号:
You can create a <CODE>Font</CODE> object from a transformobject that provides information on how torotate, scale, move, or shear the font. This next exampleuses a shear transformation to draw slanted text. First, thestring is drawn without the transform, and then it is drawnin a different location with it. Here is the complete<A HREF="./Code/StillLife.java">StillLife.java</A> source code. <P><BLOCKQUOTE><IMG SRC="./Art/StillLife.gif"></BLOCKQUOTE><P><PRE> int w = getSize().width; int h = getSize().height;//Create transforms AffineTransform at = new AffineTransform(); at.setToTranslation(30, 50); AffineTransform fontAT = new AffineTransform(); fontAT.shear(0.2, 0.0);//Create fonts and text layouts FontRenderContext frc = g2.getFontRenderContext(); Font theFont = new Font("Times", Font.BOLD, w/25); String s = new String("Still Life with Text"); TextLayout tstring = new TextLayout(s, theFont, frc); Font theDerivedFont = theFont.deriveFont(fontAT); String str = new String("Still Life with Slanted Text"); TextLayout tstring2 = new TextLayout(str, theDerivedFont, frc);//Draw regular string g2.setColor(Color.blue); g2.transform(at); tstring.draw(g2, (float)0, (float)0);//Draw string with shear transformation on the font g2.setColor(Color.green); g2.transform(at); tstring2.draw(g2, (float)0, (float)0);</PRE><A NAME="image"></A><H3>Filling a Clipping Area with an Image</H3>This next example creates a clipping area from the text string <I>The Starry Night</I>, and uses Vincent van Gogh's painting, <I>The Starry Night</I>, to fill the clipping area. Here is thecomplete <A HREF="./Code/Starry.java">Starry.java</A> source code.<P><IMG SRC="./Art/starry.gif" ALIGN=LEFT ALT="The Starry Night by Vincent van Gogh">The example code creates a text layout with the string <I>The Starry Night</I> in Times Bold. Next, it gets the width of the <CODE>TextLayout</CODE> bounds. The <A HREF="other.html#bounds">bounds</A> contains all the pixels the layout can draw. <P>Then, it gets the outline ofthe text layout and uses the width of the bounds to calculate the X and Y positions for the origins of the layout. The outline is used to create a <CODE>Shape</CODE> object, and a <CODE>Rectangle</CODE>object is created from the bounds of the <CODE>Shape</CODE> object.<P>At this point, the graphics context color is set to blueand the <CODE>Shape</CODE> object is drawn into the graphics context.The image and code segment below show what has happenedso far.<BR CLEAR=ALL><P><BLOCKQUOTE><IMG SRC="./Art/StarryShape.gif"></BLOCKQUOTE><PRE>FontRenderContext frc = g2.getFontRenderContext();Font f = new Font("Times",Font.BOLD,w/10);String s = new String("The Starry Night");TextLayout tl = new TextLayout(s, f, frc);float sw = (float) tl.getBounds().getWidth();Shape shape = tl.getOutline(null,w/2-sw/2,h/4);Rectangle r = shape.getBounds();g2.setColor(Color.blue);g2.draw(shape);</PRE><P>Lastly, a clipping area is set on the graphics context usingthe <CODE>Shape</CODE> object, and the <CODE>starry.gif</CODE> image is drawn into the clipping area starting at the lower-left corner of the <CODE>Rectangle</CODE> object (<CODE>r.x</CODE> and <CODE>r.y</CODE>) and filling the full height and width of the Rectangle object.<P><BLOCKQUOTE><IMG SRC="./Art/Starry.gif"></BLOCKQUOTE><PRE>g2.setClip(shape);g2.drawImage(img, r.x, r.y, r.width, r.height, this);</PRE><A NAME="lines"></A><H3>Filling a Clipping Area with Lines</H3>For a slightly different effect, this next example creates a clipping area from the text layout <I>By</I>, fills the clipping area with blue, and draws yellow lines across the clipping area over the blue fill. Information returned by the <CODE>TextLayout</CODE><A HREF="other.html#ascent"><CODE>getAscent</CODE></A> method is used to position the line on the display.<P>Here is the complete<A HREF="./Code/Clipping.java">Clipping.java</A> source code.<P><BLOCKQUOTE><IMG SRC="./Art/Clipping.gif"></BLOCKQUOTE><PRE>FontRenderContext frc = g2.getFontRenderContext();Font f = new Font("Helvetica",Font.BOLD,w/8);String s = new String("By");TextLayout tl = new TextLayout(s, f, frc);float sw = (float) tl.getBounds().getWidth();Shape shape = tl.getOutline(null,w/2-sw/2,h/2);g2.setClip(shape);g2.setColor(Color.blue);g2.fill(shape.getBounds());g2.setColor(Color.yellow);for (int j = shape.getBounds().y; j < shape.getBounds().y + shape.getBounds ().height; j=j+3) { Line2D line = new Line2D.Float( 0.0f, (float) j, (float) w, (float) j); g2.draw(line);}</PRE><A NAME="characters"></A><H3>Filling a Clipping Area with Characters</H3>This next example creates a clipping area from the textlayout <I>Vincent van Gogh</I>, fills it with blue, anddraws white asterisks (*) into the clipping area over theblue fill to give the effect of white stars in a blue sky.Here is the complete<A HREF="./Code/TextClipping.java">TextClipping.java</A> source code.<P><BLOCKQUOTE><IMG SRC="./Art/Vincent.gif"></BLOCKQUOTE><PRE>FontRenderContext frc = g2.getFontRenderContext();Font f = new Font("Times New Roman Bold",Font.PLAIN,w/8);String s = new String("Vincent van Gogh");TextLayout tl = new TextLayout(s, f, frc);float sw = (float) tl.getBounds().getWidth();Shape shape = tl.getOutline(null,w/2-sw/2,h-h/6);g2.setClip(shape);g2.setColor(Color.blue);g2.fill(shape.getBounds());g2.setColor(Color.white);f = new Font("Helvetica",Font.BOLD,10);tl = new TextLayout("+", f, frc);sw = (float) tl.getBounds().getWidth();Rectangle r = shape.getBounds();int x = r.x;int y = r.y;while ( y < (r.y + r.height+(int) tl.getAscent()) ) { tl.draw(g2, x, y); if ((x += (int) sw) > (r.x+r.width)) { x = r.x; y += (int) tl.getAscent(); }}</PRE> <A NAME="replace"></A><H3>Text Attributes and Replacement Graphics</H3>The <CODE>TextAttribute</CODE> class defines text attributes soyou can define an <CODE>Attributed</CODE> string with textattributes. This next example uses text attributes to definethe font and a character replacement image for the<CODE>java.awt.text.AttributedString</CODE> that contains thetext <I>The Starry Night</I>. An <CODE>ImageGraphicAttribute</CODE> is created from an image to do this. Here is the complete <A HREF="./Code/MakeImage.java">MakeImage.java</A> source code.<BLOCKQUOTE><IMG SRC="./Art/CharReplace.gif"></BLOCKQUOTE><PRE>public void paint(Graphics g) { Graphics2D g2; g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); Font f = new Font("Times",Font.BOLD, 24); AttributedString ats = new AttributedString("The Starry Night"); ImageGraphicAttribute iga = new ImageGraphicAttribute(img, (int) BOTTOM_ALIGNMENT); ats.addAttribute( TextAttribute.CHAR_REPLACEMENT, iga, 4, 5); ats.addAttribute( TextAttribute.CHAR_REPLACEMENT, iga, 11, 12); ats.addAttribute(TextAttribute.FONT, f, 0, 18); AttributedCharacterIterator iter = ats.getIterator(); FontRenderContext frc = g2.getFontRenderContext(); tl = new TextLayout(iter, frc); g2.setColor(Color.red); tl.draw(g2, (float)20, (float)30);}</PRE></FONT></TD></TR></TABLE><!-- ================ --><!-- End Main Content --><!-- ================ --></FONT></TD></TR></TABLE><!-- Copyright Insert --><BR CLEAR="ALL"><FORM ACTION="/cgi-bin/search.cgi" METHOD="POST"><TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="5"> <TR> <TD VALIGN="TOP"> <P ALIGN=CENTER> <FONT SIZE="-1" COLOR="#999999" FACE="Verdana, Arial, Helvetica, sans-serif"> [ This page was updated: <!-- new date --> 12-Nov-99 ]</font></P> </TD> </TR> <TR> <TD BGCOLOR="#CCCCCC"> <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD> </TR> <TR> <TD> <CENTER> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> <A HREF="http://java.sun.com/products/">Products & APIs</A> | <A HREF="/developer/index.html">Developer Connection</A> | <A HREF="/developer/infodocs/index.shtml">Docs & Training</A> | <A HREF="/developer/support/index.html">Online Support</A><BR> <A HREF="/developer/community/index.html">Community Discussion</A> | <A HREF="http://java.sun.com/industry/">Industry News</A> | <A HREF="http://java.sun.com/solutions">Solutions Marketplace</A> | <A HREF="http://java.sun.com/casestudies">Case Studies</A> </FONT> </CENTER> </TD> </TR> <TR> <TD BGCOLOR="#CCCCCC"> <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD> </TR> <TR> <TD ALIGN="CENTER"> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> <A HREF="http://java.sun.com/docs/glossary.html">Glossary</A> - <A HREF="http://java.sun.com/applets/">Applets</A> - <A HREF="http://java.sun.com/docs/books/tutorial/">Tutorial</A> - <A HREF="http://java.sun.com/jobs/">Employment</A> - <A HREF="http://java.sun.com/nav/business/">Business & Licensing</A> - <A HREF="http://java.sun.com/javastore/">Java Store</A> - <A HREF="http://java.sun.com/casestudies/">Java in the Real World</A> </FONT> </TD> </TR> <TR> <TD> <CENTER> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> <a href="/siteinfo/faq.html">FAQ</a> | <a href="/feedback/index.html">Feedback</a> | <a href="http://www.dynamicdiagrams.net/mapa/cgi-bin/help.tcl?db=javasoft&dest=http://java.sun.com/">Map</a> | <A HREF="http://java.sun.com/a-z/index.html">A-Z Index</A> </FONT> </CENTER> </TD> </TR> <TR> <TD> <TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="0"> <TR> <TD WIDTH="50%"> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> For more information on Java technology<BR> and other software from Sun Microsystems, call:<BR> </FONT> <FONT SIZE="-1" FACE="Verdana, Arial, Helvetica, sans-serif"> (800) 786-7638<BR></FONT> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> Outside the U.S. and Canada, dial your country's <A HREF="http://www.att.com/business_traveler/attdirecttollfree/">AT&T Direct Access Number</A> first.<BR> </FONT> </TD> <TD ALIGN="RIGHT" WIDTH="50%"> <A HREF="http://www.sun.com"><IMG SRC="/images/lgsun.gif" width="64" height="30" border="0" ALT="Sun Microsystems, Inc."></A><BR> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> Copyright © 1995-99 <A HREF="http://www.sun.com">Sun Microsystems, Inc.</A><BR> All Rights Reserved. <a href="http://www.sun.com/share/text/SMICopyright.html">Legal Terms</a>. <A HREF="http://www.sun.com/privacy/">Privacy Policy</A>. </FONT> </TD> </TR> </TABLE> </TD> </TR> </TABLE></FORM><!-- End Copyright Insert --></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -