Java Reference
In-Depth Information
black has the RGB values 0, 0, 0. An RGB value of 100, 100, 100 creates a gray color
darker than one with an RGB value of 200, 200, 200.
In addition to the methods shown in Table 12-5, the
class
Color
defines a number of
standard colors as constants. Table 12-6 shows the color name in bold and its values for
easy reference.
TABLE 12-6
Constants Defined in the
class
Color
Color.black
: (0, 0, 0)
Color.magenta
: (255, 0, 255)
Color.blue
: (0, 0, 255)
Color.orange
: (255, 200, 0)
Color.cyan
: (0, 255, 255)
Color.pink
: (255, 175, 175)
Color.darkGray
: (64, 64, 64)
Color.red
: (255, 0, 0)
Color.gray
: (128, 128, 128)
Color.white
: (255, 255, 255)
Color.green
: (0, 255, 0)
Color.yellow
: (255, 255, 0)
Color.lightGray
: (192, 192, 192)
A simple applet to illustrate the use of the
class
Color
is given in Example 12-2. In this
example, we use the
class
GridLayout
, described in Chapter 6, to place the GUI
components. Recall that
GridLayout
divides the container into a grid of rows and
columns, allowing you to place the components in rows and columns. Every component
placed in a
GridLayout
will have the same width and height. The components are
placed from left to right in the first row, followed by left to right in the second row, and
so on.
The
class
GridLayout
is contained in the
package
java.awt
. Most often, we use the
following constructor of the
class
GridLayout
:
GridLayout(
int
row,
int
col)
where
row
specifies the number of rows and
col
specifies the number of columns in the
grid, respectively. For example, to create a grid with 10 rows and 5 columns, and set it as
the layout of the container
c
, you use the following Java statement:
c.setLayout(
new
GridLayout(10, 5));
In our next applet, we use a grid with 2 rows and 2 columns. Therefore, we use the
statement:
c.setLayout(
new
GridLayout(2, 2));
Example 12-2 uses the method
random
of the
class
Math
that returns a random value
between 0 and 1. For example, the statement:
Math.random();
Search WWH ::
Custom Search