how to make Rectangle in JFrame in java


here is the simple java code to showing how to make a rectangle in JFrame in java.

import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Color;

class DrawRectangle extends JFrame
{
public DrawRectangle()
{
//to  Set JFrame title
 super("Draw A Rectangle In JFrame");

 //Set default close operation for JFrame
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 //Set JFrame size
 setSize(500,500);

 //Make JFrame visible
 setVisible(true);
}

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

 //draw rectangle outline
 g.drawRect(50,50,300,100);

 //set color to Green
 g.setColor(Color.GREEN);

 //fill rectangle with GREEN color
 g.fillRect(50,50,300,100);
}

public static void main(String[]args)
{
 DrawRectangle rect=new DrawRectangle();
}
}

The code is ready to be used just compile it and execute:

The output will come something like this.

 

Advertisements

About tarasj

hi friends i to blog what i have learned .lets talk and share
This entry was posted in ..BASIC.. and tagged , . Bookmark the permalink.