epubjs
Version:
Render ePub documents in the browser, across many devices
92 lines (91 loc) • 10.1 kB
HTML
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>The Rendering Pipeline</title><link rel="stylesheet" href="core.css" type="text/css"/><meta name="generator" content="DocBook XSL Stylesheets V1.74.0"/></head><body><div class="sect1" title="The Rendering Pipeline"><div class="titlepage"><div><div><h1 class="title"><a id="learnjava3-CHP-20-SECT-2"/>The Rendering Pipeline</h1></div></div></div><p><a id="idx11094" class="indexterm"/>One of the strengths of the 2D API is that shapes, text, and
images are manipulated in many of the same ways. In this section, we’ll
describe what happens to shapes, text, and images after you give them to a
<code class="literal">Graphics2D </code>object.
<span class="emphasis"><em>Rendering</em></span> is the process of taking some collection of
shapes, text, and images and figuring out how to represent them by
coloring pixels on a screen or printer. <code class="literal">Graphics2D</code> supports four rendering
operations:</p><div class="itemizedlist"><ul class="itemizedlist"><li class="listitem"><p>Draw a shape’s outline with the <code class="literal">draw()</code> method.</p></li><li class="listitem"><p>Fill a shape’s interior with the <a id="I_indexterm20_id809499" class="indexterm"/><code class="literal">fill()</code> method.</p></li><li class="listitem"><p>Draw some text with the <a id="I_indexterm20_id809513" class="indexterm"/><code class="literal">drawString()</code>
method.</p></li><li class="listitem"><p>Draw an image with any of the many forms of the <a id="I_indexterm20_id809527" class="indexterm"/><code class="literal">drawImage()</code>
method.</p></li></ul></div><p>The graphics context represented by a <code class="literal">Graphics2D</code> object holds the following
properties, whose values are controlled by corresponding accessor
methods—for example, <code class="literal">getFont()</code> and
<code class="literal">setFont()</code>:</p><div class="variablelist"><dl><dt><span class="term"><span class="emphasis"><em>Paint</em></span></span></dt><dd><p>The current <span class="emphasis"><em>paint</em></span> (an object of type
<code class="literal">java.awt.Paint</code>) determines what
color or pattern will be used to fill a shape. This affects the
drawing of shape outlines and text as well. You can change the
current paint using <code class="literal">Graphics2D</code>’s
<a id="I_indexterm20_id809583" class="indexterm"/><code class="literal">setPaint()</code> method.
Note that the <a id="I_indexterm20_id809594" class="indexterm"/><code class="literal">Color</code> class
implements the <code class="literal">Paint</code> interface,
so you can pass <code class="literal">Color</code>s to
<code class="literal">setPaint()</code> if you want to use
solid colors.</p></dd><dt><span class="term"><span class="emphasis"><em>Stroke</em></span></span></dt><dd><p><code class="literal">Graphics2D</code> uses the
<span class="emphasis"><em>stroke</em></span> to determine how to draw the outline of
shapes that are passed to its <a id="I_indexterm20_id809637" class="indexterm"/><code class="literal">draw()</code> method. In
graphics terminology, to “stroke” a shape means to take a path
defined by the shape and effectively trace it with a pen or brush of
a certain size and characteristics. For example, drawing the shape
of a circle using a stroke that acts like a solid line would yield a
washer or ring shape. The <code class="literal">stroke</code>
object in the <code class="literal">Graphics2D</code> API is a
little more abstract than that. It accepts the input shape to be
stroked and returns an enclosed shape representing the outline,
which <code class="literal">Graphics2D</code> then fills. You
can set the current stroke by using <a id="I_indexterm20_id809672" class="indexterm"/><code class="literal">setStroke()</code>. The 2D
API comes with a handy class, <a id="I_indexterm20_id809683" class="indexterm"/><code class="literal">java.awt.BasicStroke</code>, that implements
different line widths, end styles, join styles, and dashing.</p></dd><dt><span class="term"><span class="emphasis"><em>Font</em></span></span></dt><dd><p>Text is rendered by creating a shape that represents the
characters to be drawn. The current <span class="emphasis"><em>font</em></span>
determines the shapes that are created for a given set of
characters. The resulting text shape is then filled. The current
font is set using <a id="I_indexterm20_id809708" class="indexterm"/><code class="literal">setFont()</code>. The 2D
API gives applications access to all the TrueType and PostScript
Type 1 fonts that are installed. As of Java 7, OpenType/CFF fonts
are also supported.</p></dd><dt><span class="term"><span class="emphasis"><em>Transformation</em></span></span></dt><dd><p>Shapes, text, and images can be geometrically
<span class="emphasis"><em>transformed</em></span> before they are rendered. This
means that they may be moved, rotated, and stretched. <code class="literal">Graphics2D</code>’s transformation converts
coordinates from “user space” to “device space.” By default,
<code class="literal">Graphics2D</code> uses a transformation
that maps 72 units in user space to one inch on the output device.
If you draw a line from point (0, 0) to point (72, 0) using the
default transformation, it will be one inch long, regardless of
whether it is drawn on your monitor or your printer. The current
transformation can be modified using the <a id="I_indexterm20_id809747" class="indexterm"/><code class="literal">translate()</code>,
<a id="I_indexterm20_id809758" class="indexterm"/><code class="literal">rotate()</code>,
<a id="I_indexterm20_id809768" class="indexterm"/><code class="literal">scale()</code>, and
<a id="I_indexterm20_id809779" class="indexterm"/><code class="literal">shear()</code>
methods.</p></dd><dt><span class="term"><span class="emphasis"><em>Compositing rule</em></span></span></dt><dd><p>A <span class="emphasis"><em>compositing rule</em></span> determines how the
colors of a new drawing operation are combined with existing colors
on the <code class="literal">Graphics2D</code>’s drawing
surface. This attribute is set using <a id="I_indexterm20_id809808" class="indexterm"/><code class="literal">setComposite()</code>,
which accepts an instance of <a id="I_indexterm20_id809819" class="indexterm"/><code class="literal">java.awt.AlphaComposite</code>. Compositing
allows you to make parts of a drawing or image completely or
partially transparent, or to combine them in other interesting
ways.</p></dd><dt><span class="term"><span class="emphasis"><em>Clipping shape</em></span></span></dt><dd><p>All rendering operations are limited to the interior of the
<span class="emphasis"><em>clipping shape</em></span>. No pixels outside this shape
are modified. By default, the clipping shape allows rendering on the
entire drawing surface (usually, the rectangular area of a <code class="literal">Component</code>). However, you can further limit
this using any simple or complex shape (for example, text
shapes).</p></dd><dt><span class="term"><span class="emphasis"><em>Rendering hints</em></span></span></dt><dd><p>There are different techniques that can be used to render
graphics primitives. Usually these represent a tradeoff between
rendering speed and visual quality or vice versa. Rendering hints
(constants defined in the <code class="literal">RenderingHints</code> class) specify which
techniques to use.</p></dd></dl></div><p>Graphics primitives (shapes, text, and images) pass through the
rendering engine in a series of operations called the <span class="emphasis"><em>rendering
pipeline</em></span>. Let’s walk through the pipeline. It can be reduced to
four steps; the first step depends on the rendering operation:</p><div class="orderedlist"><ol class="orderedlist"><li class="listitem"><p>Transform the shape. For shapes that will be filled, the shape
is simply transformed using the <code class="literal">Graphics2D</code>’s current transformation. For
shapes whose outlines are drawn using <code class="literal">draw()</code>, the current stroke is used to stroke
the shape’s outline. Then the stroked outline is transformed like any
other filled shape. Text is displayed by mapping characters to shapes
using the current font. The resulting text shapes are transformed like
any other filled shape. Images are also transformed using the current
transformation.</p></li><li class="listitem"><p>Determine the colors to be used. For a filled shape, the current
<span class="emphasis"><em>paint</em></span> object determines the colors that should be
used to fill the shape. For drawing an image, the colors are taken
from the image itself.</p></li><li class="listitem"><p>Combine the colors with the existing drawing surface using the
current <span class="emphasis"><em>compositing rule</em></span>.</p></li><li class="listitem"><p>Clip the results using the current <span class="emphasis"><em>clipping
shape</em></span>.</p></li></ol></div><p>The <span class="emphasis"><em>rendering hints</em></span> are used throughout to
control the rendering quality.<a id="I_indexterm20_id809933" class="indexterm"/></p></div></body></html>