fry
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.