Simple Image Viewer in Java

Submitted by: 
Visitors have accessed this post 12568 times.


In this tutorial we will learn how to display picture in Java Application. There is difference between how a picture displayed in application and applet.
For application we require drawImage() method which draws the image to specific component and not the entire frame.

Step 1: Creating the components

We require a label, text field where user will enter name or address of the image and a button which when clicked will display the image. A panel to keep all these components.

  2. JLabel label;
  3. JTextField name;
  4. JButton display;
  6. JPanel upperPanel;

Step 2:Defining Constructor

In the Constructor of the class, we will create the label , text field and button object and add those object to the Panel. The layout for the main Frame is Border Layout, we will add upperPanel in the NORTH of the frame and other panel for displaying the image in the center of the panel as follows.

  1. ImageDisplay()
  2. {
  3. super("Image Demo");
  4. label=new JLabel("Enter name of the Image you want to open");
  5. name=new JTextField(15);
  6. display=new JButton("Display Image");
  8. DrawPanel panel=new DrawPanel();
  9. display.addActionListener(panel);
  10. upperPanel=new JPanel();
  11. setLayout(new BorderLayout());
  12. upperPanel.add(label);
  13. upperPanel.add(name);
  14. upperPanel.add(display);
  17. add(upperPanel,BorderLayout.NORTH);
  18. add(panel,BorderLayout.CENTER);
  19. }

Step 3: Drawing the Image

For drawing the image, we will create a separate class inside the main class. This class will be a panel and action listener for the button will be in this class.
Data enter by user in the text field is stored in String object and Image is created using that object. Image is created by ImageIcon's getImage() method.

  1. public void actionPerformed(ActionEvent e)
  2. {
  3. String text=name.getText().trim();
  4. image=new ImageIcon(text).getImage();
  5. repaint();
  7. }

paintComponent() method is used to repaint the panel every time user clicks the display button. For clearing the background, Current color is set as white and then fillRect() method is used to clear the Area. drawImage() method draws the Image from pixel location specified. Image's top left corner will be placed on that pixel location. this keyword specifies that image should be drawn to the same panel.

  1. public void paintComponent(Graphics g)
  2. {
  3. setBackground(Color.WHITE);
  4. g.setColor(Color.WHITE);
  5. g.fillRect(10,10,800,600);
  6. g.drawImage(image,10,10,this);
  7. }

Step 4: Defining main() method

main() method is defined which set the different properties of the frame and execution begins from here.

  1. public static void main(String[] args){
  4. ImageDisplay img=new ImageDisplay();
  5. img.setSize(800,600);
  6. img.setVisible(true);
  7. img.setDefaultCloseOperation(EXIT_ON_CLOSE);
  8. img.setResizable(false);
  9. }

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Hi there
Thanks for your sharing.I am not the common user of image.I have only tried a free trial for imaging viewer.And now i want to get an image viewer for my mac.But i want to know that if it is possible for this image viewer to work offline?
Thanks for any suggestion.

hello what is your IDE?What best IDE for Java Language?

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.