epubjs
Version:
Render ePub documents in the browser, across many devices
18 lines (17 loc) • 2.47 kB
HTML
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Stroking Shape Outlines</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="Stroking Shape Outlines"><div class="titlepage"><div><div><h1 class="title"><a id="learnjava3-CHP-20-SECT-5"/>Stroking Shape Outlines</h1></div></div></div><p><a id="I_indexterm20_id812058" class="indexterm"/> <a id="I_indexterm20_id812069" class="indexterm"/> <a id="I_indexterm20_id812078" class="indexterm"/>Just as a <code class="literal">Graphics2D</code>
object’s current paint determines how its shapes are filled, its current
stroke determines how its shapes are outlined. The current stroke
determines such drawing features as line thickness, line dashing, and end
styles. In the old days, lines were one pixel wide and that was that. With
Java 2D, line thickness can be set with floating-point accuracy and the
results, like everything else, are subject to the rendering pipeline’s
transformations and scaling.</p><p>To set the current stroke in <code class="literal">Graphics2D</code>, call <a id="I_indexterm20_id812107" class="indexterm"/><code class="literal">setStroke()</code> with any
implementation of the <code class="literal">Stroke</code> interface.
Fortunately, the 2D API includes a <a id="I_indexterm20_id812123" class="indexterm"/><code class="literal">BasicStroke</code> class that
probably does everything you need. Using <code class="literal">BasicStroke</code>, you can create dashed lines,
control the decoration that is added to line ends, and decide how the
corners in an outline should be drawn.</p><p>By default, <code class="literal">Graphics2D</code> uses a
solid stroke with a width of 1. In the previous <code class="literal">Iguana</code> example, the line width is changed just
before the outline of the rounded rectangle is drawn, like so:</p><a id="I_20_tt1152"/><pre class="programlisting"> <code class="n">g2</code><code class="o">.</code><code class="na">setStroke</code><code class="o">(</code><code class="k">new</code> <code class="n">BasicStroke</code><code class="o">(</code><code class="mi">4</code><code class="o">));</code></pre></div></body></html>