Program Statement: Write a JAVA applet program for handling Mouse Events.


Tested on:

Software: Windows 8, Jdk 1.6,   Netbeans IDE

Hardware: Intel Core i3, 4 GB RAM, 500 GB HDD




Source Code

@Author: Praveen Kanwar
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
public class MouseHandling extends Applet implements MouseListener, MouseMotionListener
{
    String msg="";
public void init()
{
    addMouseListener(this);
    addMouseListener(this);
   
}
public void mouseEntered(MouseEvent me)
{
    setBackground(Color.RED);
    msg="Mouse Entered";
    showStatus("Mouse Entered at:"+me.getX()+","+me.getY());
    repaint();
}
public void mouseExited(MouseEvent me)
{
    setBackground(Color.WHITE);
    msg="Mouse Exited";
    showStatus("Mouse Exited"+me.getX()+","+me.getY());
    repaint();
}
public void mouseDragged(MouseEvent me)
{
 setBackground(Color.RED);
    msg="Mouse Dragged";
    showStatus("Mouse Dragged at:"+me.getX()+","+me.getY());
    repaint();  
}
public void mousePressed(MouseEvent me)
{
  
}
public void mouseReleased(MouseEvent me)
{
   
}
public void mouseClicked(MouseEvent me)
{
 setBackground(Color.RED);
    msg="Mouse Clicked";
    showStatus("Mouse Clicked at:"+me.getX()+","+me.getY());
    repaint();    
}
public void mouseMoved(MouseEvent me)
{
    setBackground(Color.RED);
    msg="Mouse moved";
    showStatus("Mouse Moved at:"+me.getX()+","+me.getY());
    repaint(); 
}
public void paint(Graphics g)
{
    g.drawString(msg,30,30);
}
  
}

Output


Post a Comment

 
Top