Technical Support
Discussion Forum
Online Training
Read About Java
Java In-Depth
Product Discounts
Membership Information

Java Cup Logo

JDC Home Page


Top
Back
Next
Online Training
shadowSearchFAQFeedback

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 can 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 to use that Color.

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

Related exercise: