Display image in the applet

Ads
 

Display image in the applet

This program illustrates you to display image in an applet which has been done in this example.

This program illustrates you to display image in an applet which has been done in this example.

Display image in the applet

     

Introduction

This program illustrates you to display image in an applet which has been done in this example. In this program you will see that how many methods, classes, packages and it's properties have been used to display the image in an applet.

In this program only one function paint(Graphics g) has used. Function paint(Graphics g) is also a part of the life cycle of an applet in which anything you can draw in your applet to appear on the browser. In this function MediaTracker class of the 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 class which is used in this program as you can see. In this program method getImage() is used to return the image for the object ( img ) of the Image class taking two arguments, first is getCodeBase() and another is image name. 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 the code of the program : 

import java.applet.*; 
import java.awt.*; 
 
public class appletImage extends Applet{
  Image img;
  MediaTracker tr;
  public void paint(Graphics g) {
  tr = new MediaTracker(this);
  img = getImage(getCodeBase()"freelinuxcds.gif");
  tr.addImage(img,0);
  g.drawImage(img, 00this);
  
}

Try online this example.

Download this example.

Ads

Ads