Java Reference
In-Depth Information
Defining Colors
Display 17.5 in Chapter 17 lists the standard colors in the class Color , which are
defined for you. If that table does not have the colors you want, you can use the class
Color to define your own colors. To understand how this is done, you need to first
know a few basic facts about colors. By mixing red, green, and blue light in varying
amounts, the human eye can be given the sensation of viewing any color the eye is
capable of seeing. This is what an ordinary television set does to produce all the colors
it displays. The television mixes red, green, and blue light and shines these lights on the
screen in differing amounts. This is often called the RGB color system , for obvious
reasons. Because a computer monitor is basically the same thing as a television set,
colors for computer monitors can be produced in the same way. The Java Color class
mixes amounts of red, green, and blue to produce any new color you might want.
When specifying the amount of each of the colors red, green, and blue, you can use
either integers in the range 0 to 255 (inclusive) or float values in the range 0.0 to 1.0
(inclusive). For example, brown is formed by mixing red and green. So, the following
defines a color called brown that will look like a shade of brown:
RGB color
system
Color
constructors
Color brown = new Color(200, 150, 0);
This color brown will have a 200.0/255 fraction of the maximum amount of red
possible, a 150.0/255 fraction of the maximum amount of green possible, and no
blue. If you want to use fractions to express the color, you can. The following is an
equivalent way of defining the same color brown :
Color brown =
new Color(( float )(200.0/255), ( float )(150.0/255), ( float )0.0);
You need the type casts (float) because the constructors for the class Color accept
only arguments of type int or float , and numbers such as 200.0/255 and 0.0 are
considered to be of type double , not of type float .
Some constructors for the class Color and some of the commonly used methods for
the class Color are summarized in Display 18.19 .
RGB Colors
The class Color uses the RGB method of creating colors. That means that every color is a
combination of the three colors red, green, and blue.
Display 18.19
Some Methods in the Class Color (part 1 of 2)
The class Color is in the java.awt package.
public Color( int r, int g, int b)
Constructor that creates a new Color with the specified RGB values. The parameters r, g , and b
must each be in the range 0 to 255 (inclusive).
 
Search WWH ::




Custom Search