Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 2 ■ an IntroduCtIon to Content CreatIon: 2d new MedIa asset FundaMentals

the individual graphic image elements to control parts of your gameplay screen. Without a 2D compositing
approach, you would not otherwise be able to individually control game components, as pixel-by-pixel
processing is too processing intensive for most devices.
There is another part of image compositing, called blending modes, that also factors heavily into
professional image compositing capabilities. JavaFX blending modes are applied by using the Blend class
with BlendMode constant values found in a javafx.scene.effect subpackage that we will be covering
later during this book. This JavaFX blend effect class gives Java game developers many of the same image
compositing modes that Photoshop or GIMP affords to a digital imaging artisan. This turns Java and JavaFX
into a powerful image compositing engine, just like GIMP, and the blending algorithms are controllable
at a very flexible level, using custom Java code. Some JavaFX blending mode constants include the ADD,
SCREEN, OVERL AY, DARKEN, LIGHTEN, MULTIPLY, DIFFERENCE, EXCLUSION, SRC_ATOP,
SRC_OVER, SOFT_LIGHT, HARD_LIGHT, COLOR_BURN, and COLOR_DODGE constants.


Representing Color and Alpha in Java Game Logic: Using


Hexadecimal Notation


Now that you know what color depth and alpha channels are and that color and transparency are
represented by using a combination of four different alpha, red, green, and blue (ARGB) image channels
within any given digital image, it is now important to understand how, as programmers, we are supposed to
represent these four ARGB image color and transparency channel values in Java and in JavaFX.
In the Java programming language, color and alpha are not only used in 2D digital imagery, commonly
termed bitmap imagery, but are also used in 2D illustration, commonly termed vector imagery. Colors and
transparency values are also often used across a number of different color setting options. For instance, you
could set a background color (or a transparency value) for the JavaFX Stage, a Scene, a layout container such
as a StackPane, a vector shape fill, or a UI control, among other things, such as 3D asset characteristics. We
will be covering 3D and JavaFX in future chapters.
In Java and the JavaFX API, different levels of ARGB color intensity values are represented using
hexadecimal notation. Hexadecimal, or “hex” for short, is based on the original Base16 computer notation.
This was used long ago to represent 16 bits of data values. Unlike the more common Base10, which counts
from zero through 9, the Base16 notation counts from zero through F, where F represents the Base10 value of
15 (0 through 15 yields 16 data values).
Hexadecimal values in Java always start with a zero and an x, so the 24-bit color value for white would
look like this: 0xFFFFFF. This hexadecimal color value represents Java’s Color.WHITE constant and uses
no alpha channel. A 32-bit color value for white would look like 0xFFFFFFFF, with the alpha channel data
being fully opaque. White with a transparent alpha channel, which could not be white at all but rather would
be “clear,” is coded like this using hexadecimal: 0x00FFFFFF. I usually use 0x00000000 to represent a clear
(transparent) alpha+color value in Java code.
Each slot in a 24-bit hexadecimal representation represents one Base16 value, so getting the 256
values that we need for each RGB color will take 2 slots, as 16 times 16 equals 256. Therefore, to represent
the 24-bit image using hexadecimal notation, we would need to have six slots after the 0x to hold each
of those six hexadecimal data values (data pairs representing 256 levels of values each). If you multiply
16x16x16x16x16x16, you should get the 16,777,216 colors that are possible to address by using 24-bit, also
known as true-color digital image data.
The hexadecimal data slots represent RGB values in the following format: 0xRRGGBB. For the
Java constant Color.WHITE, all of the red, green and blue channels in the hexadecimal color data value
representation are at the full (maximum color value) luminosity setting. If you additively sum all of these
colors together, you will get white light.
The color yellow would be represented by the red and green channels being on and the blue channel
being off, so the hexadecimal representation for Color.YELLOW would therefore be 0xFFFF00 where both
the red and green channel slots are fully on (FF, or a 255 Base10 data value) and the blue channel slots are
fully off (00, or a zero value).

Free download pdf