How to Fill Rectangles and Squares in Java


This example will use fillRect method of Graphics class to draw filled rectangles and squares in an applet window.

Source Code

package com.beginner.examples;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
 
public class DrawExample8 extends Applet{
 
	public void paint(Graphics g){
		
		setForeground(Color.red);
		
		//this will fill a rectangle
		g.fillRect(10, 10, 20, 40);
		
		//this will fill a square
		g.fillRect(100, 100, 30, 30);
		System.out.println("ok");
	}
}

Output:

ok

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments