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;

       Images1()

       {

              img=Toolkit.getDefaultToolkit().getImage("Penguins.JPG");

              MediaTracker track=new MediaTracker(this);

              track.addImage(img,0);

              try{

                     track.waitForID(0);

                                  }

              catch (InterruptedException ie)

              {}

              addWindowListener(new WindowAdapter(){

                     public void windowClosing(WindowEvent we)

               {

                           System.exit(0);

                           }

              });

       }

       public void paint(Graphics g)

       {

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

                     }

       public static void main(String args[])

       {

              Images1 i=new Images1();

              i.setSize(500,400);

              i.setTitle("MyImages");

              i.setIconImage(img);

              i.setVisible(true);

       }

}

OUTPUT:

 

Attachment: workspace.zip


Description:

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

 observer)

 

 

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:

Attachment: Images1.java
Was it useful?  




Please login to reply to this problem.