How to Draw Rectangles and squares in Java


In this example we will show how to draw rectangles and squares in an applet window with drawRect method of Graphics class.

Source Code

package com.beginner.examples;

import java.applet.Applet;
import java.awt.Graphics;
 
public class DrawExample6 extends Applet{
 
	public void paint(Graphics g){
		
		//this will draw a rectangle
		g.drawRect(10, 10, 20, 40);
		
		//this will draw a square
		g.drawRect(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