Colors

Each Graphics context has a current Color that can be set via Graphics.setColor. All drawing or writing into a graphics context, such as in the paint method of an applet, is done with respect to this color. Components may also use colors (foreground and background). For example, here is a simple applet that displays a red Label:



Corresponding source code:

import java.awt.*;
public class ColorTest extends java.applet.Applet {
    public void init() {
        Label lab = new Label("A Red Label");
        lab.setForeground(Color.red);
        add(lab);
    }
}

You can also set the Color for a Container and then all components added to that Container have use that Color.

Color is a class that encapsulates information about a color. You may create Color objects with RGB or HSV parameters, but a number of predefined colors (static variables) exist such as Color.red and Color.blue exist for your convenience.

Related Magercise(s):