Java Reference
In-Depth Information
Display 18.19
Some Methods in the Class Color (part 2 of 2)
public Color( float r, float g, float 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.0 to 1.0 (inclusive).
public int getRed()
Returns the red component of the calling object. The returned value is in the range 0 to 255
(inclusive).
public int getGreen()
Returns the green component of the calling object. The returned value is in the range 0 to 255
(inclusive).
public int getBlue()
Returns the blue component of the calling object. The returned value is in the range 0 to 255
(inclusive).
public Color brighter()
Returns a brighter version of the calling object color.
public Color darker()
Returns a darker version of the calling object color.
public boolean equals(Object c)
Returns true if c is equal to the calling object color; otherwise, returns false .
PITFALL: Using doubles to Define a Color
Suppose you want to make a color that is made of half the possible amount of red,
half the possible amount of blue, and no green. The following seems reasonable:
Color purple = new Color(0.5, 0.0, 0.5);
However, this will produce a compiler error. The numbers 0.5 and 0.0 are considered
to be of type double , and this constructor requires arguments of type float (or of
type int ). So, an explicit type cast is required, as follows:
Color purple = new Color(( float )0.5, ( float )0.0, ( float )0.5);
Java does allow the following method of specifying that a number is of type float ,
which can be simpler than the previous line of code:
Color purple = new Color(0.5f, 0.0f, 0.5f);
(continued)
 
 
Search WWH ::




Custom Search