Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions? | Software Development
 

Features

Technology
  Tech. Home
  GPS
  OSI
  WiMAX
  VoIP
  WiFi
  HSDPA
  LBS
  Vehicle Tracking
  SCADA
  Tech. What is?
Jobs At RoseIndia.net!
 
Join For Newsletter

Powered by groups.yahoo.com
Visit Group! Post Questions!

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.
getContentPane():
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 
InputStreamReader
(System.in));
   String imageName=bf.readLine();
  File input = new File(imageName);
      image = ImageIO.read(input);
    catch (IOException ie) {
      System.out.println("Error:"+ie.getMessage());
    }
  }

  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();
    frame.getContentPane().add(panel);
    frame.setSize(500500);
    frame.setVisible(true);
  }
}

 Input on dos Prompts :

C:\image>javac ShowImage.java
C:\image>java ShowImage
Enter image name
rajeshxml2.gif

Input image:rajeshxml2.gif

Output of The Example:

Download this example.

                         

» View all related tutorials
Related Tags: c com class orm ant form model io components sed color format component value byte int rgb show pixel for

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

4 comments so far (
post your own) View All Comments Latest 10 Comments:

I have learn new thing in java using panel with image.

Posted by Randhir on Monday, 12.28.09 @ 13:28pm | #93645

How to add data in MS access table using JTable including JComboBox?

Posted by Subo on Tuesday, 09.16.08 @ 11:01am | #79796

Unbelievable

Posted by Ashish on Friday, 05.9.08 @ 15:39pm | #59010

This is a good program for learner of java i m helpfull to give u thank`s for that

Posted by ashish kumar ban on Thursday, 02.28.08 @ 23:45pm | #50617

 
Tell A Friend
Your Friend Name

 

 
Recently Viewed
Software Solutions
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.