JAVA_PRACTICAL11

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

Name:- Priyanka Khandu Thadke

Class:-A(CO5I) Roll no:- 60


Practical no:-11 write a program to demostrate various mouse events using
MouseMotionListener and MouseMotion listener interface.

Exercise:
Q1.Write a java program to change the background color of frame when user performs
events using Mouse.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class MouseEventA extends JFrame implements MouseListener { public MouseEventA () {

setTitle("Mouse Event Background Color");

setSize(400, 400);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

addMouseListener(this);

setVisible(true); } @Override

public void mouseClicked(MouseEvent e) {

getContentPane().setBackground(Color.YELLOW); }

@Override public void mousePressed(MouseEvent e) {

getContentPane().setBackground(Color.GREEN); }

@Override

public void mouseReleased(MouseEvent e) {

getContentPane().setBackground(Color.BLUE);

System.out.println("Blue"); }

@Override

public void mouseEntered(MouseEvent e) {


getContentPane().setBackground(Color.CYAN); }

@Override

public void mouseExited(MouseEvent e) {

getContentPane().setBackground(Color.LIGHT_GRAY); }

public static void main(String[] args) {

SwingUtilities.invokeLater(MouseEventA::new); }}

Output:

After Clicking Mouse

Q2. write java program to count the number of clickd performed by the user in a frame
window

import javax.swing.*;

import java.awt.event.*;

public class ClickCounter extends JFrame implements MouseListener {private int clickCount = 0;

private JLabel label;

public ClickCounter() {
setTitle("Click Counter");

setSize(400, 200);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

label = new JLabel("Number of clicks: 0", SwingConstants.CENTER);

label.setFont(label.getFont().deriveFont(18.0f));

add(label);

addMouseListener(this);

setVisible(true); }

@Override

public void mouseClicked(MouseEvent e) {

clickCount++;

label.setText("Number of clicks: " + clickCount);

@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) {

SwingUtilities.invokeLater(ClickCounter::new);

}}
Output:

Q3.Write a Program to demosrate the use of Mousedraged and mousemoved method of


MousemotionListner.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class MouseMotionDemo1 extends JFrame implements MouseMotionListener {

private int mouseX = 0;

private int mouseY = 0;

private String eventType = "";

public MouseMotionDemo() { setTitle("Mouse Motion Demo");

setSize(400, 300);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

addMouseMotionListener(this);

}@Override
public void mouseMoved(MouseEvent e) {

mouseX = e.getX();

mouseY = e.getY();

eventType = "Mouse Moved By Priyanka";

repaint();

} @Override

public void mouseDragged(MouseEvent e) {

mouseX = e.getX();

mouseY = e.getY();

eventType = "Mouse Dragged";

repaint(); } @Override

public void paint(Graphics g) {

super.paint(g);

g.setColor(Color.BLACK);

g.drawString(eventType + " at (" + mouseX + ", " + mouseY + ")", 50, 50);

} public static void main(String[] args) {

new MouseMotionDemo(); }}

Output:

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