How to draw rectangle in applet

Four coordinate are required For drawing the rectangle in the java applet. First two coordinate define the origin point of the rectangle. Origin point start from upper left corner and further x-coordinate increases width of the rectangle in right side and y-coordinate increases height of the rectangle in downward.

Syntax of drawRect in Java Applet

Graphics_Instance . drawRect(int X1, Int Y1, int Width, int Height);

If you want to set border color of the rectangle then use setColor( ) method of Graphics class, which is exist in java.awt package.

Example -

import java.awt.*;
import java.applet.*;
public class designline extends Applet
int width, height;

   public void init() {
      setBackground( Color.gray);

   public void paint( Graphics g ) {


      g.setColor( );
      g.drawRect( 11, 22, 101, 50 );
First to compile the code and create the class file for this
First to compile the code and create the class file for this
Prepare the HTML file in the same location with <applet> tag. Like

<applet code="designline" width="400" height="400" />

Save the .html file in same location also run in any browser.

How to draw rectangle in applet


Popular posts from this blog

difference between structure and union in C Language

Difference between Linear search and Binary Search in c language

Difference between static and dynamic websites