USING COLORS WITH THE STANDARD GRAPHICS LIBRARY


Built-in colors. The simplest way to specify colors using standard draw is via a list of predefined colors. You can set the foreground color to blue with:

StdDraw.setPenColor(StdDraw.BLUE);
or clear the background to light gray with:
StdDraw.clear(StdDraw.LIGHT_GRAY);
The default foreground color is black and the default background color is white. Here is the complete list of predefined colors:
StdDraw.BLACK
StdDraw.BLUE
StdDraw.CYAN
StdDraw.DARK_GRAY
StdDraw.GRAY
StdDraw.GREEN
StdDraw.LIGHT_GRAY
StdDraw.MAGENTA
StdDraw.ORANGE
StdDraw.PINK
StdDraw.RED
StdDraw.WHITE
StdDraw.YELLOW
StdDraw.BOOK_BLUE
StdDraw.BOOK_LIGHT_BLUE
StdDraw.BOOK_RED

RGB colors. You can use the RGB color space to specify other colors. RGB format specifies a color by three integer values between 0 and 255, representing the combination of red, green, and blue in the color. This format is commonly used in television screens, computer monitors, digital cameras, and web pages. (The primary color green is used instead of yellow because yellow phosphors are more expensive to produce.)

StdDraw.setPenColor(255,   0,   0);   // red
StdDraw.setPenColor(  0, 255,   0);   // green
StdDraw.setPenColor(  0,   0, 255);   // blue
StdDraw.setPenColor(255, 255,   0);   // yellow
StdDraw.setPenColor(255, 255, 255);   // white
StdDraw.setPenColor(  0,   0,   0);   // black
StdDraw.setPenColor(100, 100, 100);   // gray
Note that if all three arguments are the same, you get a shade of gray.

User-defined colors. The data type java.awt.Color allows you to construct your own colors using RGB or HSB formats. (We'll introduce objects and the Color data type in Section 3.1.) To access the Color data type, you need to include the following statement at the beginning of your Java program:

import java.awt.Color;