AJP11_15
AJP11_15
AJP11_15
ENGINEERING
Experiment No: 11
Program code
1. Write a program code to generate the following output
Ans:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
Label l;
setLayout(null);
addMouseListener(this);
l.setText("Mouse Entered");
l.setText("Mouse Exited");
Output:
Practical Related Question
1. List various methods of MouseListener and MouseMotionListener.
Answer:
a) mousePressed
b) mouseReleased
c) mouseEntered
d) mouseExited
e) mouseClicked
Create a class that extends Applet: This class will contain methods to handle
mouse events.
Implement MouseListener: Your class should implement the MouseListener
interface.
Register the Mouse Listener: In the init method of your applet, register the
mouse listener.
Override the mouseClicked method: This method provides access to the
MouseEvent, which contains the coordinates.
Use getX() and getY() methods: In the mouseClicked method, use these methods to
retrieve the X and Y coordinates of the mouse click.
Import Required Packages: Ensure you import the necessary packages for
handling mouse events.
Create a Class: Define a class that will handle mouse events. This class can
extend either Applet, JFrame, or any other Swing component.
Implement the MouseListener Interface: Your class should implement the
MouseListener interface (or MouseAdapter if you want to avoid
implementing all methods).
Create a Component: Inside your class, create a graphical component (like
JPanel or a label) where you want to detect mouse events.
Register the Mouse Listener: Use the addMouseListener(this); method on
the component to register the mouse listener.
Override the Mouse Event Methods: Implement the necessary methods of
the MouseListener interface to handle events like mouse clicks, presses,
releases, entering, and exiting.
Handle Mouse Events: Inside the overridden methods, define how you want
to respond to the mouse events, such as updating the UI or performing
actions based on the events.
Exercise
1. Write a program to change the background color of Applet when
user performs events using Mouse.
Answer:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
addMouseListener(this);
setBackground(Color.GREEN);
setBackground(Color.RED);
setBackground(Color.BLUE);
setBackground(Color.YELLOW);
setBackground(Color.LIGHT_GRAY);
Output:
2. Write a program to count the number of clicks performed by the
user in a Frame window.
Ans:
import javax.swing.*;
import java.awt.event.*;
public ClickCounter() {
setTitle("Click Counter");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
label = new JLabel("Click Count: " + clickCount, JLabel.CENTER);
add(label);
addMouseListener(this);
setVisible(true);
@Override
@Override
@Override
@Override
@Override
new ClickCounter();
Output:
3. Write a program to demonstrate the use of mouseDragged and
mouseMoved method of MouseMotionListener.
Answer:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public MouseMotionDemo() {
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
add(label, BorderLayout.NORTH);
panel.setBackground(Color.LIGHT_GRAY);
panel.addMouseMotionListener(this);
add(panel, BorderLayout.CENTER);
setVisible(true);
@Override
}
@Override
new MouseMotionDemo();
Output: