Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Showing Images in a Swing JFrame in Java

In this article we will discuss how we can display an image on a swing JFrame in Java

This program discuss how can we display an image on a swing JFrame in Java. Suppose we want to display an image over the JFrame then there is no easy method in swing to do so. This program will tell you how to add the images on the frame easily.

Program Explanation:

First we will include the import classes which are needed for the program to work

Listing1: Including the import classes
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

In this case Dimension is used to store the size of the image which we are going to display on the JFrame. Graphics is used to draw the Image over the panel. Image object is used to store the image and ImageIcon creates an ImageIcon from an array of bytes which were read from an image file.

JFrame is used to display the frame, JPanel is used to insert a panel inside a frame. The Jpanel may consist of several components.

Now we will define the class along with the main method:

Listing2: Class along with main method

public class ImageInJframe extends JFrame
{
public static void main(String args[])
{
	new ImageInJframe().start();
}

The name of the class is ImageInJframe which extends JFrame, we defined a main method which will call the start method which contains the logic part.

Now we will define the start method:

Listing3: start method

public void start()
{
	  ImageImplement panel = new ImageImplement(new ImageIcon("1.png").getImage());
	  add(panel);
	  setVisible(true);
	  setSize(400,400);
	  setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

We define an Object of ImagePanel class which we are going to define. We obtain the image which we want to display via new ImageIcon("1.png").getImage() and then we pass that as an argument to ImageImplement class.

We add this to our JFrame and set the frame as visible and size as 400,400 which define the width and height of the frame.

We set the default close operation as EXITONCLOSE so that the application closes when user clicks the cross button on the title bar.

Now we define the ImageImplement class:

Listing5: ImageImplement class

class ImageImplement extends JPanel {

	  private Image img;


	  public ImageImplement(Image img) {
	    this.img = img;
	    Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
	    setPreferredSize(size);
	    setMinimumSize(size);
	    setMaximumSize(size);
	    setSize(size);
	    setLayout(null);
	  }

	  public void paintComponent(Graphics g) {
	    g.drawImage(img, 0, 0, null);
	  }

	}

We made a class named ImageImplement. It extends JPanel. We declared a variable of Image class which will be responsible to store the image to be shown on the frame and now we will define the constructor. Here we passed the Image object as argument which defines the image to be displayed. We set the img variable to the value of the passed argument img.

Now we obtain the size of the image and store that info in a Dimension object. We set the size of the JPanel. We define the paintComponent(Graphics g) method which is going to draw the image on the panel and defined the drwImage method which actually draws the image on the panel which is placed on the frame.

Output:

output

Full Source code:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

class ImageImplement extends JPanel {

	  private Image img;


	  public ImageImplement(Image img) {
	    this.img = img;
	    Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
	    setPreferredSize(size);
	    setMinimumSize(size);
	    setMaximumSize(size);
	    setSize(size);
	    setLayout(null);
	  }

	  public void paintComponent(Graphics g) {
	    g.drawImage(img, 0, 0, null);
	  }

	}

public class ImageInJframe extends JFrame
{
public static void main(String args[])
{
	new ImageInJframe().start();
}

public void start()
{
	ImageImplement panel = new ImageImplement(new ImageIcon("1.png").getImage());
	  add(panel);
	  setVisible(true);
	  setSize(400,400);
	  setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

Hope you liked the article. See you in the next article



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login