Home : Course Map : Chapter 6 : Java :
Colors
JavaTech
Course Map
Chapter 6

Introduction
AWT
Swing
Containers
  Demo 1
UI Components
  Demo 2
UI Layout
  Demo 3   Demo 4
Text Display
  Demo 5
Drawing
  Demo 6   Demo 7
Draw Polygons
  Demo 8   Demo 9
Colors
Text Draw 
  Demo 10
Images
  Demo 11
Exercises

    Supplements
AWT
  Demo 1
Drawing
  Demo 2
Text Drawing
  Demo 3
UI Components
  Demo 4

Java2D
Shapes & Areas
  Demo 1   Demo 2
Stroke & Paint
  Demo 3
Transforms
  Demo 4
Gradients&Textures
  Demo 5   Demo 6
Text
  Demo 7   Demo 8
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

Colors are defined by the class

    java.awt.Color

The sRGB (standard Red-Green-Blue) is the default colorspace. It spans a subset of the standard CIEXYZ color space that includes all visible colors (ref. Knudsen).

A color model, based on a particular color space, specifies exactly how a color is represented. In Java the default ARGB model includes the RGB components plus an alpha transparency factor that specifies what happens when a color is drawn over another. Other color models, such as a gray scale model, can also be obtained.

For convenience, there are several Color constructors. Some constructors take int values between 0 and 255 for each color component. Some take float values between 0.0f and 1.0f. There are 4-parameter constructors that let you specify the R, G, B, and alpha values and 3-parameter versions that default to opaque alpha. In integer format, alpha is 0 for completely transparent and 255 for opaque and in floating-point these are 0.0f/1.0f, respectively.

To specify an arbitrary color you use constructors with either int 0-255, or float 0.0-1.0 value ranges for each of the three color components:

  Color red = Color( 0xFF,0,0); // R,G,B, default alpha = 255
  Color blue = Color(0.0,0.0,1.0);// R,G,B, default alpha = 1.0

Note that it is common to use hexadecimal values to specify color components and here we used 0xFF instead of the decimal 255 for the red component. The three color components and alpha, each represented by a byte value, can be packed into an int. One constructor has a single int argument for packed RGB (alpha defaults to 0xFF) and another constructor has an int plus a boolean that is true if the integer value includes an alpha component as shown here

  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 Chapter 10 we discuss bit handling and how to access and modify the bytes in a pixel integer value.

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 class definition includes several colors as class constants, such as

   Color.BLUE,Color.WHITE, Color.RED, etc.

See the API Specifications for a list of all the color constants. (Lower case names, e.g. Color.blue, are legacies of Java 1.0. These capitalized versions follow the standard of all capital letters for constants.)

To set the current drawing color in the graphics context:

   g.setColor(Color c);//g = Graphics object

The background and foreground colors of components, such as for a panel, can be set using

  setBackground(Color c);
  setForeground(Color c);

Similarly, "getter" methods are paired with these methods to obtain the colors:

  Color c = g.getColor();//g = Graphics object

  // In an applet or other Component object:
  Color bg = getBackground();
  Color fg = getForeground();

 

References & Web Resources

 

Latest update: Oct. 27, 2005

            Tech
Java Tech Graphics
Starting to Plot
  Demo 1
Drawing Panel
  Demo 2
Histogram Display

  Demo 3
Exercises

           Physics
Display Text Data
  Demo 1
Plot Data
  Demo 2
Find Max/Min
  Demo 3
Exercises

  
  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.