Display Image using Java2D

Display Image using Java2D


Posted in : Core Java Posted on : October 29, 2010 at 5:15 PM Comments : [ 0 ]

This section contains the detail about the Display Image using Java2D in java Swing.

Display Image using Java2D

One of the most important capabilities of Java2D API is to display image. An image is an array of pixels. Each pixel represents a color at a given position. We can use components like JLabel to display an image, or we can draw it using the Java 2D API.

Example :

Given below example will display image on panel :

package corejava;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Java2DImage extends JPanel {
	public void paintComponent(Graphics g) {
		super.paintComponent(g);

		Graphics2D g2d = (Graphics2D) g;
		Image image = new ImageIcon(
				

"E:/ankit_tutorial/java/beautiful-nature-photographs.jpeg")
				.getImage();
		g2d.drawImage(image, 0, 0, null);
	}

	public static void main(String[] args) {

		Java2DImage image = new Java2DImage();
		JFrame frame = new JFrame("Java2D Image");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(image);
		frame.setSize(380, 320);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}

}

Output :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics