0% found this document useful (0 votes)
34 views2 pages

Import Import Public Class Extends Public Static Void New: "Image Frame" "Icon - Confused - Gif"

This Java code displays an image by loading it from a file, drawing it to a graphics context, and displaying it in a JFrame window. It prompts the user to enter an image file name, reads the file into a BufferedImage object using ImageIO, and overrides the paint method to draw the image to the Panel. The main method creates the JFrame and adds the Panel to display the loaded image.

Uploaded by

Avishek Dutta
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views2 pages

Import Import Public Class Extends Public Static Void New: "Image Frame" "Icon - Confused - Gif"

This Java code displays an image by loading it from a file, drawing it to a graphics context, and displaying it in a JFrame window. It prompts the user to enter an image file name, reads the file into a BufferedImage object using ImageIO, and overrides the paint method to draw the image to the Panel. The main method creates the JFrame and adds the Panel to display the loaded image.

Uploaded by

Avishek Dutta
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import 

java.awt.*;
import java.awt.event.*;

public class AwtImage extends Frame{
  Image img;
  public static void main(String[] args){
    AwtImage ai = new AwtImage();
  }

  public AwtImage(){
    super("Image Frame");
    MediaTracker mt = new MediaTracker(this);
    img = Toolkit.getDefaultToolkit().getImage("icon_confused.gif");
    mt.addImage(img,0);
    setSize(400,400);
    setVisible(true);
    addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent we){
        dispose();
      }
    });
  }
  public void update(Graphics g){
    paint(g);
  }
  
  public void paint(Graphics g){
    if(img != null)
      g.drawImage(img, 100, 100, this);
    else
      g.clearRect(0, 0, getSize().width, getSize().height);
  }
}
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);
  }
}

You might also like