epubjs
Version:
Render ePub documents in the browser, across many devices
58 lines (57 loc) • 7.21 kB
HTML
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>The Big Picture</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 Big Picture"><div class="titlepage"><div><div><h1 class="title"><a id="learnjava3-CHP-20-SECT-1"/>The Big Picture</h1></div></div></div><p><a id="idx11083" class="indexterm"/>The classes you’ll use for drawing come from six packages:
<a id="I_indexterm20_id809090" class="indexterm"/><code class="literal">java.awt</code>, <a id="I_indexterm20_id809100" class="indexterm"/><code class="literal">java.awt.color</code>,
<a id="I_indexterm20_id809111" class="indexterm"/><code class="literal">java.awt.font</code>, <a id="I_indexterm20_id809122" class="indexterm"/><code class="literal">java.awt.geom</code>, <a id="I_indexterm20_id809132" class="indexterm"/><code class="literal">java.awt.image</code>, and
<a id="I_indexterm20_id809143" class="indexterm"/><code class="literal">java.awt.print</code>.
Collectively, these classes make up most of the 2D API and cover the
drawing of shapes, text, and images. <a class="xref" href="ch20s01.html#learnjava3-CHP-20-FIG-1" title="Figure 20-1. Graphics classes of the 2D API">Figure 20-1</a> shows a bird’s-eye view of these
classes. There’s much more in the 2D API than we can cover in two
chapters. For a full treatment, see Jonathan Knudsen’s <span class="emphasis"><em><a class="ulink" href="http://shop.oreilly.com/product/9781565924840.do">Java 2D
Graphics</a></em></span> (O’Reilly).</p><p>An instance of <code class="literal">java.awt.Graphics2D</code> is called a
<span class="emphasis"><em>graphics context</em></span>. It represents a drawing
surface—such as a component’s display area, a page on a printer, or an
offscreen image buffer. A graphics context provides methods for drawing
three kinds of graphics objects: shapes, text, and images. <code class="literal">Graphics2D</code> is called a graphics context because
it also holds contextual information about the drawing area. This
information includes the drawing area’s clipping region, painting color,
transfer mode, text font, and geometric transformation. If you consider the drawing
area to be a painter’s canvas, you might think of a graphics context as an
easel that holds a set of tools and marks off the work area.</p><div class="figure"><a id="learnjava3-CHP-20-FIG-1"/><div class="figure-contents"><div class="mediaobject"><a id="I_20_tt1135"/><img src="httpatomoreillycomsourceoreillyimages1707691.png" alt="Graphics classes of the 2D API"/></div></div><p class="title">Figure 20-1. Graphics classes of the 2D API</p></div><p>There are four ways to acquire a <code class="literal">Graphics2D</code> object. The following list describes
them in order from the most common to the least:</p><div class="variablelist"><dl><dt><span class="term"><span class="emphasis"><em>From AWT or Swing as the result of a painting request
on a component</em></span></span></dt><dd><p>In this case, a new graphics context for the appropriate area
is created and passed to your component’s <a id="I_indexterm20_id809244" class="indexterm"/><code class="literal">paint()</code> or
<a id="I_indexterm20_id809255" class="indexterm"/><code class="literal">update()</code> method.
(The <code class="literal">update()</code> method really
applies only to AWT components, not the newer Swing
components.)</p></dd><dt><span class="term"><span class="emphasis"><em>Directly from an offscreen image
buffer</em></span></span></dt><dd><p>In this case, we ask the image buffer for a graphics context
directly. We’ll use this when we discuss techniques such as double
buffering.</p></dd><dt><span class="term"><span class="emphasis"><em>By copying an existing</em></span> <code class="literal">Graphics2D</code>
<span class="emphasis"><em>object</em></span></span></dt><dd><p>Duplicating a graphics object can be useful for more elaborate
drawing operations; different copies of a <code class="literal">Graphics2D</code> object can draw on the same
area, but with different attributes and clipping regions. A <code class="literal">Graphics2D</code> object can be copied by calling
the <code class="literal">create()</code> method.</p></dd><dt><span class="term"><span class="emphasis"><em>Directly from an onscreen component</em></span></span></dt><dd><p>It’s possible to ask a component to give you a <code class="literal">Graphics2D</code> object for its display area.
However, this is almost always a mistake; if you feel tempted to do
this, think about why you’re trying to circumvent the normal
<code class="literal">paint()</code>/<a id="I_indexterm20_id809339" class="indexterm"/><code class="literal">repaint()</code>
mechanism.</p></dd></dl></div><p>Each time a component’s <a id="I_indexterm20_id809353" class="indexterm"/><code class="literal">paint()</code> method is called,
the windowing system provides the component with a new <code class="literal">Graphics2D</code> object for drawing in the display
area. This means that attributes set during one painting session, such as
the drawing color or clipping region, are reset the next time <code class="literal">paint()</code> is called. (Each call to <code class="literal">paint()</code> starts with a tidy new easel.) For the
most common attributes, such as foreground color, background color, and
font, we can set defaults in the component itself. Thereafter, the
graphics contexts for painting in that component come with those
properties initialized appropriately.</p><p>The <code class="literal">paint()</code> method can make no
assumptions about what is already drawn on the screen. It is responsible
for rendering its entire work area. Higher-level APIs are normally
responsible for buffering output and limiting the number of times <code class="literal">paint()</code> is invoked for a component. AWT
components may use an additional method called <a id="I_indexterm20_id809405" class="indexterm"/><code class="literal">update()</code>, which allows
them to update their appearance under the assumption that their previous
artwork is still on the screen. However, this method is not used by Swing
components.</p><p>For backward compatibility, a graphics context is always passed to
the <code class="literal">paint()</code> method as an object of type
<code class="literal">Graphics</code>. If you want to take advantage
of the nifty features in the 2D API (as you almost undoubtedly will), you
need to cast this reference to a <code class="literal">Graphics2D</code> object. You’ll see how this works in
the upcoming examples.<a id="I_indexterm20_id809438" class="indexterm"/></p></div></body></html>