Java Reference
In-Depth Information
int intRed = 0xFF0000; // r = 0xFF, g = 0x00, b = 0x00;
Color red = new Color(intRed); // default alpha = 0xFF
int intGreen = 0x8800FF00; // alpha = 0x88, r = 0x00, g = 0xFF,
// b = 0x00
Color green = new Color(intGreen, true);
Using hex numbers makes for a compact representation that allows you to quickly
see the component values. In Section 10.14 we discuss bit handling and how to
access and modify the bytes in a pixel integer value.
Here is an example using the float three-parameter constructor:
Color blue = new Color (0.0f, 0.0f, 1.0f);// R,G,B values
// between 0.0 and 1.0
Partially transparent red and blue colors are created in these two examples:
Color transparentRed = new Color (0xFF, 0, 0, 0x33);
// R,G,B, Alpha
Color transparentBlue = new Color (0.0f, 0.0f, 1.0f, 0.5f);
// R,G,B, Alpha
The transparency factor on the image color determines what percentage of the
component's background color shows through.
For convenience the Color class definition provides several colors as class
constants, such as
Color.BLUE, Color.WHITE, Color.RED
See the Color class in the Java API Specifications for a list of all the color
constants. The original Color class in Java 1.0 used lower-case color constant
names - Color.blue , Color.white , etc. This violated the convention of
using all upper-case letters in names for constants, and so in Java 1.4 the
upper-case names were added while the lower-case names remain for backward
compatibility.
To set the color for the graphics context to use during drawing (i.e. the current
“pen” color):
g.setColor (Color c); // where g = Graphics object
The background color of a component, such as a panel, can be set using
setBackground (Color c)
Similarly, there are methods to obtain the colors currently in use. For example,
Color c = g.getColor ();
Search WWH ::




Custom Search