Java - Applet - setColor

setColor() is a method in the Graphics class in Java that is used to set the color of the graphics context. It takes a Color object as a parameter and sets the color of subsequent drawing operations.

Here is an example of how to use setColor():

import java.applet.Applet;
import java.awt.*;

public class MyGraphics extends Applet {
    public void paint(Graphics g) {
        // Set the color to red
        g.setColor(Color.RED);
        
        // Draw a rectangle with red color
        g.drawRect(50, 50, 100, 100);
        
        // Set the color to blue
        g.setColor(Color.BLUE);
        
        // Draw a filled oval with blue color
        g.fillOval(100, 100, 50, 50);
    }
}

In the example above, setColor() is used to set the color of the graphics context to red and blue, respectively, before drawing a rectangle and a filled oval with those colors.