Home Java Example Java Applet Display image in the applet
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Display image in the applet

Advertisement
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.

Advertisement

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 14, 2007

Ask Questions?    Discuss: Display image in the applet   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
munde manojkumar
October 2, 2011
display image or play songs on applet in java

please give me program
shweta gupta
November 17, 2011
java programming

i want do a programming of java applet, i search it, nd finally i done it thanku..........
Munish
January 27, 2012
Fetch image

how i can show image on window browser from any device . how to give path for that image
sahilpatel
September 14, 2012
Add Image in Applet

How can i add image from my pc/laptop? where i am write a address of image..?
saif
September 21, 2012
java

nice example its help me alottt
stalin
November 1, 2012
Save button

HI, in my project I am using applet and want to save image(.jpg)file format.but file will saved applet.ser format. Can any Body help me how can I do it Thanks & Regards Stalin
sivaprakash
October 24, 2013
java

i want display image into java eclipse ide plz help me
DMCA.com