Display Image in Java

an example that display image in Java application.

Display Image in Java

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 ImageIO.read(File 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 java.io.*;
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 = ImageIO.read(input);
  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>javac ShowImage.java
C:\image>java ShowImage
Enter image name

Input image:rajeshxml2.gif

Output of The Example:

Download this example.


  1. Display Image in Java
  2. Show Coordinates
  3. What is Java Swing?
  4. Creating a Frame
  5. Setting an Icon for a Frame in Java
  6. Show Dialog Box in Java
  7. Show message and confirm dialog box
  8. Show input dialog box
  9. Changing the Label of a JButton Component in Java
  10. Setting Multi-Line label on the Button
  11. Setting icon on the button in Java
  12. Making a Frame Non Resizable in Java
  13. Remove Minimize and Maximize Button of Frame in Java
  14. Removing the Title Bar of a Frame
  15. Setting Bounds for a maximized frame
  16. Iconifying and Maximizing a frame in Java
  17. Making a component drag gable in java
  18. Create a ToolBar in Java
  19. Animating Images in Java Application
  20. Drawing with Color in Java
  21. Drawing with Gradient Color in Java
  22. Adding a Rollover and Pressed Icon to a JButton Component in Java
  23. Creating Check Box in Java Swing
  24. Customize the Icon in a JCheckBox Component of Java Swing
  25. Create a JComboBox Component in Java
  26. Combo Box operation in Java Swing
  27. Create a JRadioButton Component in Java
  28. Selecting a Radio Button component in Java
  29. Create a JList Component in Java
  30. Setting Tool Tip Text for items in a JList Component
  31. Setting the Dimensions of an Item in a JList Component in Java
  32. Create a JSpinner Component in Java
  33. Show Time in JSpinner Component
  34. Disabling Keyboard Editing in a JSpinner Component
  35. Limiting the Values in a Number JSpinner Component
  36. JSlider Component of Java Swing
  37. Progress Bar in Java Swing
  38. Create menus and submenus in Java
  39. Crate a Popup Menu in Java
  40. Create a Popup Menus with Nested Menus in Java