How to fill colors in shapes using Applet in Java



Problem Description

How to fill colors in shapes using Applet?

Solution

Following example demonstrates how to create an applet which will have fill color in a rectangle using setColor(), fillRect() methods of Graphics class to fill color in a Rectangle.

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

public class fillColor extends Applet {
   public void paint(Graphics g) {
      g.drawRect(300,150,200,100);
      g.setColor(Color.yellow);   
      g.fillRect( 300,150, 200, 100 );
      g.setColor(Color.magenta);
      g.drawString("Rectangle",500,150);
   }
}

Result

The above code sample will produce the following result in a java enabled web browser.

A Rectangle with yellow color filled in it will be drawn in the browser.
java_applets.htm
Advertisements