Using the Color Class

Colors in Java are represented by the Color class, which includes the following constants as class variables: black, blue, cyan, darkGray, gray, green, lightGray, magenta, orange, pink, red, white, and yellow.

In a container, you can set the background color of the component using these constants by calling the setBackground(Color) method like this:

setBackground(Color.orange);

The current color, like the current font, must be set before drawing takes place using the setColor(Color) method. The following code includes a statement to set the current color to orange and draw text in that color:

public void paintComponent(Graphics comp) {
    Graphics2D comp2D = (Graphics2D) comp;
    comp2D.setColor(Color.orange);
    comp2D.drawString("Go, Buccaneers!", 5, 50);
}

Unlike the setBackground() method, which you can call directly on a container, you must call the setColor() method on a Graphics2D object.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.217.139