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
- 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 and MouseListener interface in Java.
- When the class gets created, then we need to initiate and create the GUI components which will be displayed on the Applet Window. These components are Buttons, Messages, etc.
- After the initialization of the components is done, we need to define the init() method, which will be used to make the components applied on the Applet window by customizing their colors, font, size, etc.
- Then mainly, we will apply the event listeners like actionPerformed() on the button. If the button is been clicked, the shape is been filled with a random color.
- Additionally, there is MouseListener, which has various methods to perform events like mouseClick, mouseEntered, mouseExited, etc.
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
- We have imported the packages that are necessary to define the Applet and the AWT components like Buttons, Font, Color, etc.
- Then we have defined the main ListenerApplet class which is been implemented in the ActionListener and MouseListener interface.
- There is an init() method that is used for the initialization of the components like font, Buttons, and Color and adds it into the Applet window by applying the customization properties of Color, font, Size, etc.
- Then we have the actionPerformed() method which is of the ActionListener interface. This method is used to perform the actions as per the click on the Button.
- As we have also defined the MouseListener, we have used different methods of this listener like mouseclick(), mouseEntered(), mousePressed(), etc. All these methods are used to handle the events that occur by the mouse clicks or the movements of the Mouse.
- There is a user-defined method called shapeColorFn() that is used to generate the random color and apply it on each button click to the Shape.