Display image in the applet

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

Display image in the applet

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

 Display image in the applet

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.

Tutorials

  1. What is an Applet
  2. The Life cycle of An Applet
  3. Java Applet - Creating First Applet Example
  4. Java - Drawing Shapes Example in java
  5. Java - Drawing Shapes Example using color in java
  6. Java - Event Listeners Example in Java Applet
  7. Applet - Passing Parameter in Java Applet
  8. Opening a URL from an Applet
  9. Java - Opening a url in new window from an applet
  10. Applet is not Working
  11. Display image in the applet
  12. Applet Write Files Example
  13. Play Audio in Java Applet
  14. Security Issues with the Applet
  15. Swing Applet Example in java
  16. The Sample Banner Example in Java
  17. Clock Applet in Java
  18. HTML Document Creation
  19. Tag Parameters: The Tag
  20. The APPLET Tag in Detail
  21. Java and HTML: The Basics
  22. What Exactly is HTML?
  23. Welcome to the Internet
  24. java.applet package examples
  25. java.applet package examples
  26. What is an Applet - Java Applet Tutorial
  27. Java - Read file Applet
  28. Applet versus Application
  29. Tag Parameters: The Tag
  30. Java - Opening a url in new window from an applet
  31. Java - Opening a URL from an Applet
  32. Applet Tag Parameters,Applet Tag in HTML
  33. Applets in Java
  34. What is Applet in Java?
  35. What is Applet in Java with Example?