Academic Java    Java Tutorial  >  Applets  >  setColor()

setColor() Example

Java setColor EXAMPLE output The default color for drawing using the Graphics parameter for paint() is black.

This can be changed using the method Graphics.setColor(). As shown in the example a Color object is the parameter of setColor().
// setColor() Example
import java.awt.*;
import javax.swing.*;

class SetColorExample extends JApplet {

   @Override public void paint(Graphics g) {
      super.paint(g);

      Color c0=Color.pink, c1=new Color(160,160,255);

      g.setColor(c0);
      g.fillRect(50,50,200,200);

      g.setColor(c1);
      g.fillRect(100,100,100,100);

   }
}