This is a Java Program to Load and Display an Image Using Applet
We have to write a program in Java such that it loads and displays an image using applet.
For loading and displaying image, we can have the following set of input and output.
When the Applet is Executed :
This image is available at the present working directory as “logo.jpeg”
When the applet is executed, it is expected that the image is displayed in the frame.
1. Use method getImage to load the image.
2. Use method drawImage to draw the image.
Here is source code of the Java Program to load and display image using applet. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.
/*Java Applet to Load and Display Image*/
import java.awt.*;
import java.applet.*;
public class Load_Image extends Applet
{
Image image;
//Fucntion to Load the image
public void init()
{
image=getImage(getCodeBase(),"logo.jpeg");
}
//Function to draw the image
public void paint(Graphics g)
{
g.drawImage(image,0,0,this);
}
}
/*
<applet code = Load_Image.class width=500 height=500>
</applet>
*/
To execute the applet use the following commands:
>>>javac Load_Image.java >>>appletviewer Load_Image.java
1. The method getImage(getCodeBase(),image file name) is used to get the image.
2. The method drawImage(image file,x,y,component) is used to draw the image starting from the x and y co-ordinates in the component specified.
Here’s the run time test cases to Load and Display Image using Applet.
Test case 1 – To Load and Display Image.
Sanfoundry Global Education & Learning Series – Java Programs.
- Check Java Books
- Practice Information Technology MCQs
- Apply for Computer Science Internship
- Check Programming Books
- Practice BCA MCQs