0% found this document useful (0 votes)
6 views9 pages

PR AJPA11

The document contains practical programming exercises from DKTE’s Yashwantrao Chavan Polytechnic, focusing on Java Applet development using mouse events. It includes code samples for changing background colors based on mouse actions, counting mouse clicks, debugging a mouse event program, and demonstrating mouse motion methods. Each section provides code snippets along with expected outputs for various user interactions with the applets.

Uploaded by

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

PR AJPA11

The document contains practical programming exercises from DKTE’s Yashwantrao Chavan Polytechnic, focusing on Java Applet development using mouse events. It includes code samples for changing background colors based on mouse actions, counting mouse clicks, debugging a mouse event program, and demonstrating mouse motion methods. Each section provides code snippets along with expected outputs for various user interactions with the applets.

Uploaded by

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

DKTE’s Yashwantrao Chavan Polytechnic , Ichalkaranji

PRACTICAL NO : 11

ENROLLMENT NO : 2215770092
NAME: Vedraj Sanjay jagdale

1. Write a program to change the background color of Applet when user performs
events using Mouse.
Code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code="Moucolorex1" height=400 width=400>
</applet>*/
public class Moucolorex1 extends Applet implements MouseListener
{
Label l; public
void init()
{ setLayout(null); l=new
Label("Hello Mouse");
addMouseListener(this);
}
public void mousePressed(MouseEvent e)
{ setBackground(Color.green);
showStatus("mouse pressed");
repaint();
}
public void mouseReleased(MouseEvent e)
{ setBackground(Color.blue);
showStatus("mouse
released");
repaint();
}
public void mouseEntered(MouseEvent e)

Vedraj Sanjay jagdale


DKTE’s Yashwantrao Chavan Polytechnic , Ichalkaranji

{ setBackground(Color.red);
showStatus("mouse
entered");
repaint();
}
public void mouseExited(MouseEvent e)
{ setBackground(Color.black);
showStatus("mouse exited");
repaint();
}
public void mouseClicked(MouseEvent e)
{ setBackground(Color.yellow);
showStatus("mouse clicked"); repaint();
}
}
Outputs:
1. After mouse entered-

2. After mouse exited-

Vedraj Sanjay jagdale


DKTE’s Yashwantrao Chavan Polytechnic , Ichalkaranji

3. After mouse clicked-

4. After mouse pressed-

Vedraj Sanjay jagdale


DKTE’s Yashwantrao Chavan Polytechnic , Ichalkaranji

4.After mouse released-

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.*;
/*<applet code="Msclick1" height=300 width=300>
</applet>*/
public class Msclick1 extends Applet implements MouseListener
{
TextField l; public
void init()

Vedraj Sanjay jagdale


DKTE’s Yashwantrao Chavan Polytechnic , Ichalkaranji

{ l=new TextField("Hello Mouse",35);


add(l);
addMouseListener(this);
}
public void mousePressed(MouseEvent e)
{

}
public void mouseReleased(MouseEvent e)
{

} public void
mouseEntered(MouseEvent e)
{

} public void
mouseExited(MouseEvent e)
{

}
public void mouseClicked(MouseEvent e)
{
l.setText("mouse clicked (Total number of clicks ):"
+e.getClickCount());
repaint();
}
}

Vedraj Sanjay jagdale


DKTE’s Yashwantrao Chavan Polytechnic , Ichalkaranji

Output: After clicking 7 times-

3. Debug the following Program code and write the


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

/* <APPLET CODE ="MouseDemo" Width=300 Height=300> </APPLET> */

public class MouseDemo extends Applet implements MouseListener


{
Label l; public
void init()
{ setLayout(null); l=new
Label("Hello Mouse");
l.setBounds(30,100,150,90);
add(l);
l.addMouseListener(this);
}
public void mousePressed(MouseEvent e)
{
l.setBounds(30,100,150,90);
l.setText("Mouse Pressed no. of clicks:" + e.getClickCount() +
"atposition" + e.getX()
+ ","+ e.getY());

Vedraj Sanjay jagdale


DKTE’s Yashwantrao Chavan Polytechnic , Ichalkaranji

} 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:

Vedraj Sanjay jagdale


DKTE’s Yashwantrao Chavan Polytechnic , Ichalkaranji

4. Write a program to demonstrate the use of mouseDragged and mouseMoved


method of MouseMotionListener.
Code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code="mousec1" width=300 height=300>
</applet>*/
public class mousec1 extends Applet implements MouseMotionListener
{
TextField t=new TextField(20);
public void init()
{ add(t);
addMouseMotionListener(this);
}

Vedraj Sanjay jagdale


DKTE’s Yashwantrao Chavan Polytechnic , Ichalkaranji

public void mouseMoved(MouseEvent me)


{ setBackground(Color.blue);
t.setText("mouseMoved"); }
public void mouseDragged(MouseEvent me)
{ setBackground(Color.red);
t.setText("mouseDragged");
}
}

Output:

Vedraj Sanjay jagdale

You might also like