Colors in Java
The basic scheme for representing a picture in a computer is to
break the picture down into small elements called pixels and
then represent the color of each pixel by a numeric code (this
idea is discussed in section 1.6 of the text). In most computer
languages, including Java, the color is specified by three numbers --
one representing the amount of red in the color, another the amount
of green, and the third the amount of blue. These numbers are referred to
as the RGB value of the color. In Java, each of the three
primary colors is represented by an 8-bit code. Hence, the possible
base 10 values for each have a range of 0-255. Zero means none of that
color while 255 means the maximum amount of the color. Pure red is
represented by 255 for red, 0 for green, and 0 for blue, while magenta
is a mix of red and blue (255 for red, 0 for green, and 255 for blue).
In Java you can create your own colors. So far in the graphics programs
we have written we have used the pre-defined colors, such as Color.red,
from the Color class. However, we may also create our own Color object
and use it in a graphics program. One way to create a Color object
is to declare a variable of type Color and instantiate it using
the constructor that requires three integer parameters -- the first
representing the amount of red, the second the amount of green, and
the third the amount of blue in the color. For example, the following
declares the Color object myColor and instantiates it to a color
with code 255 for red, 0 for green, and 255 for blue.
Color myColor = new Color(255, 0, 255);
The statement page.setColor(myColor) then will set the foreground
color for the page to be the color defined by the myColor object.
The file Colors.java contains an applet that
defines myColor to be a Color object with color code (200, 100, 255) -
a shade of purple. Save the program and its associated HTML file
Colors.html to your directory, compile and
run it using the appletviewer. Now make the following modifications:
- Change the constructor so the color code is (0,0,0) --- absence of
color. What color should this be? Run the program to check.
- Try a few other combinations of color codes to see what you get. The description
of the Color class in Appendix M contains documentation that gives
the codes for the pre-defined colors.
- Now we will modify the program to generate random colors.
Notice in the Color class in Appendix M there is a constructor
that takes a single integer as an argument. The first 8 bits
of this integer are ignored while the last 24 bits define the color --
8 bits for red, 8 for green, and the last 8 bits for blue. Hence,
the bit pattern
00000000000000001111111100000000
should represent pure green. Its base 10 value is 65280. Change the
declaration of the myColor object to
Color myColor = new Color (65280);
Compile and run the program. Do you see green?
- Now add the following statements to the program:
- declare generator to be an object of type Random (the import statement
is already there);
- declare colorCode to be a variable of type int;
- assign colorCode a random integer value (use the nextInt())
method;
- replace the number 65025 in the Color constructor above with the
variable colorCode
Compile and run the program -- reload the program several times so you can
see the different random colors generated.
- The Color class has methods that return the individual color codes (for
red, green, and blue) for a Color object. For example,
redCode = myColor.getRed();
returns the code for the red component of the myColor object (redCode
should be a variable of type int). The methods
that return the green and blue components are getGreen and
getBlue, respectively. Add statements to the program, similar
to the above, to get the three color codes (you need to declare some
variables). Then add statements such as
page.drawString("Red: " + redCode, ____ , ____ );
to label the rectangle with the three color codes (fill in the
blanks with appropriate coordinates so each string is drawn inside
the rectangle -- you also need to set the drawing color to something such as
black so the strings will show up).
Compile and run the program to make sure it works. Reload several times
to see the different colors and their corresponding codes displayed.