USING COLORS WITH THE STANDARD GRAPHICS LIBRARY


The Java class Color allows you to use a myriad of built-in colors. Or, you can create your own from RGB or HSB formats. (We'll introduce classes in Section 3.1.) To access the Color class, you need to include the following statement at the beginning of your Java program:

import java.awt.Color
You can now set the foreground color to blue using our standard graphics library with:
StdDraw.setColor(Color.blue);
or clear the background with
StdDraw.clear(Color.lightGray);
The default foreground color is black and the default background color is white.

Built-in colors. The following colors are built-in:

Color.black
Color.blue
Color.cyan
Color.darkGray
Color.gray
Color.green
Color.lightGray
Color.magenta
Color.orange
Color.pink
Color.red
Color.white
Color.yellow

User-defined colors. There are a number of ways to construct your own colors. For complete details check out the Java Color API. Unless you have extreme needs, the following examples will probably suffice: