Like Adobe Photoshop and many other interactive drawing programs, the pen used to draw in an applet keeps track of a current foreground color and a current background color. The foreground color is the default color used by methods that draw text, lines and shapes. The background color is used by methods that erase objects.
By default, an applet's foreground color is black and its background color is white. There are methods for changing these defaults. The method "setForeground" can be used to change the foreground color and "setBackground" can be used to set the background color. For example, including the instruction
in a method would change the default color associated with the pen to red when that method was invoked. The new default color would apply to all drawing primitives (including those in other methods) until another "setForeground" command was issued.pen.setForeground("Red");
There are several ways to specify a color in either setForeground, setBackground or any of the drawing methods discussed later. The simplest method is to simply type the color's name in quotes as in the previous example. The number of names recognized by JavaTools, however, is limited to "Red", "Blue", "Green", "Yellow", "Black", and "White".
If a wider variety of colors is required, the colors needed must be specified by indicating the strength of the Red/Blue/Green components of the desired colors. For example, the color obtained by mixing pure red and blue could be made the foreground color by the command:
In general, any color can be obtained by including a color description of the form:pen.setForeground( new Color(255,0,255));
in which each of the color values falls between 0 and 255 (inclusive).new Color( red-value, green-value, blue-value)
An alternative approach is to describe colors using hexdecimal values (base 16). While this might normally be less pleasant than the use of decimal numbers discussed in the preceding paragraph, you have probably already used hexadecimal color codes in HTML files and discovered that you can easily obtain the hexadecimal code for a color using the PageSpinner "Paste Color Code" entry in the Edit menu. Once you know the hexadecimal code for a color, you can use a color description of the form:
to identify the color in a Java program. That is, you just attach the prefix "0x" to the hexadecimal value and then provide it as a parameter to the "Color" constructor method. As before, such color descriptions can then be used as parameters for other methods. For example, to set the background color to a light shade of gray you could use the instruction:new Color( 0xff00ff )
pen.setBackground( new Color(0x202020) );