Home Java Example Java Awt Image on Frame in Java AWT
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Image on Frame in Java AWT

Advertisement
In this section, you will learn how to display image on the frame. This program shows you how to display image in your application.

Image on Frame in Java AWT

     

Introduction

In this section, you will learn how to display image on the frame. This program shows you how to display image in your application.

In this program, there are three methods have been used to display the image on the frame in your application. These are explained below : 

main() : 
This is the main() method of the program from which your program starts to execute the program in sequence. This method has simply create the instance for the AwtImage class.

AwtImage() : 
This is the constructor of the AwtImage class in which, the instance of the MediaTracker class has been created to add the image on the frame using the addImage() method of the MediaTracker class. This constructor set the size, visibility and the close operation on the close button of the frame.

MediaTracker : 
MediaTracker
is the class of java.awt.*; package, has been used. MediaTracker is a utility class that tracks the status of a number of media objects. And this type of object can include images and audio clips. In this program only the explanation about the adding images has been given. MediaTracker class is used after creating the instance for that and calling the addImage() of the MediaTracker.

addImage() : 
This is the addImage() method of the MediaTracker class which is used load the image. Then the addImage() method of the MediaTracker has been used. Syntax of the addImage() function is MediaTracker.addImage(img, x, y, x1, y1). Arguments of addImage() function is explained below : 

img - image name type of Image.
x   - lower X - Coordinate type of int.
y   - lower Y - Coordinate type of int.
x1   - upper X - Coordinate type of int.
y1   - upper Y - Coordinate type of int.

Here is code of the program : 

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, 100100this);
  else
  g.clearRect(00, getSize().width, getSize().height);
  }
}

Download this example.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 17, 2007

Related Tutorials

Ask Questions?    Discuss: Image on Frame in Java AWT   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Michael Plautz
June 18, 2011
Helpful

This was the most helpful page I went to to learn how to do this. Thank you to whomever posted this.
jaspuneet
August 12, 2011
rating

very gud coding....thanx
arun kamboj
January 25, 2012
set the image

i am designing a login form with the help of awt and jdbc and i wanna to add a banner and the image on my login form can u please solve my problem............
rahul
June 27, 2012
for AWT image not displying on my pc

it's not displaying on frame? where we have to keep the image?
Pankaj kumar
October 2, 2013
java

This most helpful page
DMCA.com