• Post Reply Bookmark Topic Watch Topic
  • New Topic

basic question about events

kashif sohail
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AWT events :
For example I have a class
All imports
Public class MouseEvents extends Applets implements MouseListner,MouseMotionListner{
String msg=� �;
Int mouseX=0,mouseY=0;
Public void init() {
addMotionListner (this); // I didn�t understand the use of this Plz explain
addMouseListner (this);
public void mousePressed(MouseEvent me){
consider all other required methods are here (implemented)
public void paint(Graphic g) {
My problem is this I m unable to figure out that
1: what does this do.
2: how mousePressed and other methods r called as I have not given any parameter to them ( actually I think I have just created the method which takes an object of type MouseEvent ) I don�t know how and at what time I have passed the parameter object of type MouseEvent and how I have used the me.getX with out passing parameter ,
did any new instance of object is created some where implicitly as it is the same case with Graphic .

Sean MacLean
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the short story. Most of the AWT Components in Java are designed for use in a Graphical User Interface. One of the key concepts of a GUI is that the interface waits for the user to do something and then reacts to that 'something' and then simply waits for something else (a big ol' loop). What the Components are waiting for is an 'event'. So, if you design a Component (ie. Applet, Frame, Button, etc) for use in a GUI, you'll want it to 'fire' an event when the user clicks, resizes, etc, that Component. And, to make things complete, you want to have something, somewhere, 'listening' for the event so that the program can do something about it.
In the example you've given above, the statement "addMouseListener(this)" tells the underlying process that if the Applet you've added the Listener to gets clicked, etc by a mouse, then pass the event to the specified MouseListener (the 'this' that you passed to addMouseListener). Now, by having your Applet implement MouseListener, your making it the object to send any mouse events too. Soooooo, the Applet has been specified as the generator of the mouse events with addMouseListener() and it has also been deemed to be the handler of the mouse events via addMouseListener(this) (where the 'this' is the Applet because of the implements MouseListener. I hope my explaination adds a bit of clarity. It's pretty neat stuff once you get a handle on it. Just be glad you don't have to use the EventMasks from the Java 1.0 days - those were sad times!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!