Open In App

How to Display Image using Applet?

Last Updated : 23 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will be using the Applet to display the image in proper layout. Here, we will display the image along with the text in the Applet Viewer.

Approach for Displaying Images Using Applet

  • We need to import the essential packages like Applet and AWT for customization.
  • When the packages get imported, then we need to create the class that implemented the ActionListener interface in Java.
  • After that, we have to initialize the UI component of the Image.
  • After initializing the component, we used the init() method in Java to load the image from the local storage. We will be displaying the GFGLOGO in the applet.
  • Then, we have to define the paint() method, which will help render the content inside the Applet window.
  • We will make the HTML file to run the applet.
  • We have to compile the Java code using Javac and then we need to run using appletviewer.

Creating and Running the Applet

The folder view of the project is shown in the below image.

PS

Step 1: First, create the file as AppletImage.java and enter the code into it.

Step 2: Once the code is been inserted then we need to create the AppletImage.html file, through which we will display the output.

HTML




<!DOCTYPE html>
<html>
<head>
    <title>Image Applet</title>
</head>
<body>
    <applet code="AppletImage.class" width="300" height="300">
    </applet>
</body>
</html>


Step 3: We need to have the image which we need to display in the applet. Here, we have used the gfglogo.png which is present in the code directory.

Step 4: Then we need to compile the Java code using the below command:

javac AppletImage.java

Step 5: Now finally we need to run the application using the below command:

appletviewer AppletImage.html

Program to Display Image using Applet

Implementation of the AppletImage program is mentioned below:

Java




// Java Program to display image using Applet
  
//Importing Necessary Packages
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
  
// Driver Class
public class AppletImage extends Applet {
    private Image img;
    public void init() {
        // Loading the image from a local file (gfglogo.png)
        img = Toolkit.getDefaultToolkit().getImage("gfglogo.png");
    }
    public void paint(Graphics g) {
        if (img != null) {
            // Clearing the applet background initially
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, getWidth(), getHeight());
            // Drawing a border around the image
            g.setColor(Color.BLACK);
            g.drawRect(10, 10, getWidth() - 20, getHeight() - 20);
            // Drawing the image, scaled to fit within the border
            int widImg = img.getWidth(this);
            int heiImg = img.getHeight(this);
            if (widImg > 0 && heiImg > 0) {
                int x = 20;
                int y = 20;
                int maxWid = getWidth() - 40;
                int maxHei = getHeight() - 40;
                // Calculating the new dimensions to fit within the border
                int nWid, nHei;
                if (widImg <= maxWid && heiImg <= maxHei) {
                    nWid = widImg;
                    nHei = heiImg;
                } else {
                    double wRatio = (double) maxWid / widImg;
                    double hRatio = (double) maxHei / heiImg;
                    double scale = Math.min(wRatio, hRatio);
                    nWid = (int) (widImg * scale);
                    nHei = (int) (heiImg * scale);
                }
                g.drawImage(img, x, y, nWid, nHei, this);
            }
            // Adding a title text
            g.setColor(Color.BLACK);
            g.setFont(new Font("SansSerif", Font.BOLD, 16));
            g.drawString("GeeksforGeeks Logo", 20, getHeight() - 10);
        }
    }
}


Output for the program

Output

Explanation of the Program

  • We have imported the packages which are necessary to define the Applet and the Label to display the image name in the application.
  • Then we have defined the AppletImage class which is implementing the ActionListener interface.
  • There is an init() method, which is responsible for loading the gfglogo.png image from the local disk of the system.
  • We have defined the paint() method, which performs the entire functionality of displaying the image to the Applet. We have set the custom color to the Applet window, also, we have defined the dimensions in which the gfglogo will be displayed on the Applet window.
  • We have customized the appearance of the image by adding a border to it. Also, we have added the image title text using the drawString() method.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads