Java Reference
In-Depth Information
You can alter some aspects of the appearance of the basic component by calling methods for the object.
The following methods have an effect on the appearance of a
Component
object:
Method
Description
void
setBackground(
Color
aColor)
Sets the background color to
aColor
. The background color
is the color used for the basic component, as created by the
operating system.
Color
getBackground()
Retrieves the current background color.
void
setForeground(
Color
bColor)
Sets the foreground color to
bColor
. The foreground color
is the color used for anything appearing on the basic
component, such as the label on a button, for example.
Color
getForeground()
Retrieves the current foreground color.
void
setCursor(
Cursor
aCursor)
Sets the cursor for the component to
aCursor
. This sets the
appearance of the cursor within the area occupied by the
Component
object.
void
setFont(
Font
aFont)
Sets the font for the
Component
object.
Font getFont()
Returns the
Font
object used by the component.
To be able to make use of these properly, we need to understand what
Color
objects are, and we also
need to know how to create
Cursor
and
Font
objects.
Defining Color
A screen color is represented by an object of class
Color
. You define a color value as a combination of the
three primary colors: red, green, and blue. They are usually expressed in that sequence, and are often
referred to as
RGB
values
. There are other ways of specifying colors in Java, but we will confine ourselves to
RGB. You can specify the intensity of each primary color to be a value between 0 and 255. If the intensities
of all three are 0, you have the color black, and if all three are set to 255 you have white. If only one intensity
value is positive and the others are zero, you will have a pure primary color; for example (0, 200, 0) will be a
shade of green. We could define variables corresponding to these colors with the statements:
Color myBlack = new Color(0,0,0); // Color black
Color myWhite = new Color(255,255,255); // Color white
Color myGreen = new Color(0,200,0); // A shade of green
The three arguments to the constructor correspond to the intensities of the red, green, and blue
components of the color respectively. The
Color
class defines a number of standard color constants as
public
final
static
variables, whose RGB values are given in parentheses: