0% found this document useful (0 votes)
646 views8 pages

Expreriment No.11: Xiii 1) Program To Change The Background Colour of Applet When User Performs Mouse Events

The document describes 3 Java applet programs that utilize different mouse event listeners: 1. The first program changes the background color of the applet when different mouse events occur like press, release, enter, and exit. 2. The second program counts the number of clicks on a frame window and displays the count in a label. 3. The third program implements the MouseMotionListener to change the background color and display messages for mouse drag and mouse move events.

Uploaded by

Kiran Rajput
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)
646 views8 pages

Expreriment No.11: Xiii 1) Program To Change The Background Colour of Applet When User Performs Mouse Events

The document describes 3 Java applet programs that utilize different mouse event listeners: 1. The first program changes the background color of the applet when different mouse events occur like press, release, enter, and exit. 2. The second program counts the number of clicks on a frame window and displays the count in a label. 3. The third program implements the MouseMotionListener to change the background color and display messages for mouse drag and mouse move events.

Uploaded by

Kiran Rajput
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/ 8

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();
}

You might also like