Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Doubt in Listener interface working mechanism (http://www.go4expert.com/forums/doubt-listener-interface-mechanism-t9015/)

shyam_oec 24Feb2008 02:14

Doubt in Listener interface working mechanism
 
Code:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/*
<applet code="AdapterDemo" width=300 height=100>
</applet>
*/

public class AdapterDemo extends Applet
{
  String str="";

  public void init()
  {  setBackground(Color.red);
    addMouseListener(new MyMouseAdapter());
    addMouseMotionListener(new MyMouseMotionListener());
  }
   
  public void paint(Graphics g)
  {
    g.drawString(str,20,30);
  }

}

class MyMouseAdapter extends MouseAdapter
{
  AdapterDemo adapterdemo=new AdapterDemo();
  public void mouseClicked(MouseEvent me)
 { adapterdemo.showStatus("mouse clicked"); 
    adapterdemo.str="mouse clicked";
    adapterdemo.repaint();
 }
}

class MyMouseMotionListener extends MouseMotionAdapter
{
  AdapterDemo adapterdemo=new AdapterDemo();
  public void mouseMoved(MouseEvent me)
  {
    int x,y;
    x=me.getX();
    y=me.getY();
    adapterdemo.str="mouse moved";
    adapterdemo.repaint();
    adapterdemo.showStatus("mouse at :"+x+","+y);
  }
}


Why this code generated runtime error/exceptions.Is it always necessery to pass event generator object reference to the Event Listener class through 'this',as a parameter in the event registration method?
like addMouseListener(new MyMouseAdapter(this));
addMouseMotionListener(new MyMouseMotionListener(this));


All times are GMT +5.5. The time now is 00:29.