File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes basic question about events Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "basic question about events" Watch "basic question about events" New topic
Author

basic question about events

kashif sohail
Greenhorn

Joined: Dec 04, 2000
Posts: 29
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){
mouseX=me.getX();
mouseY=me.getY();
msg=�Down�;
repaint();
}
consider all other required methods are here (implemented)
public void paint(Graphic g) {
g.drawstring(msg,mouseX,mouseY);
}
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
author
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
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!
Sean
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: basic question about events