UNPKG

epubjs

Version:

Render ePub documents in the browser, across many devices

80 lines (79 loc) 10.6 kB
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head><title>Filling Shapes</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="Filling Shapes"><div class="titlepage"><div><div><h1 class="title"><a id="learnjava3-CHP-20-SECT-4"/>Filling Shapes</h1></div></div></div><p><code class="literal">Iguana</code> fills its shapes with a number of colors, using the <a id="I_indexterm20_id811480" class="indexterm"/><code class="literal">setPaint()</code> method of <code class="literal">Graphics2D</code>. This method sets the current color in the graphics context, so we set it to a different color before each drawing operation. <code class="literal">setPaint()</code> accepts any object that implements the <code class="literal">Paint</code> interface. The 2D API includes three implementations of this interface, representing solid colors, color gradients, and textures.</p><div class="sect2" title="Solid Colors"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-20-SECT-4.1"/>Solid Colors</h2></div></div></div><p><a id="idx11086" class="indexterm"/> <a id="idx11111" class="indexterm"/>The <a id="I_indexterm20_id811545" class="indexterm"/><code class="literal">java.awt.Color</code> class represents color in Java. A <code class="literal">Color</code> object describes a single color and implements the <code class="literal">Paint</code> interface for filling an area with it. You can create an arbitrary <code class="literal">Color</code> by specifying the red, green, and blue values, either as integers between 0 and 255 or as floating-point values between 0.0 and 1.0. The (somewhat strange) <a id="I_indexterm20_id811574" class="indexterm"/><code class="literal">getColor()</code> method can be used to look up a named color in the system properties table, as described in <a class="xref" href="ch11.html" title="Chapter 11. Core Utilities">Chapter 11</a>.</p><p>The <code class="literal">Color</code> class also defines a number of <code class="literal">static final</code> color values; we used these in the <code class="literal">Iguana</code> example. These constants, such as <code class="literal">Color.black</code> and <code class="literal">Color.red</code>, provide a convenient set of basic color objects for your drawings.</p><div class="tip" title="Tip"><h3 class="title">Tip</h3><p>Excessive creation of redundant color instances is a common cause of memory bloat in Java clients. Consider using a factory pattern to ensure you don’t have 200 instances of periwinkle.<a id="I_indexterm20_id811628" class="indexterm"/><a id="I_indexterm20_id811635" class="indexterm"/></p></div></div><div class="sect2" title="Color Gradients"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-20-SECT-4.2"/>Color Gradients</h2></div></div></div><p><a id="I_indexterm20_id811649" class="indexterm"/> <a id="I_indexterm20_id811659" class="indexterm"/>A <span class="emphasis"><em>color gradient</em></span> is a smooth blend between two or more colors. The <a id="I_indexterm20_id811674" class="indexterm"/><code class="literal">GradientPaint</code> class encapsulates this idea in a handy implementation of the <code class="literal">Paint</code> interface. All you need to do is specify two points and the color at each point. <code class="literal">GradientPaint</code> takes care of the details so that the color fades smoothly from one point to the other. In the previous example, the ellipse is filled with a gradient this way:</p><a id="I_20_tt1149"/><pre class="programlisting"> <code class="n">g2</code><code class="o">.</code><code class="na">setPaint</code><code class="o">(</code><code class="k">new</code> <code class="n">GradientPaint</code><code class="o">(</code><code class="mi">40</code><code class="o">,</code> <code class="mi">40</code><code class="o">,</code> <code class="n">Color</code><code class="o">.</code><code class="na">blue</code><code class="o">,</code> <code class="mi">60</code><code class="o">,</code> <code class="mi">50</code><code class="o">,</code> <code class="n">Color</code><code class="o">.</code><code class="na">white</code><code class="o">,</code> <code class="kc">true</code><code class="o">));</code></pre><p>The last parameter in <code class="literal">GradientPaint</code>’s constructor determines whether the gradient is <a id="I_indexterm20_id811715" class="indexterm"/><span class="emphasis"><em>cyclic</em></span>. In a cyclic gradient, the colors keep fluctuating beyond the two points that you’ve specified. Otherwise, the gradient just draws a single blend from one point to the other. Beyond each endpoint, the color is solid.</p><p>Java 6 added multistop gradient capabilities to <code class="literal">LinearGradientPaint</code> and <code class="literal">RadialGradientPaint</code>. A multistop gradient can, for example, smoothly fade from green to blue to red.</p></div><div class="sect2" title="Textures"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-20-SECT-4.3"/>Textures</h2></div></div></div><p><a id="I_indexterm20_id811749" class="indexterm"/> <a id="I_indexterm20_id811760" class="indexterm"/>A <span class="emphasis"><em>texture</em></span> is simply an image repeated over and over like a floor tile. This concept is represented in the 2D API with the <a id="I_indexterm20_id811775" class="indexterm"/><code class="literal">TexturePaint</code> class. To create a texture, just specify the image to be used and the rectangle that will be used to reproduce it. To do this, you also need to know how to create and use images, which we’ll get to a little later.</p></div><div class="sect2" title="Desktop Colors"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-20-SECT-4.4"/>Desktop Colors</h2></div></div></div><p><a id="idx11085" class="indexterm"/> <a id="idx11110" class="indexterm"/>The <code class="literal">Color</code> class makes it easy to construct a particular color; however, that’s not always what you want to do. Sometimes you want to match a preexisting color scheme. This is particularly important when you are designing a user interface; you might want your components to have the same colors as other components on that platform and to change automatically if the user redefines his or her color scheme.</p><p>That’s where the <a id="I_indexterm20_id811840" class="indexterm"/><code class="literal">SystemColor</code> class comes in. A system color represents the color used by the local windowing system in a certain context. The <code class="literal">SystemColor</code> class holds lots of predefined system colors, just like the <code class="literal">Color</code> class holds some predefined basic colors. For example, the field <a id="I_indexterm20_id811863" class="indexterm"/><code class="literal">activeCaption</code> represents the color used for the background of the titlebar of an active window; <a id="I_indexterm20_id811875" class="indexterm"/><code class="literal">activeCaptionText</code> represents the color used for the title itself. <a id="I_indexterm20_id811886" class="indexterm"/><code class="literal">menu</code> represents the background color of menu selections; <a id="I_indexterm20_id811902" class="indexterm"/><code class="literal">menuText</code> represents the color of a menu item’s text when it is not selected; <a id="I_indexterm20_id811919" class="indexterm"/><code class="literal">textHighlightText</code> is the color used when the menu item is selected; and so on. You could use the <a id="I_indexterm20_id811931" class="indexterm"/><code class="literal">window</code> value to set the color of a <code class="literal">Window</code> to match the other windows on the user’s screen—whether or not they’re generated by Java programs.</p><a id="I_20_tt1150"/><pre class="programlisting"> <code class="n">myWindow</code><code class="o">.</code><code class="na">setBackground</code><code class="o">(</code> <code class="n">SystemColor</code><code class="o">.</code><code class="na">window</code> <code class="o">);</code></pre><p>Because the <code class="literal">SystemColor</code> class is a subclass of <code class="literal">Color</code>, you can use it wherever you would use a <code class="literal">Color</code>. However, the <a id="I_indexterm20_id811977" class="indexterm"/><code class="literal">SystemColor</code> constants are tricky. They are constant, immutable objects as far as you, the programmer, are concerned (your code is not allowed to modify them), but they can be modified at runtime by the system. If the user changes his color scheme, the system colors are automatically updated to follow suit; as a result, anything displayed with system colors will automatically change color the next time it is redrawn. For example, the window <code class="literal">myWindow</code> would automatically change its background color to the new background color.</p><p>The <code class="literal">SystemColor</code> class has one noticeable shortcoming. You can’t compare a system color to a <code class="literal">Color</code> directly; the <code class="literal">Color.equals()</code> method doesn’t return reliable results. For example, if you want to find out whether the window background color is red, you can’t call:</p><a id="I_20_tt1151"/><pre class="programlisting"> <code class="n">Color</code><code class="o">.</code><code class="na">red</code><code class="o">.</code><code class="na">equals</code><code class="o">(</code><code class="n">SystemColor</code><code class="o">.</code><code class="na">window</code><code class="o">);</code></pre><p>Instead, you should use <code class="literal">getRGB()</code> to find the color components of both objects and compare them, rather than comparing the objects themselves.<a id="I_indexterm20_id812037" class="indexterm"/><a id="I_indexterm20_id812044" class="indexterm"/></p></div></div></body></html>