0% found this document useful (0 votes)
40 views5 pages

Ajp11. Minu

The document contains code for two Java programs. The first program uses a MouseListener interface to display text indicating the mouse action in a label. It tracks mouse click, enter, exit, press, and release events. The second program uses both MouseListener and MouseMotionListener interfaces to track mouse events in an applet and change the background color in response to events while displaying the mouse position. It tracks mouse click, enter, exit, press, release, move, and drag events. Both programs display the mouse coordinates and status messages for different mouse actions.

Uploaded by

minakshibhavar91
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views5 pages

Ajp11. Minu

The document contains code for two Java programs. The first program uses a MouseListener interface to display text indicating the mouse action in a label. It tracks mouse click, enter, exit, press, and release events. The second program uses both MouseListener and MouseMotionListener interfaces to track mouse events in an applet and change the background color in response to events while displaying the mouse position. It tracks mouse click, enter, exit, press, release, move, and drag events. Both programs display the mouse coordinates and status messages for different mouse actions.

Uploaded by

minakshibhavar91
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical No: 11

Name :- Minakshi Sunil Bhavar


Roll no :- 20

1.Write a program using MouseListener.


import java.awt.*;
import java.awt.event.*;
public class MouseListenerExample extends Frame implements MouseListener{
Label l;
MouseListenerExample(){
addMouseListener(this);

l=new Label();
l.setBounds(20,50,100,20);
add(l);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
l.setText("Mouse Clicked");
}
public void mouseEntered(MouseEvent e) {
l.setText("Mouse Entered");
}
public void mouseExited(MouseEvent e) {
l.setText("Mouse Exited");
}
public void mousePressed(MouseEvent e) {
l.setText("Mouse Pressed");
}
public void mouseReleased(MouseEvent e) {
l.setText("Mouse Released");
}
public static void main(String[] args) {
new MouseListenerExample();
}
}

OUTPUT:
2.Write a program using MouseMotion Listener interface.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/*
<applet code="Mouse" width=500 height=500>
</applet>
*/
public class Mouse extends Applet implements MouseListener,MouseMotionListener
{
int X=0,Y=20;
String msg="MouseEvents";
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
setBackground(Color.black);
setForeground(Color.red);
}
public void mouseEntered(MouseEvent m)
{
setBackground(Color.magenta);
showStatus("Mouse Entered");
repaint();
}
public void mouseExited(MouseEvent m)
{
setBackground(Color.black);
showStatus("Mouse Exited");
repaint();
}
public void mousePressed(MouseEvent m)
{
X=10;
Y=20;
setBackground(Color.green);
repaint();
}
public void mouseReleased(MouseEvent m)
{
X=10;
Y=20;
setBackground(Color.blue);
repaint();
}
public void mouseMoved(MouseEvent m)
{
X=m.getX();
Y=m.getY();
setBackground(Color.white);
showStatus("Mouse Moved");
repaint();
}
public void mouseDragged(MouseEvent m)
{
setBackground(Color.yellow);
showStatus("Mouse Moved"+m.getX()+" "+m.getY());
repaint();
}
public void mouseClicked(MouseEvent m)
{
setBackground(Color.pink);
showStatus("Mouse Clicked");
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,X,Y);
}
}

OUTPUT:

You might also like