Expreriment No.
11
XIII
1] Program to change the background colour of Applet when user performs mouse
events (Write)
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
/*<applet code="mousev.class" height="400" width="400"></applet>*/
public class mousev extends Applet implements MouseListener
{ Button b;
String msg;
int x=100,y=100;
public void init()
{
msg="Click the above button";
FlowLayout f1=new FlowLayout();
setLayout(f1);
b=new Button("Click me");
b.addMouseListener(this);
add(b);
setVisible(true);
setSize(400,400);
}
public void paint(Graphics g)
{
g.drawString(msg,x,y);
}
public void mouseClicked(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
setBackground(Color.red);
msg="Mouse Pressed ";
}
public void mouseReleased(MouseEvent e)
{
setBackground(Color.blue);
msg="Mouse Released ";
}
public void mouseEntered(MouseEvent e)
{
setBackground(Color.yellow);
msg="Mouse Entered ";
}
public void mouseExited(MouseEvent e)
{
setBackground(Color.white);
msg="Mouse Exited ";
}
print
2] PROGRAM TO COUNT THE NUMBER OF CLICKS PERFORMED BY USER
IN FRAME WINDOW
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
public class mousev2 extends Frame implements MouseListener
{
Label l,l2;Frame f;
String msg;
int x=100,y=100;
static int n=0;
mousev2()
{
f=new Frame();
msg="Click the above button";
FlowLayout f1=new FlowLayout(40,40,FlowLayout.CENTER);
f.setLayout(f1);
f.addMouseListener(this);
l=new Label("Click on Frame ");
l2=new Label("");
f.add(l);
f.add(l2);
f.setVisible(true);
f.setSize(500,300);
f.addWindowListener(new mywindowadpt());
public void mouseClicked(MouseEvent e)
{
System.out.println(e);
++n;
l2.setText("Number of clicks = "+n);
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
class mywindowadpt extends WindowAdapter
{ public void windowClosing(WindowEvent e)
{f.dispose();
}
}
public static void main(String args[])
{
mousev2 m=new mousev2();
}
}
3] PROGRAM TO IMPLEMENT MOUSEMOTIONLISTENER
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
/*<applet code="mousev3.class" height="400" width="400"></applet>*/
public class mousev3 extends Applet implements MouseMotionListener
{
String msg;
int x=100,y=100;
int n=0;
public void init()
{
msg="Move or Drag Mouse";
FlowLayout f1=new FlowLayout();
setLayout(f1);
addMouseMotionListener(this);
setVisible(true);
setSize(300,300);
}
public void paint(Graphics g)
{
g.drawString(msg,x,y);
}
public void mouseDragged(MouseEvent e)
{
setBackground(Color.blue);
setForeground(Color.white);
msg="Mouse Dragged ";
repaint();
}
public void mouseMoved(MouseEvent e)
{
setBackground(Color.red);
setForeground(Color.white);
msg="Mouse Moved ";
repaint();
}