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

Joined Date :04-Feb-2013
Articles
Posted
27
Messages
Posted
0


   
Messages
Posted:
Post Your Comments
Name (Max 50 Chars)
Comments
   Design  HTML

TekTipsDownload
GateExam
Academic Projects
TekTipsExperts



 
Site optimized for IE7, 1280 X 768 and above. Copyright © 2010 - 2018 KTS InfoTech
Site Developed Using KTS WebCloud