0% found this document useful (0 votes)
24 views4 pages

AJP Practical 11

Uploaded by

258 Gauri Jadhav
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)
24 views4 pages

AJP Practical 11

Uploaded by

258 Gauri Jadhav
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/ 4

Practical 11 :Write a program to demonstrate mouse events– 2200100176 Vedika Mohite

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


import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/*<applet code="MouseEventClass4" width="300" height="200"> </applet> */
public class MouseEventClass4 extends Applet implements MouseListener {
Label l;
public void init()
{
setLayout(null);
l=new Label("Hello Mouse");
l.setBounds(50,50,300,100) ;
add(l);
addMouseListener(this); //Corrected line
}

@Override
public void mouseClicked(MouseEvent e) {
l.setText("Mouse clicked(# of clicks:"+e.getClickCount());
}

@Override
public void mouseEntered(MouseEvent arg0) {
l.setText("Mouse Entered");
}

@Override
public void mouseExited(MouseEvent arg0) {
l.setText("Mouse Exited");
}

@Override
public void mousePressed(MouseEvent e) {
l.setText("Mouse Pressed no. of clicks:"+e.getClickCount()+"at position
"+e.getX()+","+e.getY());
}

@Override
public void mouseReleased(MouseEvent e) {
l.setText("Mouse Released;# of clicks:"+e.getClickCount());
}
}

Output :-

Vedika Mohite
Practical 11 :Write a program to demonstrate mouse events – 2200100176 Vedika Mohite

Exercise Q.1: Write a program to change the background color of applet when user performs events using Mouse.


import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MouseEventClass1 extends Applet implements MouseListener{

public void init()


{
addMouseListener(this);
}

@Override
public void mouseClicked(MouseEvent arg0) {
setBackground(Color.cyan);
}

@Override
public void mouseEntered(MouseEvent arg0) {
setBackground(Color.pink);
}

@Override
public void mouseExited(MouseEvent arg0) {
setBackground(Color.white);
}

@Override
public void mousePressed(MouseEvent arg0) {
setBackground(Color.blue);
}

@Override
public void mouseReleased(MouseEvent arg0) {
setBackground(Color.GREEN);

}
}

Output :-

Vedika Mohite
Practical 11 :Write a program to demonstrate mouse events– 2200100176 Vedika Mohite

Exercise Q.2: Write a program to count the number of clicks performed by the user in a frame window.


import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
public class MouseEventClass2 implements MouseListener{

Frame f;
int i=0;
Label l1,l2;
MouseEventClass2()
{
f=new Frame();
l1=new Label("No of clicks= ");
l2=new Label(" ");
f.add(l1);
f.add(l2);
f.addMouseListener(this);

f.setLayout(new FlowLayout());
f.setSize(500,500);
f.setVisible(true);
}
public static void main(String args[])
{
MouseEventClass2 m1=new MouseEventClass2();
}

@Override
public void mouseClicked(MouseEvent e) {
i=i+e.getClickCount();
l2.setText(i+"");
}

@Override
public void mouseEntered(MouseEvent arg0) {
}

@Override
public void mouseExited(MouseEvent arg0) {
} Output :-

@Override
public void mousePressed(MouseEvent arg0) {
}

@Override
public void mouseReleased(MouseEvent arg0) {

Vedika Mohite
Practical 11 :Write a program to demonstrate mouse events – 2200100176 Vedika Mohite

Exercise Q.3: Write a program to demonstrate the use of mouseDragged and mouseMoved method of
MouseMotionListener.


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

public class MouseEventClass3 extends Applet implements MouseMotionListener{


int x,y;
public void init()
{
addMouseMotionListener(this);
}
public void paint(Graphics g)
{

@Override
public void mouseDragged(MouseEvent e) {

Graphics g=getGraphics();
x=e.getX();
y=e.getY();
g.fillOval(x, y, 8, 8);

@Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
showStatus("Mouse is moving");

Output :-

Vedika Mohite

You might also like