Login  Register

Re: How can I get input from an event handler?

Posted by Avital Steinberg on Aug 18, 2014; 3:01pm
URL: http://imagej.273.s1.nabble.com/How-can-I-get-input-from-an-event-handler-tp5009243p5009258.html

Thank you for your answer - how would I use such a mouse listener from
another class in the same project.

If I would use the code in the link you sent me in one class:

import ij.*;
import ij.plugin.filter.PlugInFilter;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

        /**
        This plugin implements the MouseListener and MouseMotionListener interfaces
        and listens for mouse events generated by the current image.
        */
        public class Mouse_Listener implements PlugInFilter, MouseListener,
MouseMotionListener {
                ImagePlus img;
                ImageCanvas canvas;
                static Vector images = new Vector();

        public int setup(String arg, ImagePlus img) {
                this.img = img;
                IJ.register(Mouse_Listener.class);
                return DOES_ALL+NO_CHANGES;
        }

        public void run(ImageProcessor ip) {
                Integer id = new Integer(img.getID());
                if (images.contains(id)) {
                        IJ.log("Already listening to this image");
                        return;
                } else {
                        ImageWindow win = img.getWindow();
                        canvas = win.getCanvas();
                        canvas.addMouseListener(this);
                        canvas.addMouseMotionListener(this);
                        //int tool = Toolbar.getInstance().addTool("Test Tool");
                        //Toolbar.getInstance().setTool(tool);
                        images.addElement(id);
                }
        }
       
        public void mousePressed(MouseEvent e) {
                int x = e.getX();
                int y = e.getY();
                int offscreenX = canvas.offScreenX(x);
                int offscreenY = canvas.offScreenY(y);
                IJ.log("Mouse pressed:
"+offscreenX+","+offscreenY+modifiers(e.getModifiers()));
                //IJ.log("Right button: "+((e.getModifiers()&Event.META_MASK)!=0));
        }

        public void mouseReleased(MouseEvent e) {
                IJ.log("mouseReleased: ");
        }
       
        public void mouseDragged(MouseEvent e) {
                int x = e.getX();
                int y = e.getY();
                int offscreenX = canvas.offScreenX(x);
                int offscreenY = canvas.offScreenY(y);
                IJ.log("Mouse dragged:
"+offscreenX+","+offscreenY+modifiers(e.getModifiers()));
        }

        public static String modifiers(int flags) {
                String s = " [ ";
                if (flags == 0) return "";
                if ((flags & Event.SHIFT_MASK) != 0) s += "Shift ";
                if ((flags & Event.CTRL_MASK) != 0) s += "Control ";
                if ((flags & Event.META_MASK) != 0) s += "Meta (right button) ";
                if ((flags & Event.ALT_MASK) != 0) s += "Alt ";
                s += "]";
                if (s.equals(" [ ]"))
  s = " [no modifiers]";
                return s;
        }

        public void mouseExited(MouseEvent e) {}
        public void mouseClicked(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseMoved(MouseEvent e) {}


}

How would I access offscreenX and offscreenY from another class?



Avital


On Mon, Aug 18, 2014 at 6:29 AM, Robert Dougherty <[hidden email]> wrote:

> Avital,
>
> Wayne has a nice example at
> http://imagej.nih.gov/ij/plugins/mouse-listener.html.  To listen for
> mouse clicks on an ImagePlus, the trick is to get the ImageCanvas for that
> ImagePlus and add a MouseListener for the canvas to "this".
>
> Bob
>
> On Aug 17, 2014, at 7:46 AM, Avital Steinberg <[hidden email]>
> wrote:
>
> > Hi,
> > I have an event handler which gets input from mouse clicks. It's in a
> class
> > (class Mouse_Listener) that implents the interfaces PlugInFilter,
> > MouseListener and MouseMotionListener. I'm writing a plugin which is in
> > another class in the same package. (it implements the PlugIn interface)
> How
> > can I extract the information from the Mouse_Listener class, so that I
> can
> > use it in the other class?
> >
> > Thank you,
> > Avital
> >
> > --
> > ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>
> Robert P. Dougherty
> President
> OptiNav, Inc.
> 1414 127th Pl NE #106
> Bellevue, WA 98005
> (425) 891-4883
> FAX (425) 467-1119
> [hidden email]
> www.optinav. com
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html