Share on Google+Share on Google+

Display Image in Java

an example that display image in Java application.

Display Image in Java


This example takes an image from the system and displays it on a frame using ImageIO class. User enters the name of the image using the command prompt and then the program displays the same image on the frame. The image is read from the system by using file) method. 

The methods used in this example are:.
drawImage(Image img,int x,int y,ImageObserver observer): This method is used to draw an image. The image is drawn at the top-left corner at (x, y)  coordinates in this graphics context's coordinate space
setSize(int height , int width):
Use this method to set the size of the frame.
setVisible(boolean b):
Pass the boolean value as true to display the image or Vice-Versa. Here we are passing the boolean value as true to display the image on the frame.
This method gets the content pane in which our GUI component will be added. 

Code deacription:
In this example first we imports the required packages. Then create a class named ShowImage that extends the Panel class. Now declare a variable of BufferedImage class. Constructor ShowImage() recieves the name of the image passed at the command prompt in the BufferedReader object and reads the image from the system. Use paint() method to draw the image. Create a frame and an object of ShowImage in the main method. Add this object into content pane, set the size of the frame and define the visibility mode as true to display the image on the frame.

Here is the code of the program :

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class ShowImage extends Panel {
  BufferedImage  image;
  public ShowImage() {
  try {
  System.out.println("Enter image name\n");
  BufferedReader bf=new BufferedReader(new 
 String imageName=bf.readLine();
  File input = new File(imageName);
  image =;
  catch (IOException ie) {

  public void paint(Graphics g) {
  g.drawImageimage, 00null);

  static public void main(String args[]) throws
Exception {
  JFrame frame = new JFrame("Display image");
  Panel panel = new ShowImage();

 Input on dos Prompts :

C:\image>java ShowImage
Enter image name

Input image:rajeshxml2.gif

Output of The Example:

Download this example.



Posted on: March 27, 2005 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Display Image in Java   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
July 1, 2011
java programing

July 1, 2011
java programing

good and useful
how to attach a .swf or .fla file in core java?
July 4, 2011
image attach

plz any one tell me hurry..
September 17, 2011

hey this worked thanx a lottt yaaarr
November 19, 2011

December 14, 2011

its too long.....
April 23, 2012
where is the Paint method applied?

You had mplemented the paint method, but you did not use it.
July 16, 2012
image displaying

thanks this really helped
March 10, 2014
image details

please tell java code to retrieve image details like camera model,data taken