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
********************************************************************