AJP11_15

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

DEPARTMENT OF COMPUTER

ENGINEERING

Subject: Advanced Java Programming Subject Code: 22517

Semester: 05 Course: CO5I-B

Laboratory No: L002 Name of Subject Teacher: Prof. Pradeep Shirke

Name of Student: Ashwath Bhekare Roll ID: 22203B0015

Experiment No: 11

Title of Experiment Write a program to demonstrate various mouse events using


MouseListener and MouseMotion listener interface.

 Program code
1. Write a program code to generate the following output

Ans:
import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

/* <APPLET CODE ="MouseDemo" WIDTH=300 HEIGHT=200> </APPLET> */

public class MouseDemo extends Applet implements MouseListener {

Label l;

public void init() {

setLayout(null);

l = new Label("Hello Mouse");

l.setBounds(50, 150, 200, 30); // Adjusted height for better visibility


add(l);

addMouseListener(this);

public void mousePressed(MouseEvent e) {

l.setText("Mouse Pressed; # of clicks: " + e.getClickCount() + " at position: " +


e.getX() + ", " + e.getY());

public void mouseReleased(MouseEvent e) {

l.setText("Mouse Released; # of clicks: " + e.getClickCount());

public void mouseEntered(MouseEvent e) {

l.setText("Mouse Entered");

public void mouseExited(MouseEvent e) {

l.setText("Mouse Exited");

public void mouseClicked(MouseEvent e) {

l.setText("Mouse Clicked; # of clicks: " + e.getClickCount());

Output:
 Practical Related Question
1. List various methods of MouseListener and MouseMotionListener.
Answer:

a) mousePressed
b) mouseReleased
c) mouseEntered
d) mouseExited
e) mouseClicked

2. Do all components generate the MouseEvent.


Answer:

 Any component can generate mouse events.


 An object that wants to respond to these events must implement one or both
listener interfaces.
 It must also register itself with the component by calling the component's
addMouseListener() and/or addMouseMotionListener() methods.

3. Write the steps to obtain the coordinates of MouseClick.


Answer:

 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.

4. Write the steps to register for MosueEvents.


Answer:

 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.*;

/* <APPLET CODE="ColorChangeApplet" WIDTH=300 HEIGHT=200> </APPLET> */

public class ColorChangeApplet extends Applet implements MouseListener {


public void init() {

addMouseListener(this);

public void mouseClicked(MouseEvent e) {

setBackground(Color.GREEN);

public void mousePressed(MouseEvent e) {

setBackground(Color.RED);

public void mouseReleased(MouseEvent e) {

setBackground(Color.BLUE);

public void mouseEntered(MouseEvent e) {

setBackground(Color.YELLOW);

public void mouseExited(MouseEvent e) {

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.*;

/* <APPLET CODE="ColorChangeApplet" WIDTH=300 HEIGHT=200> </APPLET> */

public class ClickCounter extends JFrame implements MouseListener {

private int clickCount = 0;

private JLabel label;

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

public void mouseClicked(MouseEvent e) {

clickCount++; // Increment the click count

label.setText("Click Count: " + clickCount); // Update the label

@Override

public void mousePressed(MouseEvent e) {}

@Override

public void mouseReleased(MouseEvent e) {}

@Override

public void mouseEntered(MouseEvent e) {}

@Override

public void mouseExited(MouseEvent e) {}

public static void main(String[] args) {

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 class MouseMotionDemo extends JFrame implements MouseMotionListener {

private JLabel label;

public MouseMotionDemo() {

setTitle("Mouse Motion Demo");

setSize(400, 300);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocationRelativeTo(null);

label = new JLabel("Move or Drag the Mouse", JLabel.CENTER);

label.setFont(new Font("Arial", Font.PLAIN, 18));

add(label, BorderLayout.NORTH);

JPanel panel = new JPanel();

panel.setBackground(Color.LIGHT_GRAY);

panel.addMouseMotionListener(this);

add(panel, BorderLayout.CENTER);

setVisible(true);

@Override

public void mouseMoved(MouseEvent e) {

label.setText("Mouse Moved at: (" + e.getX() + ", " + e.getY() + ")");

}
@Override

public void mouseDragged(MouseEvent e) {

label.setText("Mouse Dragged at: (" + e.getX() + ", " + e.getY() + ")");

public static void main(String[] args) {

new MouseMotionDemo();

Output:

Marks Obtained Dated signature of Teacher

Process Product Total (50)


Related (35) Related
(15)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy