How To Implement Mouse event In Java
Rate this article
0 out of 5
In this tutorial we will show you how to implement mouse event in java. After viewing the video tutorial, download the source code and try to modify the code so as to get a feel of what is learned in this video tutorial.


Source Code
MouseEvents.java

import  java.applet.*;
import java.awt.event.*;
import java.awt.*;
/*<applet code="MouseEvents" width=400 height=400>
</applet>*/
public class MouseEvents extends Applet implements MouseListener,MouseMotionListener
{
  String msg="";
  int mouseX=100,mouseY=100;
  
  public void init()
  {
    addMouseListener(this);
    addMouseMotionListener(this);
  }
  
  public void mouseClicked(MouseEvent me)
  {
    mouseX=0;
    mouseY=10;
    msg="Mouse clicked";
    repaint();
  }

  public void mouseEntered(MouseEvent me)
  {
    mouseX=me.getX();
    mouseY=me.getY();
    msg="Mouse Entered";
    repaint();
  }

  public void mouseExited(MouseEvent me)
  {
    mouseX=me.getX();
    mouseY=me.getY();
    msg="Mouse Exited";
    repaint();
  }
  
  public void mousePressed(MouseEvent me)
  {
    mouseX=me.getX();
    mouseY=me.getY();
    msg="Mouse Pressed";
    repaint();
  }
 
  public void start()
  {
    if(mouseX==100&& mouseY==100)
    {
      msg="You win";
     }
  repaint();
  }
   
  public void mouseReleased(MouseEvent me)
  {
    mouseX=me.getX();
    mouseY=me.getY();
    msg="Mouse Released";
    repaint();
  }

  public void mouseMoved(MouseEvent me)
  {
    showStatus("Moving mouse at"+me.getX()+","+me.getY()); 
  } 
  public void mouseDragged(MouseEvent me)
  {
    mouseX=me.getX();
    mouseY=me.getY();
    msg="***";
    showStatus("Dragging mouse at"+me.getX()+","+me.getY()); 
    repaint();
  }
 

  public void paint(Graphics g)
  {
    g.drawString(msg,mouseX,mouseY);
   
  }
}



Source Code

About The Author
TekTips Videos

Number Of articles Posted      : 27
Number Of Messages Posted  : 0
Joined Date : 2013/2/04
   
Author
Messages
Posted:
Post Your Comments  
Name (Max 50 Chars)
Comments
 
 

 
TekTipsDownload
GateExam
Academic Projects
TekTipsExperts
Android