Draw square in JFrame

Source code below will show you, how to draw a square in a JFrame. For your information, square has 4 equal side. It mean it has 4 side with same length.

************************************************************************
COMPLETE SOURCE CODE FOR : DrawSquareInJFrame.java
************************************************************************


import javax.swing.JFrame;

import java.awt.Graphics;
import java.awt.Color;

public class DrawSquareInJFrame extends JFrame
{
public DrawSquareInJFrame()
{
//Set JFrame title
super("Draw A Square In JFrame");

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

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

//Make JFrame visible
setVisible(true);
}

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

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

//set color to RED
//So after this, if you draw anything, all of it's result will be RED
g.setColor(Color.RED);

//fill square with RED
g.fillRect(50,50,100,100);
}

public static void main(String[]args)
{
DrawSquareInJFrame dlijf=new DrawSquareInJFrame();
}
}


************************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************************

Draw oval in JFrame

Source code below will show you, how to draw an oval in a JFrame

*************************************************************
COMPLETE SOURCE CODE FOR : DrawOvalInJFrame.java
*************************************************************


import javax.swing.JFrame;

import java.awt.Graphics;
import java.awt.Color;

public class DrawOvalInJFrame extends JFrame
{
public DrawOvalInJFrame()
{
//Set JFrame title
super("Draw An Oval In JFrame");

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

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

//Make JFrame visible
setVisible(true);
}

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

//draw oval outline
g.drawOval(50,50,100,300);

//set color to RED
//So after this, if you draw anything, all of it's result will be RED
g.setColor(Color.RED);

//fill oval with RED
g.fillOval(50,50,100,300);
}

public static void main(String[]args)
{
DrawOvalInJFrame dlijf=new DrawOvalInJFrame();
}
}


*************************************************************
JUST COMPILE AND EXECUTE IT
*************************************************************

Draw circle in JFrame

Source code below will show you, how to draw a circle in a JFrame

*******************************************************************
COMPLETE SOURCE CODE FOR : DrawCircleInJFrame.java
*******************************************************************


import javax.swing.JFrame;

import java.awt.Graphics;
import java.awt.Color;

public class DrawCircleInJFrame extends JFrame
{
public DrawCircleInJFrame()
{
//Set JFrame title
super("Draw A Circle In JFrame");

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

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

//Make JFrame visible
setVisible(true);
}

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

//draw circle outline
g.drawOval(50,50,100,100);

//set color to RED
//So after this, if you draw anything, all of it's result will be RED
g.setColor(Color.RED);

//fill circle with RED
g.fillOval(50,50,100,100);
}

public static void main(String[]args)
{
DrawCircleInJFrame dlijf=new DrawCircleInJFrame();
}
}


*******************************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************************

Draw line in JFrame

Source code below will draw a line from top left corner of a JFrame until bottom right corner of the JFrame.

************************************************************************
COMPLETE SOURCE CODE FOR : DrawLineInJFrame.java
************************************************************************


import javax.swing.JFrame;

import java.awt.Graphics;

public class DrawLineInJFrame extends JFrame
{
public DrawLineInJFrame()
{
//Set JFrame title
super("Draw A Line In JFrame");

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

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

//Make JFrame visible
setVisible(true);
}

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

//draw line in JFrame from top left corner until bottom right corner
g.drawLine(0,0,getSize().width,getSize().height);
}

public static void main(String[]args)
{
DrawLineInJFrame dlijf=new DrawLineInJFrame();
}
}


************************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************************

Draw image in JFrame

Source code below will show you, how to draw image in JFrame. Image that will be use is drawImageIntoJFrame.jpg like below


drawImageIntoJFrame.jpg


********************************************************************
COMPLETE SOURCE CODE FOR : DrawImageOnJFrame.java
********************************************************************


import javax.swing.JFrame;
import javax.swing.ImageIcon;

import java.awt.Image;
import java.awt.Graphics;
import java.awt.Toolkit;

public class DrawImageOnJFrame extends JFrame
{
Image imageToBeDraw;
ImageIcon ii;

public DrawImageOnJFrame()
{
//set JFrame title
super("Draw Image On JFrame");

//Get image. You must change image location follow to image location in your computer.
imageToBeDraw=Toolkit.getDefaultToolkit().getImage("C:\\Documents and Settings\\evergreen\\Desktop\\drawImageIntoJFrame.jpg");

//Create an ImageIcon object
ii=new ImageIcon(imageToBeDraw);

//set close operation for JFrame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//set JFrame size follow the image size
setSize(ii.getIconWidth(),ii.getIconHeight());

//make you JFrame cannot resizable
setResizable(false);

//make JFrame visible. So we can see it.
setVisible(true);
}

public void paint(Graphics g)
{
//This will draw drawImageIntoJFrame.jpg into JFrame
g.drawImage(imageToBeDraw,0,0,this);
}

public static void main(String[]args)
{
DrawImageOnJFrame diojf=new DrawImageOnJFrame();
}
}


********************************************************************
JUST COMPILE AND EXECUTE IT
********************************************************************

Add JPanel into JFrame

Source code below will show you, how to add a JPanel into JFrame.

**************************************************************************
COMPLETE SOURCE CODE FOR : AddJPanelIntoJFrame.java
**************************************************************************


import javax.swing.JPanel;
import javax.swing.JFrame;

public class AddJPanelIntoJFrame
{
public static void main(String[]args)
{
//Create a panel
JPanel panel=new JPanel();

//Create a window using JFrame with title ( Add JPanel into JFrame )
JFrame frame=new JFrame("Add JPanel into JFrame");

//add created panel into JFrame
frame.add(panel);

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

//Set JFrame size to :
//Width : 400 pixels
//Height : 400 pixels
frame.setSize(400,400);

//Make JFrame visible. So we can see it
frame.setVisible(true);
}
}


**************************************************************************
JUST COMPILE AND EXECUTE IT
**************************************************************************