Open In App

How to Create an Event Listener in Applet?

In this article, we will be creating the event Listener in the Applet window. Here, we will use the ActionListener on Button and MouseListener when the event has occurred through mouse activities.

Approach for Implementing Event Listener Applet

Steps to Implement EventListener Applet

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



Step 1: We need to check the Java version by running the below command in the terminal:



Step 2: First, create the file as ListenerApplet.java and enter the code into it.

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




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

Step 4: Then we need to compile the Java code using the below command. We can see that the new .class file is been generated.

javac ListenerApplet.java

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

appletviewer ListenerApplet.html

Program to Create Event Listener using Applet

Implementation of the ListenerApplet program is mentioned below:




// Java Program to create an event listener in Applet
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.Panel;
  
//Driver Class
public class ListenerApplet extends Applet implements ActionListener, MouseListener {
    
    private String message = "Click, hover";
    private Color shapeColor = Color.WHITE;
    
    public void init() {
        
        // Creating a panel to hold the btn
        Panel cp = new Panel();
        
        // Creating a btn with an ActionListener
        Button btn = new Button("Change Color");
        Font buttonFont = new Font("Arial", Font.BOLD, 18);
        
        btn.setFont(buttonFont);
        btn.setBackground(new Color(50, 100, 150));
        btn.setForeground(Color.WHITE);
        btn.addActionListener(this);
        
        // Adding the btn to the control panel
        cp.add(btn);
        
        // Registering MouseListener
        addMouseListener(this);
        
        // Adding the control panel to the applet
        add(cp);
    }
    
    public void actionPerformed(ActionEvent e) {
        
        // Changing the shape color on btn click
        shapeColor = shapeColorFn();
        repaint();
    }
    
    public void mouseClicked(MouseEvent e) {
        
        // Displaying a message when the applet is clicked
        message = "Mouse Clicked at (" + e.getX() + ", " + e.getY() + ")";
        repaint();
    }
    
    public void mouseEntered(MouseEvent e) {
        
        // Changing the message when the mouse enters the applet
        message = "Mouse Entered!";
        repaint();
    }
    
    public void mouseExited(MouseEvent e) {
        
        // Changing the message when the mouse exits the applet
        message = "Mouse Exited!";
        repaint();
    }
    
    public void mousePressed(MouseEvent e) {
        // Empty method required by MouseListener
    }
    
    public void mouseReleased(MouseEvent e) {
        // Empty method required by MouseListener
    }
    
    public void paint(Graphics g) {
        
        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;
        
        // Drawing a shape with the chosen color
        g.setColor(shapeColor);
        g.fillRoundRect(centerX - 100, centerY - 50, 200, 100, 20, 20);
        
        // Drawing the message text
        g.setFont(new Font("Arial", Font.PLAIN, 18));
        g.setColor(Color.BLACK);
        g.drawString(message, 10, 40);
    }
    
    // Method to generate a random color
    private Color shapeColorFn() {
        
        int red = (int) (Math.random() * 256);
        int green = (int) (Math.random() * 256);
        int blue = (int) (Math.random() * 256);
        
        return new Color(red, green, blue);
    }
    
}

Output for the program:

Explanation of the Program of Event Listener Applet


Article Tags :