📄 graphics rendering.htm
字号:
}
</PRE>
<HR>
<P><A name=52230>
<H3>10.2.3 <IMG src="Graphics Rendering.files/space.gif">Rendering Graphics
Primitives</H3></A>The <CODE>Graphics2D</CODE> class provides methods for
creating <CODE>Shape</CODE>s and <CODE>Text</CODE>, and for rendering
<CODE>Images</CODE>. <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Graphics.doc.html#52234">Table
10-3</A> lists these methods.
<P>
<TABLE cellPadding=3 border=3>
<CAPTION><FONT size=-1><B><A name=52234><I>Table 10-3 </I><IMG
src="Graphics Rendering.files/sm-blank.gif" border=0> Graphics Primitives
Methods </A></B></FONT></CAPTION>
<TBODY>
<TR vAlign=top>
<TH><A name=52238>Method </A>
<TH><A name=52240>Description </A>
<TR vAlign=top>
<TD><A name=52242>draw</A><BR>
<TD><A name=52262>Strokes the outline of a Shape using the Stroke and
Paint settings of the current Graphics2D context.</A><BR>
<TR vAlign=top>
<TD><A name=52246>fill</A><BR>
<TD><A name=52269>Fills the interior of a Shape using the Paint settings
of the Graphics2D context.</A><BR>
<TR vAlign=top>
<TD><A name=52250>drawString</A><BR>
<TD><A name=52252>Renders the specified text string using the Paint
setting of the Graphics2D context.</A><BR>
<TR vAlign=top>
<TD><A name=52254>drawImage</A><BR>
<TD><A name=52256>Renders the specified Image.</A><BR>
<TR vAlign=top>
<TD><A name=52286>drawRenderableImage</A><BR>
<TD><A name=52288>Renders the specified RenderableImage.</A><BR>
<TR vAlign=top>
<TD><A name=52282>drawRenderedImage</A><BR>
<TD><A name=52284>Renders the specified
RenderedImage.</A><BR></TR></TBODY></TABLE>
<P><A name=52289>
<H4>10.2.3.1 <IMG src="Graphics Rendering.files/space.gif">Drawing a
Shape</H4></A>The <CODE>Graphics2D.draw</CODE> method is used to render the
outline of any <CODE>Shape</CODE>. The <CODE>Graphics2D</CODE> class also
inherits draw methods from the <CODE>Graphics</CODE> class, such as
<CODE>drawLine</CODE>, <CODE>drawRect</CODE>, <CODE>drawRoundRect</CODE>,
<CODE>drawOval</CODE>, <CODE>drawArc</CODE>, <CODE>drawPolyline</CODE>,
<CODE>drawPolygon</CODE>, and <CODE>draw3DRect</CODE>.
<P>When a <CODE>Shape</CODE> is drawn, its path is stroked with the
<CODE>Stroke</CODE> object in the <CODE>Graphics2D</CODE> context. (See <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Graphics.doc.html#51812">Section
10.2.2, "Stroke Attributes</A>," for more information.) By setting an
appropriate <CODE>BasicStroke</CODE> object in the <CODE>Graphics2D</CODE>
context, you can draw lines of any width or pattern. The
<CODE>BasicStroke</CODE> object also defines the line's endcap and join
attributes.
<P>To render a <CODE>Shape</CODE>'s outline:
<P>
<UL>1. Create the <CODE>BasicStroke</CODE> object
<P>2. Call <CODE>Graphics2D.setStroke</CODE>
<P>3. Create the <CODE>Shape</CODE>
<P>4. Call <CODE>Graphics2D.draw(shape)</CODE>
<P></P></UL><A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Graphics.doc.html#52316">Listing
10-4</A> shows a code example in which a <CODE>GeneralPath</CODE> object is
used to define a star and a <CODE>BasicStroke</CODE> object is added to the
<CODE>Graphics2D</CODE> context to define the star's line width and join
attributes.
<P><CAPTION><FONT size=-1><B><A name=52316>
<CENTER><FONT size=-1><B><I>Listing 10-4 </I><IMG
src="Graphics Rendering.files/sm-blank.gif" border=0> Example Drawing a Shape
</B></FONT></CENTER></A>
<P></B></FONT></CAPTION>
<HR>
<TR valign="top"><TD><PRE> public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
</PRE><TR valign="top"><TD><PRE> // Create and set the stroke.
g2.setStroke(new BasicStroke(4.0f));
</PRE><TR valign="top"><TD><PRE> // Create a star using a general path object.
GeneralPath p new GeneralPath(GeneralPath.NON_ZERO);
p.moveTo(- 100.0f, - 25.0f);
p.lineTo(+ 100.0f, - 25.0f);
p.lineTo(- 50.0f, + 100.0f);
p.lineTo(+ 0.0f, - 100.0f);
p.lineTo(+ 50.0f, + 100.0f);
p.closePath();
</PRE><TR valign="top"><TD><PRE> // Translate the origin towards the center of the canvas.
g2.translate(100.0f, 100.0f);
</PRE><TR valign="top"><TD><PRE> // Render the star's path.
g2.draw(p);
}
</PRE>
<HR>
<P><A name=52346>
<H4>10.2.3.2 <IMG src="Graphics Rendering.files/space.gif">Filling a
Shape</H4></A>The <CODE>Graphics2D.fill</CODE> method is used to fill any
<CODE>Shape</CODE>. When a <CODE>Shape</CODE> is filled, the area within its
path is rendered with the <CODE>Paint</CODE> object in the Graphics2D context:
a <CODE>Color</CODE>, <CODE>TexturePaint</CODE>, or
<CODE>GradientPaint</CODE>.
<P>The <CODE>Graphics2D</CODE> class also inherits fill methods from the
<CODE>Graphics</CODE> class, such as <CODE>fillRect</CODE>,
<CODE>fill3DRect</CODE>, <CODE>fillRoundRect</CODE>, <CODE>FillOval</CODE>,
<CODE>fillArc</CODE>, <CODE>fillPolygon</CODE>, and <CODE>clearRect</CODE>.
<P>To fill a <CODE>Shape</CODE>:
<P>
<UL>1. Set the fill color or pattern on the <CODE>Graphics2D</CODE> context
using <CODE>Graphics2D.setColor</CODE>, or <CODE>Graphics2DsetPaint</CODE>.
<P>2. Create the <CODE>Shape</CODE>
<P>3. Call <CODE>Graphics2D.fill</CODE> to render the <CODE>Shape</CODE>
<P></P></UL><A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Graphics.doc.html#52394">Listing
10-5</A> shows a code example in which the <CODE>setColor</CODE> method is
called to define a green fill for a <CODE>Rectangle2D</CODE>.
<P><CAPTION><FONT size=-1><B><A name=52394>
<CENTER><FONT size=-1><B><I>Listing 10-5 </I><IMG
src="Graphics Rendering.files/sm-blank.gif" border=0> Example Filling a
Shape</B></FONT></CENTER></A>
<P></B></FONT></CAPTION>
<HR>
<TR valign="top"><TD><PRE> Public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
</PRE><TR valign="top"><TD><PRE> g2.setpaint(Color.green);
Rectangle2D r2 = new Rectangle2D.float(25, 25, 150, 150);
</PRE><TR valign="top"><TD><PRE> g2.fill(r2);
}
</PRE>
<HR>
<P><A name=52415>
<H4>10.2.3.3 <IMG src="Graphics Rendering.files/space.gif">Rendering
Text</H4></A>The entire subject of fonts and text layout is too extensive to
try to describe here. In this section, we'll give a brief overview of the
<CODE>Graphics2D.drawString</CODE> method, which is used to render a text
string.
<P>There are two basic variations on the <CODE>drawString</CODE> method. Two
methods takes a <CODE>String</CODE> for an argument and two methods take an
<CODE>AttributedCharacterIterator</CODE>. If the argument is a
<CODE>String</CODE>, the current <CODE>Font</CODE> in the
<CODE>Graphics2D</CODE> context is used to convert the characters in the
<CODE>String</CODE> into a set of glyphs with whatever basic layout and
shaping algorithms the font implements. If the argument is an
<CODE>AttributedCharacterIterator</CODE>, the iterator is asked to convert
itself to a <CODE>TextLayout</CODE> using its embedded font attributes. The
<CODE>TextLayout</CODE> implements more sophisticated glyph layout algorithms
that perform Unicode I-directional layout adjustments automatically for
multiple fonts of differing writing directions.
<P>A third method used to render text is the
<CODE>Graphics2D.drawGlyphVector</CODE> method, which takes a
<CODE>GlyphVector</CODE> as an argument. The <CODE>GlyphVector</CODE> object
contains the appropriate font-specific glyph codes with explicit coordinates
for the position of each glyph.
<P>The character outlines are filled with the <CODE>Paint</CODE> object in the
Graphics2D context.
<P><A name=51398>
<H2>10.3 <IMG src="Graphics Rendering.files/space.gif">Graphics2D
Example</H2></A><A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Graphics.doc.html#51518">Listing
10-6</A> shows a code sample for a Graphics2D example.
<P><CAPTION><FONT size=-1><B><A name=51518>
<CENTER><FONT size=-1><B><I>Listing 10-6 </I><IMG
src="Graphics Rendering.files/sm-blank.gif" border=0> Graphics2D
Example</B></FONT></CENTER></A>
<P></B></FONT></CAPTION>
<HR>
<TR valign="top"><TD><PRE> // Read a RenderedImage and convert it to a BufferedImage.
imagePath = new String("./images/sample.jpg");
Image ai = loadAWTImage(imagePath, this);
RenderedImage ri = JAI.create("awtimage", ai);
BufferedImage bi = getBufferedImage(ri);
RenderedImage targetImage = null;
targetImage = new BufferedImage(bi.getWidth(),
bi.getHeight(),
bi.getType());
</PRE><TR valign="top"><TD><PRE> // Create a Graphics2D object to draw into the BufferedImage.
Graphics2D g2d = targetImage.createGraphics();
</PRE>
<HR>
<P><A name=51405>
<H2>10.4 <IMG src="Graphics Rendering.files/space.gif">Adding Graphics and
Text to an Image</H2></A>The <CODE>java.awt.Graphics2D</CODE> class enables
you to draw lines, geometric shapes, images, and text. These objects can then
be "painted" over a <CODE>TiledImage</CODE>.
<P>
<P>
<HR>
<BR>
<CENTER><A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/JAITOC.fm.html"><IMG
alt=Contents src="Graphics Rendering.files/contents.gif"></A> <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Analysis.doc.html"><IMG
alt=Previous src="Graphics Rendering.files/previous.gif"></A> <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Properties.doc.html"><IMG
alt=Next src="Graphics Rendering.files/next.gif"></A>
<P><FONT size=5><I>Programming in Java Advanced Imaging</I></FONT>
</CENTER><BR>
<H5><A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/copyright.html">Copyright</A>
© 1999, Sun Microsystems, Inc. All rights reserved.</H5><!-- Last updated: Tue Nov 02 18:04:40 1999 --></BLOCKQUOTE>
<SCRIPT language=JavaScript
src="Graphics Rendering.files/s_code_remote.js"></SCRIPT>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -