Displaying Image In Java Applet With Programming Explanation

Description:

Displaying image in java applet with programming explanation:- In this article, we will be discussing a new class image,  image class so in java applet image class is used to load and displaying image in java applet, and this image class is a part of or that is contained in java  .AWT package. AWT stands for( abstract window toolkit) and so image class is an entry in an AWT packaged class that provides support for imaging.


 what is imaging in the java applet:

Imaging is the display and manipulation of graphical images. so in our image class, an image is a rectangular graphical object that means image is an object of the image class that appears to be a say rectangle graphical object the display of any image object is rectangular.

Common operation on displaying image in java applet:

There are  three common operations that occur when we work with images  which are

creating an image

loading an image

and

displaying  image in java applet

creating an image means we are creating an object of image class loading an image means we are using some methods to load a particular image along with the object and that object this display image in java applet, that is the third step by using the created object we will be displaying the image actual image, so three steps are there first on creating an image that means image class object is created loading an image means we’ll be giving initialization to the newly created object so we are specifying where to take the image of the URL or pathname associated with the image and the actual image name that is loading an image then the third step is displaying image in java applet window.



so here for loading and displaying the  image we need help from two other methods and was first owned either from code base

or by using the  get document base

we’ll be getting help from any one of these two methods so get code base and get document base these are the two API or methods used to collect the URL or pathname of the image object so code base or getCodeBase method retains the directory from this applet class file was loaded as a URL object.

so used to retain the base URL get codebase method is used on the other hand get document based retains the URL of the document in which applet is embedded, so that means that retains the directory holding HTML file that stated the applet otherwise get document base retains the document in which applet is embedded, so that means that retains the URL, URL of the document in which applet is embedded so that is the HTML file you are so forgetting HTML file URL where our actual image file is there that particular HTML file URL is retained by get document base.


Creating Image object in java applet:

so creating an image object, we will be first using the class image for creating an image object, then loading the image object by using it image method then displaying an image using the draw image method.

so here are two different methods one is

and

so getImage uses two arguments first one is URL and the second one is the image name. so there are two different forms for getImage the first one is only with the URL the second one is having two parameters first one is the URL and the second one is the name of the image, so normally we will be using the second form that is getImage with two arguments 1st argument the document codebase or documentbase otherwise the second one image name.

displaying  image in java applet we need the  API or method that is drawImage so draw images are having different parameters so first one is the image object as you can see in the above example.

so the first step we are creating an image object that image object name is specified here

and the left, top, width, height and the pointer to be assigned to the image all these things are specified as different parameter along with drawImage.


Example: displaying image in java applet:

so this is a simple example for displaying an image

Java Code:

HTML Code:


displaying image in Java applet Code explanation:

First of all, we import the awt package and java applet package classes.

then we have specified the applet class name

then we have specified an image type of object name or instance for an image is specified

Then we create the init method, so inside init method we are loading that image

So first image object is created then loading of the image so getImage is the method used here to load the image so getCodebase otherwise you can use getdocumentbase here then img.jpg that is a name of the image to be displayed.

then we create a paint method

actually the paint method with the support of graphics class drawing the image, so in actually we had seen drawText or drawstring for displaying strings on to the applet window here for displaying image in java applet, drawImages method used so draw image first argument or parameter is the image object name that is IMG then where to display the coordinate X and y coordinate value top and left value specified as any coordinator that is X & Y. X Y coordinates on 150, 50 then width and height of the applet window is specified, then specified that this point so on execution the applet window will display the img.jpg image just like this

displaying image in java applet



displaying image in Java applet Html code explanation:

First of all I create the basic structure of html

Then inside body tag we write the  applet code

<Applet code=”Testing.class” width=500 height =500> that is the applet name also Testing this is the applet name so that after compilation will be getting a class file that file name is specified as the code parameter value for the applet.

Then we specify the width and height of the display area.


The final output of displaying image in  java applet image:

displaying image in java applet

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

1 Comment

Leave a Reply

%d bloggers like this: