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
1 |
<span style="font-size: 16px; font-family: 'times new roman', times, serif;">getCodeBase()</span> |
or by using the get document base
1 |
<span style="font-size: 16px; font-family: 'times new roman', times, serif;">getDocumentBase()</span> |
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
1 2 3 |
<span style="font-size: 16px; font-family: 'times new roman', times, serif;">Image getImage(URL url) Image getImage(URL url, String imageName)</span> |
and
1 |
<span style="font-size: 20px; font-family: 'times new roman', times, serif;">Image drawImage(image imageobj,int left,int top,int width, int height, imageObserver imgob)</span> |
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
1 |
<span style="font-size: 16px; font-family: 'times new roman', times, serif;">Image imageObj</span> |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<span style="font-size: 16px; font-family: 'times new roman', times, serif;">java code: import java.awt. *; import java.applet.*; public class Testing extends Applet{ Image img; public void init() { img=getImage(getCodeBase(),"img.jpg"); } public void paint(Graphics g) { g.drawImage(img,150,50,200,200,this); } } </span> |
HTML Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<span style="font-size: 16px; font-family: 'times new roman', times, serif;"><html> <head> <title> image Example </title> </head> <body> <Applet code="Testing.class" width=500 height =500> </Applet> </body> </html> </span> |
displaying image in Java applet Code explanation:
First of all, we import the awt package and java applet package classes.
1 2 3 |
<span style="font-size: 16px; font-family: 'times new roman', times, serif;">import java.awt. *; import java.applet.*;</span> |
then we have specified the applet class name
1 |
<span style="font-size: 16px; font-family: 'times new roman', times, serif;">public class Testing extends Applet</span> |
then we have specified an image type of object name or instance for an image is specified
1 |
<span style="font-size: 16px; font-family: 'times new roman', times, serif;">Image img;</span> |
Then we create the init method, so inside init method we are loading that image
1 |
<span style="font-size: 16px; font-family: 'times new roman', times, serif;">img=getImage(getCodeBase(),"img.jpg");</span> |
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
1 2 3 4 5 6 7 |
<span style="font-size: 16px; font-family: 'times new roman', times, serif;">public void paint(Graphics g) { g.drawImage(img,150,50,200,200,this); }</span> |
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 Html code explanation:
First of all I create the basic structure of html
1 2 3 4 5 6 7 8 9 |
<span style="font-family: 'times new roman', times, serif; font-size: 16px;"><html> <head> <title> Image Example </title> </head> <body> </body> </html></span> |
Then inside body tag we write the applet code
1 2 3 4 |
<span style="font-size: 16px; font-family: 'times new roman', times, serif;"><Applet code="Testing.class" width=500 height =500> </Applet> </span> |
<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.
1 |
<span style="font-size: 16px; font-family: 'times new roman', times, serif;">width=500 height =500</span> |
Then we specify the width and height of the display area.
Thanks