28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 5 ■ An Introduction to Game Design: Concepts, Multimedia, and Using Scene Builder<br />

There are 256 levels of brightness for each red, green, and blue color value that is held for each pixel. This allows<br />

you to set 8 bits of value-controlling color brightness variation for each of the red, green, and blue values, from a<br />

minimum of 0 (#00, or off, all dark, or black) to a maximum of 255 (#FF, or fully on, maximum color contributed). The<br />

number of bits that are used to represent digital image pixel color is referred to as the color depth of the image.<br />

Common color depths used in the digital imaging industry include 8 bit, 16 bit, 24 bit and 32 bit. I will outline<br />

these here, along with their formats. The lowest color depth exists in 8-bit indexed color images. These feature a<br />

maximum of 256 color values and use GIF and PNG8 image formats to hold this indexed color type of data.<br />

A medium color depth image will feature a 16-bit color depth and will thus contain 65,536 colors (calculated as<br />

256 × 256). It is supported by the TARGA (TGA) and tagged image file format (TIFF) digital image formats. If you want to<br />

use digital image formats other than GIF, JPEG, and PNG in your <strong>Java</strong> 8 games, import the third-party ImageJ library.<br />

Truecolor color depth images will feature 24-bit color depth and will thus contain more than 16 million<br />

colors. This is calculated as 256 × 256 × 256, which yields 16,777,216 colors. File formats supporting 24-bit color<br />

depth include JPEG (or JPG), PNG, BMP, XCF, PSD, TGA, TIFF, and WebP. <strong>Java</strong>FX supports three of these: JPG,<br />

PNG24 (24 bit), and PNG32 (32 bit). Using 24-bit color depth will give you the highest quality level. This is why I am<br />

recommending the use of PNG24 or PNG32 for your <strong>Java</strong> games. Next, let’s take a look at how to represent image pixel<br />

transparency values through alpha channels and how these can be used for compositing digital imagery in real time<br />

in <strong>Java</strong> 8 games!<br />

Digital Image Compositing: Using Alpha Channels and Transparency in Layers<br />

Compositing is the process of seamlessly blending together multiple layers of digital imagery. As you might imagine,<br />

this is an extremely important concept for game design and development. Compositing is useful when you want to<br />

create an image on the display that appears as though it is one single image (or animation), when it is actually the<br />

seamless collection of two or more composited image layers. One of the principle reasons you would want to set up<br />

an image or animation composite is to allow programmatic control over various elements in those images, by having<br />

them on different layers.<br />

To accomplish this, you need to have an alpha channel transparency value, which you can use to control the<br />

precision of the blending amount of a given pixel with another pixel (in the same x, y image location) on other layers<br />

(above and below it).<br />

Like the other RGB channels, an alpha channel has 256 transparency levels. In <strong>Java</strong> programming the alpha<br />

channel is represented by the first two slots in a hexadecimal representation of #AARRGGBB data values (which I<br />

will be covering in detail in the next section). Alpha channel ARGB data values use eight slots (32 bit) of data rather<br />

than the six data slots (#RRGGBB) used in a 24-bit image, which is really a 32-bit image with zero alpha channel data.<br />

Therefore, a 24-bit (PNG24) image has no alpha channel and will not be used for compositing, unless it is the<br />

bottom image plate in a compositing layer stack. In contrast, PNG32 images will be used as compositing layers on<br />

top of PNG24 (background plate) or PNG32 (lower, z-order compositing layers), which will need this alpha channel<br />

capability to show through (via alpha channel transparency values) in certain pixel locations in the image composite.<br />

How do digital image alpha channels, and the concept of image compositing, factor into <strong>Java</strong> game design? The<br />

primary advantage is an ability to break the game play screen, and the sprites, projectiles, and background graphic<br />

elements that it includes, into a number of component layers. The reason for doing this is to be able to apply <strong>Java</strong> 8<br />

programming logic (or <strong>Java</strong>FX classes) to individual graphic image elements to control parts of your game play screen<br />

that you would not otherwise be able to control individually were it one single image.<br />

Another part of image compositing, called blending modes, also factors heavily into professional imagecompositing<br />

capabilities. <strong>Java</strong>FX blending modes are applied by using the Blend class with the BlendMode constant<br />

values found in the javafx.scene.effect subpackage (see Chapter 4). This <strong>Java</strong>FX blending effect package gives<br />

<strong>Java</strong> game developers many of the same image-compositing modes that Photoshop (and GIMP) afford to a digital<br />

imaging artisan. This turns <strong>Java</strong> 8 (via <strong>Java</strong>FX) into a powerful image-compositing engine, just like Photoshop, and<br />

the blending algorithms are controllable at a very flexible level, using custom <strong>Java</strong> 8 code. <strong>Java</strong>FX blending mode<br />

constants include ADD, SCREEN, OVERLAY, DARKEN, LIGHTEN, MULTIPLY, DIFFERENCE, EXCLUSION,<br />

SRC_ATOP, SRC_OVER, SOFT_LIGHT, HARD_LIGHT, COLOR_BURN, and COLOR_DODGE.<br />

108

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!