Posted By:
20 Feb, 2014 12:49 pm PST

How to display an Image in the frame using Java ?

Write a program to display an image in the frame and also in the title bar of the frame.

import java.awt.*;

import java.awt.event.*;

class Images1 extends Frame{

       static Image img;




              MediaTracker track=new MediaTracker(this);





              catch (InterruptedException ie)


              addWindowListener(new WindowAdapter(){

                     public void windowClosing(WindowEvent we)






       public void paint(Graphics g)


              g.drawImage(img, 50, 50,200,200, null) ;


       public static void main(String args[])


              Images1 i=new Images1();











Java.awt : Contains all of the classes for creating user interfaces and for painting graphics and images.

Frame class : A Frame is a top-level window with a title and a border.

Syntax: public class Frame

extends Window

implements MenuContainer


WindowListener Interface: The listener interface for receiving window events. The class that is interested in processing a window event either implements this interface (and all the methods it contains) or extends the abstract WindowAdapter class (overriding only the methods of interest). The listener object created from that class is then registered with a Window using the window's addWindowListener method. When the window's status changes by virtue of being opened, closed, activated or deactivated, iconified or deiconified, the relevant method in the listener object is invoked, and the WindowEvent is passed to it.

Toolkit.getDefaultToolkit.getImage() : Returns an image which gets pixel data from the specified file, whose format can be either GIF, JPEG or PNG.
MediaTracker class : The MediaTracker class is a utility class to track the status of a number of media objects. Media objects could include audio clips as well as images, though currently only images are supported.



drawImage(Image img, int x, int y, int width, int height, ImageObserver




Draws as much of the specified image as has already been scaled to fit inside the specified rectangle.

WaitforID(int id):



Starts loading all images tracked by this media tracker with the specified identifier. This method waits until all the images with the specified identifier have finished loading.


Paint function() :  This method will be invoked when it is time to paint. This method will be executed by the painting subsystem whenever you component needs to be rendered. Its signature is:

Was it useful?  

Please login to reply to this problem.