Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

As for showing image in Java, I'd share some info here. This following example takes an image from the system and show it on a frame using ImageIO class. User enters the name of the image using the command prompt and then the program shows the same image on the frame. The image is read from the system by using ImageIO.read(File file) method.

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.drawImage( image, 0, 0, null);
  }

  static public void main(String args[]) throws
Exception {
  JFrame frame = new JFrame("Display image");
  Panel panel = new ShowImage();
  frame.getContentPane().add(panel);
  frame.setSize(500, 500);
  frame.setVisible(true);
  }
}

Tag: image

As for showing image in Java, Java, I'd share some info here. This following example takes an image from the system and show it on a frame using ImageIO class. User enters the name of the image using the command prompt and then the program shows the same image on the frame. The image is read from the system by using ImageIO.read(File file) method.

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.drawImage( image, 0, 0, null);
  }

  static public void main(String args[]) throws
Exception {
  JFrame frame = new JFrame("Display image");
  Panel panel = new ShowImage();
  frame.getContentPane().add(panel);
  frame.setSize(500, 500);
  frame.setVisible(true);
  }
}

Tag: Tags: image, image in Java