📄 graphics rendering.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0093)http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Graphics.doc.html -->
<HTML><HEAD><TITLE>Graphics Rendering</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content=reference name=collection>
<META content="MSHTML 6.00.2900.3132" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff>
<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>
<CENTER><A name=51142>
<TABLE width="90%" border=0>
<TBODY>
<TR>
<TD align=right><FONT size=3>C H A P T E R</FONT><FONT size=7><IMG
src="Graphics Rendering.files/sm-space.gif">10</FONT></TD></TR></TBODY></TABLE></A></CENTER>
<CENTER><A name=51143>
<TABLE width="90%" border=0>
<TBODY>
<TR>
<TD align=right>
<HR noShade SIZE=7>
<FONT size=6>Graphics Rendering</FONT></TD></TR></TBODY></TABLE></A></CENTER>
<BLOCKQUOTE>
<P><BR><BR><BR>
<P><FONT size=7><B>T</B></FONT>HIS chapter describes the JAI presentation of
rendering shapes, text, and images.
<P><A name=51145>
<H2>10.1 <IMG
src="Graphics Rendering.files/space.gif">Introduction</H2></A>JAI provides
classes that support drawing operations beyond the <CODE>Graphics2D</CODE>
class. Three different types of graphics rendering are offered: simple 2D
graphics, renderable graphics, and tiled image graphics. These are described
in more detail in the sections that follow.
<P><A name=51772>
<HR>
<CENTER><IMG src="Graphics Rendering.files/Graphics.doc.anc.gif"></CENTER>
<HR>
</A><A name=51778>
<CENTER><FONT size=-1><B><I>Figure 10-1 </I><IMG
src="Graphics Rendering.files/sm-blank.gif" border=0> Simple Text and Line
Added to an Image</B></FONT></CENTER></A>
<P><A name=51359>
<H3>10.1.1 <IMG src="Graphics Rendering.files/space.gif">Simple 2D
Graphics</H3></A>The <CODE>Graphics2D</CODE> class extends the even simpler
<CODE>Graphics</CODE> class to provide more control over geometry, coordinate
transformations, color management, and text layout. <CODE>Graphics2D</CODE> is
the fundamental class for rendering two-dimensional shapes, text and images.
<CODE>Graphics2D</CODE> supports geometric rendering by providing a mechanism
for rendering virtually any geometric shape, draw styled lines of any width,
and fill geometric shapes with virtually any texture.
<P>The <CODE>BufferedImage.createGraphics</CODE> method creates a
<CODE>Graphics2D</CODE> object, which can then be used to draw into this
<CODE>BufferedImage</CODE>.
<P>Geometric shapes are provided through implementations of the
<CODE>Shape</CODE> interface, such as <CODE>Polygon</CODE>,
<CODE>Rectangle</CODE>, <CODE>CubicCurve2D</CODE>, and
<CODE>QuadCurve2D</CODE>. Fill and pen styles are provided through
implementations of the <CODE>Paint</CODE> and <CODE>Stroke</CODE> interfaces.
For example, the <CODE>Paint</CODE> interface supports <CODE>Color</CODE>,
<CODE>GradientPaint</CODE>, and <CODE>TexturePaint</CODE>. The
<CODE>Stroke</CODE> interface supports <CODE>BasicStroke</CODE>, which defines
a set of attributes for the outlines of graphics primitives.
<P>Text is added to graphics using the <CODE>Font</CODE> class, which
represents character fonts. A <CODE>Font</CODE> is defined by a collections of
<CODE>Glyphs</CODE>, which in turn are defined by individual
<CODE>Shapes</CODE>. Since text is represented by glyphs, text strings can
also be stroked and filled like other geometric objects.
<P><A name=51362>
<H3>10.1.2 <IMG src="Graphics Rendering.files/space.gif">Renderable
Graphics</H3></A>The <CODE>RenderableGraphics</CODE> class is an
implementation of <CODE>Graphics2D</CODE> with <CODE>RenderableImage</CODE>
semantics. This means that content may be drawn into the image using the
<CODE>Graphics2D</CODE> interface and later be turned into
<CODE>RenderedImages</CODE> with different resolutions and characteristics.
<P>The <CODE>RenderableGraphics</CODE> class allows you to store a sequence of
drawing commands and "replay" them at an arbitrary output resolution. By
serializing an instance of <CODE>RenderableGraphics</CODE>, you create a kind
of metafile for storing the graphical content.
<P>The methods in the <CODE>RenderableGraphics</CODE> class override the
methods in the <CODE>java.awt.Graphics</CODE> and
<CODE>java.awt.Graphics2D</CODE> classes. This means that you can use the
methods in <CODE>RenderableGraphics</CODE> to set your fonts and colors, to
create the graphics shapes and text, define a clipping path, and so on.
<P>The only method unique to <CODE>RenderableGraphics</CODE> is the
<CODE>createRendering</CODE> method, which creates a
<CODE>RenderedImage</CODE> that represents a rendering of the image using a
given <CODE>RenderContext</CODE>. This is the most general way to obtain a
rendering of a <CODE>RenderableImage</CODE>.
<P><A name=51781>
<H2>10.2 <IMG src="Graphics Rendering.files/space.gif">A Review of Graphics
Rendering</H2></A>To render a graphic object, you set up the
<CODE>Graphics2D</CODE> context and pass the graphic object to one of the
<CODE>Graphics2D</CODE> rendering methods. Before rendering the graphic
object, you first need to set certain state attributes that define how the
<CODE>Graphics2D</CODE> context displays the graphics. For example, you
specify:
<P>
<UL>
<LI>The stroke width
<P></P></LI></UL>
<UL>
<LI>How strokes are joined
<P></P></LI></UL>
<UL>
<LI>A clipping path to limit the area that is rendered
<P></P></LI></UL>
<UL>
<LI>Define colors and patterns to fill shapes with
<P></P></LI></UL><CODE>Graphics2D</CODE> defines several methods that add or
change attributes in the graphics context. Most of these methods take an
object that represents a particular attribute, such as a <CODE>Paint</CODE> or
<CODE>Stroke</CODE> object.
<P><A name=51790>
<H3>10.2.1 <IMG src="Graphics Rendering.files/space.gif">Overview of the
Rendering Process</H3></A>When a graphic object is rendered, the geometry,
image, and attribute information are combined to calculate which pixel values
must be changed on the display.
<P>The rendering process for a <CODE>Shape</CODE> is described into the
following four steps:
<P>
<UL>1. If the <CODE>Shape</CODE> is to be stroked, the <CODE>Stroke</CODE>
attribute in the <CODE>Graphics2D</CODE> context is used to generate a new
<CODE>Shape</CODE> that encompasses the stroked path.
<P>2. The coordinates of the <CODE>Shape</CODE>'s path are transformed from
user space into device coordinate space according to the transform attribute
in the <CODE>Graphics2D</CODE> context.
<P>3. The <CODE>Shape</CODE>'s path is clipped using the clip attribute in
the <CODE>Graphics2D</CODE> context.
<P>4. The remaining <CODE>Shape</CODE> is filled using the
<CODE>Paint</CODE> and <CODE>Composite</CODE> attributes in the
<CODE>Graphics2D</CODE> context.
<P></P></UL>Rendering text is similar to rendering a <CODE>Shape</CODE>, since
the text is rendered as glyphs and each glyph is a <CODE>Shape</CODE>.
However, you still must specify what <CODE>Font</CODE> to use for the text and
get the appropriate glyphs from the <CODE>Font</CODE> before rendering. The
attributes are described in more detail in the following sections.
<P><A name=51812>
<H3>10.2.2 <IMG src="Graphics Rendering.files/space.gif">Stroke
Attributes</H3></A>The <CODE>Graphics2D</CODE> <CODE>Stroke</CODE> attribute
defines the characteristics of strokes. The <CODE>BasicStroke</CODE> object is
used to define the stroke attributes for a <CODE>Graphics2D</CODE> context.
<CODE>BasicStroke</CODE> defines characteristics such as line width, endcap
style, segment join style, and pattern (solid or dashing). To change the
<CODE>Stroke</CODE> attribute in the <CODE>Graphics2D</CODE> context, you call
the <CODE>setStroke</CODE> method.
<P><A name=51997>
<H4>10.2.2.1 <IMG src="Graphics Rendering.files/space.gif">Line
Width</H4></A>The line width is specified in <EM>points</EM> (there are 72
points to the inch). To set the stroke width, create a
<CODE>BasicStroke</CODE> object with the desired width and call
<CODE>setStroke</CODE>. The following example sets the stroke width to 12
points.
<P><PRE><HR>
wideStroke = new BasicStroke(12.0);
</PRE><PRE> g2.setStroke(wideStroke);
<HR></PRE><A name=51840>
<H4>10.2.2.2 <IMG src="Graphics Rendering.files/space.gif">Endcap
Style</H4></A><A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Graphics.doc.html#51844">Table
10-1</A> lists the endcap style attributes.
<P>
<TABLE cellPadding=3 border=3>
<CAPTION><FONT size=-1><B><A name=51844><I>Table 10-1 </I><IMG
src="Graphics Rendering.files/sm-blank.gif" border=0> Endcap Styles
</A></B></FONT></CAPTION>
<TBODY>
<TR vAlign=top>
<TH><A name=51850>Appearance </A>
<TH><A name=51852>Attribute </A>
<TH><A name=51854>Description </A>
<TR vAlign=top>
<TD><A name=51856>
<HR>
<CENTER><IMG
src="Graphics Rendering.files/Graphics.doc.anc1.gif"></CENTER>
<HR>
</A><BR>
<TD><A name=51858>CAP_BUTT</A><BR>
<TD><A name=51860>Ends unclosed subpaths with no added
decoration.</A><BR>
<TR vAlign=top>
<TD><A name=51862>
<HR>
<CENTER><IMG
src="Graphics Rendering.files/Graphics.doc.anc2.gif"></CENTER>
<HR>
</A><BR>
<TD><A name=51864>CAP_ROUND</A><BR>
<TD><A name=51866>Ends unclosed subpaths with a round end cap that has a
radius equal to half the pen width.</A><BR>
<TR vAlign=top>
<TD><A name=51868>
<HR>
<CENTER><IMG
src="Graphics Rendering.files/Graphics.doc.anc3.gif"></CENTER>
<HR>
</A><BR>
<TD><A name=51870>CAP_SQUARED</A><BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -