Pageviews

Sunday 7 July 2013

Display images in Java Applets

by Unknown  |  in Java at  Sunday, July 07, 2013

Displaying background images is the another powerful functionality provided by Java Applets.You can choose any of your favorite wallpapers as your background screen in Applets.So,here are some easy steps to display background images in Applets.


  • Firstly create your Applet.
  • In the declaration section,you need to declare a variable for your image,like;


Image variable_name;

In the init(),you need to initialize your image and define its path,where it is located in you system,using the following syntax;

p=getImage(getDocumentBase(),"Image_path.jpg");

If your image is located within the folder,where you are writing the codes,then you need not to specify the path of the image just need to mention its name,but if your image located in the another folder/drive,then you need to specify the path within the double quotes(" ").

Now we need to invoke our image in the paint(),where your image will be displayed using below syntax;

g.drawImage(p,10,10,this);                                        //g-object of Graphics class.

Here is the complete code for displaying a image in your Applet.

import java.applet.*;
import java.awt.*;

public class applet4 extends Applet
{
Image p;
public void init()
{
p=getImage(getDocumentBase(),"4.png");
}
public void paint(Graphics g)
{
g.drawImage(p,10,10,this);
}
}

Applet


The output of the applet is :





Now you are all done with the coding part,now you can display your defined images as background image in your Applet.

0 comments:

Proudly Powered by Blogger.