We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProcessing DevelopmentLibraries,  Tool Development › Accessing MouseEvents in libaries
Page Index Toggle Pages: 1
Accessing MouseEvents in libaries (Read 2583 times)
Accessing MouseEvents in libaries
Jun 9th, 2005, 8:11pm
 
Hey, I'm working on putting together a MyGUI library and wanted to understand the best way to access mouse events.

The code I've come up with that works looks something like the code below:
Code:
public class MyMouseTest implements MouseListener, MouseMotionListener {
// core variables
private PApplet parent;

// default constructor
public MyMouseTest(PApplet parent) {
this.parent = parent;
// Register mouse events
parent.registerMouseEvent(this);
addMouseListener(this);
addMouseMotionListener(this);
}

/* Methods for handling mouse events */
// Track mouse events
public void mousePressed(MouseEvent e) {
println("Mouse pressed:");
}

public void mouseReleased(MouseEvent e) {
println("Mouse released:");
}

public void mouseDragged(MouseEvent e) {
println("Mouse dragged:");
}

// Non used events from MouseListener and MouseMotionListener interfaces
public void mouseEvent(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
public void mouseMoved(MouseEvent e) { }

}


Implemented:
Code:
MyMouseTest mt;

void setup() {
// Create MyGUI object:
mt = new MyMouseTest(this);
// Set applet size and framerate
size(300, 300);
background(153);
framerate(25);
}

void draw() {

}


So this works, it prints messages out to the console when you click/release/drag across the applet. Is there a cleaner way of doing this?
I mean, did I have to implement MouseListener and MouseMovedListener?
What is the point of registeMouseEvent?

Thanks for any advice people can give.
Re: Accessing MouseEvents in libaries
Reply #1 - Jun 9th, 2005, 8:58pm
 
i started writing a response but realized that this just needs to go into the library howto, so what follows is a new section that should cover your questions. short answer is that you shouldn't use addMouseListener, read on for why:


//////////////////////////////////////////////////////////////


LIBRARY METHODS

public void pre()
method that's called just after beginFrame(), meaning that it
can affect drawing.

public void draw()
method that's called at the end of draw(), but before endFrame().

public void mouseEvent(MouseEvent e)
called when a mouse event occurs in the parent applet

public void keyEvent(KeyEvent e)
called when a key event occurs in the parent applet

public void post()
method called after draw has completed and the frame is done.
no drawing allowed.

public void size(int width, int height)
this will be called the first time an applet sets its size, but
also any time that it's called while the PApplet is running.

public void stop()
can be called by users, for instance movie.stop() will shut down
a movie that's being played, or camera.stop() stops capturing
video. server.stop() will shut down the server and shut it down
completely, which is identical to its "dispose" function.

public void dispose()
this should only be called by PApplet. dispose() is what gets
called when the host applet is stopped, so this should shut down
any threads, disconnect from the net, unload memory, etc.

To register any of these methods with the parent, call
parent.registerPre(this) or whatever the name of the function
is that you'd like to use.

Note that making things "public" is extremely important. When running
inside Processing, anything left blank has public added by the
preprocessor, meaning "void loop()" becomes "public void loop()".
But in straight Java, the public becomes important.

You can only draw inside of pre(), draw(), mouseEvent(), or keyEvent()
otherwise you may run into trouble. pre() and draw() happen while
legitimate drawing is taking place, and the mouse/key events happen
just before draw() events are called, they're queued up by the host
applet until it's safe to draw.

For this reason, you should use registerMouse() and mouseEvent() (and
same for the keys) to handle your events, rather than your class
implementing MouseListener. For instance, to figure out what the mouse
event is throwing back at you, this would be an example handler:

public void mouseEvent(MouseEvent event) {
 int x = event.getX();
 int y = event.getY();

 switch (event.getID()) {
   case MouseEvent.MOUSE_PRESSED:
     // do something for the mouse being pressed
     break;
   case MouseEvent.MOUSE_RELEASED:
     // do something for mouse released
     break;
   case MouseEvent.MOUSE_CLICKED:
     // do something for mouse clicked
     break;
   case MouseEvent.MOUSE_DRAGGED:
     // do something for mouse dragged
     break;
   case MouseEvent.MOUSE_MOVED:
     // umm
     break;
 }
}

More on mouse handling can be found in Sun's Java documentation:
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/MouseEvent.html
which also covers things like modifiers (shift-click) and whatnot.
Re: Accessing MouseEvents in libaries
Reply #2 - Jun 9th, 2005, 9:46pm
 
Hey, I'm working on putting together a MyGUI library and wanted to understand the best way to access mouse events.

Hi Markavian! I've put the finishing touches on a first release of my own GUI library today, which is a wrapper over a lot of components found in the awt. I was going to release it this weekend, together with a redesigned version of my website. Now I'm feeling awkward about not having announced it beforehand. I've kept quiet about it because I didn't want to make any promises I can't keep.
Is there an etiquette for the library writing procedure or a precedent in this respect?
Markavian, how far are you with your own library?
Re: Accessing MouseEvents in libaries
Reply #3 - Jun 9th, 2005, 10:21pm
 
Thanks for that information Fry, event.getID(); was the method I couldn't quite find when browswing through the Java API notes. I figured I was meant to be using the mouseEvent(MouseEvent e) { } method to access mousePressed, mouseDragged, etc. but wasn't sure how to implement it.

Phillip - I've responded to your comment in another post, quoting what you said.
Page Index Toggle Pages: 1