Launching mouse listener

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Launching mouse listener

AJBell
Dear List,

I have written a plugin that uses the mouse listener to perform an action on an open image, which works fine when I run it from the Plugins menu. What I would like to do, and I am a bit stumpted, is to run this plugin from inside a PlugInFrame as it forms part of a larger analysis. I had thought I could just create an instance to run it (as is that not what I am doing from the plugins menu?), but my code below does not work. Any help would be appreciated as I am still fairly new to java!

Andrew Bell
University of Leicester

public class Example_Frame extends PlugInFrame implements ActionListener{
       
        int i=0;

        public Example_Frame() {
                super("Example_Frame");
                Button button = new Button("Action");
                button.addActionListener(this);
                   add(button);
               
                pack();
                GUI.center(this);
                show();
        }

        public void actionPerformed(ActionEvent event){
                IJ.log("Button pressed");
                Mouse_Listener ml = new Mouse_Listener();
               
        }

}
Reply | Threaded
Open this post in threaded view
|

Re: Launching mouse listener

John Hayes
Hi Andrew,

Posting your previous code that worked might be helpful as I’m assuming it is the Mouse_Listener class?

However, it appears you are creating a mouse listener (who should probably be your Example_Frame class below) and not adding the listener to the open image’s window. Something like this is what I think you’re wanting to do.

> WindowManager.getCurrentImage().getWindow().addMouseListener(this)


where Example_Frame is defined like:

> public class Example_Frame extends PlugInFrame implements ActionListener, MouseListener {


You’ll also have to add the appropriate MouseListener implementations to compile and use the new code.

I hope that is a bit helpful,

John

On 4 oct. 2012, at 15:33, AJBell wrote:

> Dear List,
>
> I have written a plugin that uses the mouse listener to perform an action on
> an open image, which works fine when I run it from the Plugins menu. What I
> would like to do, and I am a bit stumpted, is to run this plugin from inside
> a PlugInFrame as it forms part of a larger analysis. I had thought I could
> just create an instance to run it (as is that not what I am doing from the
> plugins menu?), but my code below does not work. Any help would be
> appreciated as I am still fairly new to java!
>
> Andrew Bell
> University of Leicester
>
> public class Example_Frame extends PlugInFrame implements ActionListener{
>
> int i=0;
>
> public Example_Frame() {
> super("Example_Frame");
> Button button = new Button("Action");
> button.addActionListener(this);
>           add(button);
>
> pack();
> GUI.center(this);
> show();
> }
>
> public void actionPerformed(ActionEvent event){
> IJ.log("Button pressed");
> Mouse_Listener ml = new Mouse_Listener();
>
> }
>
> }
>
>
>
> --
> View this message in context: http://imagej.1557.n6.nabble.com/Launching-mouse-listener-tp5000306.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Launching mouse listener

AJBell
Hi John,

Thank you for you reply. If I understand you correctly, you are suggesting that the button simply adds the listener which is defined within the class to the selected image, which makes sense, however my implementation below does not work. Could you point out what I've missed as I am still not actually adding the listener?

Andrew

public class Example_Frame extends PlugInFrame implements ActionListener, MouseListener{

        public Example_Frame() {
                super("Example_Frame");
                Button button = new Button("Action");
                button.addActionListener(this);
                    add(button);
                pack();
                GUI.center(this);
                show();
        }

        public void actionPerformed(ActionEvent event){
                IJ.log("Button pressed");
                WindowManager.getCurrentImage().getWindow().addMouseListener(this);
        }

        public void mousePressed(MouseEvent e) {
                IJ.log("Mouse pressed");
               
        }
        public void mouseReleased(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
        public void mouseClicked(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}

}

John Hayes wrote
Hi Andrew,

Posting your previous code that worked might be helpful as I’m assuming it is the Mouse_Listener class?

However, it appears you are creating a mouse listener (who should probably be your Example_Frame class below) and not adding the listener to the open image’s window. Something like this is what I think you’re wanting to do.

> WindowManager.getCurrentImage().getWindow().addMouseListener(this)


where Example_Frame is defined like:

> public class Example_Frame extends PlugInFrame implements ActionListener, MouseListener {


You’ll also have to add the appropriate MouseListener implementations to compile and use the new code.

I hope that is a bit helpful,

John

On 4 oct. 2012, at 15:33, AJBell wrote:

> Dear List,
>
> I have written a plugin that uses the mouse listener to perform an action on
> an open image, which works fine when I run it from the Plugins menu. What I
> would like to do, and I am a bit stumpted, is to run this plugin from inside
> a PlugInFrame as it forms part of a larger analysis. I had thought I could
> just create an instance to run it (as is that not what I am doing from the
> plugins menu?), but my code below does not work. Any help would be
> appreciated as I am still fairly new to java!
>
> Andrew Bell
> University of Leicester
>
> public class Example_Frame extends PlugInFrame implements ActionListener{
>
> int i=0;
>
> public Example_Frame() {
> super("Example_Frame");
> Button button = new Button("Action");
> button.addActionListener(this);
>           add(button);
>
> pack();
> GUI.center(this);
> show();
> }
>
> public void actionPerformed(ActionEvent event){
> IJ.log("Button pressed");
> Mouse_Listener ml = new Mouse_Listener();
>
> }
>
> }
>
>
>
> --
> View this message in context: http://imagej.1557.n6.nabble.com/Launching-mouse-listener-tp5000306.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Launching mouse listener

John Hayes
Hi Andrew,

Sorry, this probably does what you want:

> ImagePlus image = WindowManager.getCurrentImage();
> image.getCanvas().addMouseListener(this);
       
The previous code worked on the ‘ImageWindow’ but not the area that the image is actually drawn (which is one of the contents of the ImageWindow). The ImageCanvas is where the image is actually drawn. I tested the code with the above modification and after clicking the button, you can click on the drawn image and the log message reports ‘Mouse pressed’.

Hope that helps,

John

On 4 oct. 2012, at 16:36, AJBell wrote:

> Hi John,
>
> Thank you for you reply. If I understand you correctly, you are suggesting
> that the button simply adds the listener which is defined within the class
> to the selected image, which makes sense, however my implementation below
> does not work. Could you point out what I've missed as I am still not
> actually adding the listener?
>
> Andrew
>
> public class Example_Frame extends PlugInFrame implements ActionListener,
> MouseListener{
>
> public Example_Frame() {
> super("Example_Frame");
> Button button = new Button("Action");
> button.addActionListener(this);
>            add(button);
> pack();
> GUI.center(this);
> show();
> }
>
> public void actionPerformed(ActionEvent event){
> IJ.log("Button pressed");
> WindowManager.getCurrentImage().getWindow().addMouseListener(this);
> }
>
> public void mousePressed(MouseEvent e) {
> IJ.log("Mouse pressed");
>
> }
> public void mouseReleased(MouseEvent e) {}
> public void mouseExited(MouseEvent e) {}
> public void mouseClicked(MouseEvent e) {}
> public void mouseEntered(MouseEvent e) {}
>
> }
>
>
> John Hayes wrote
>> Hi Andrew,
>>
>> Posting your previous code that worked might be helpful as I’m assuming it
>> is the Mouse_Listener class?
>>
>> However, it appears you are creating a mouse listener (who should probably
>> be your Example_Frame class below) and not adding the listener to the open
>> image’s window. Something like this is what I think you’re wanting to do.
>>
>>> WindowManager.getCurrentImage().getWindow().addMouseListener(this)
>>
>>
>> where Example_Frame is defined like:
>>
>>> public class Example_Frame extends PlugInFrame implements ActionListener,
>>> MouseListener {
>>
>>
>> You’ll also have to add the appropriate MouseListener implementations to
>> compile and use the new code.
>>
>> I hope that is a bit helpful,
>>
>> John
>>
>> On 4 oct. 2012, at 15:33, AJBell wrote:
>>
>>> Dear List,
>>>
>>> I have written a plugin that uses the mouse listener to perform an action
>>> on
>>> an open image, which works fine when I run it from the Plugins menu. What
>>> I
>>> would like to do, and I am a bit stumpted, is to run this plugin from
>>> inside
>>> a PlugInFrame as it forms part of a larger analysis. I had thought I
>>> could
>>> just create an instance to run it (as is that not what I am doing from
>>> the
>>> plugins menu?), but my code below does not work. Any help would be
>>> appreciated as I am still fairly new to java!
>>>
>>> Andrew Bell
>>> University of Leicester
>>>
>>> public class Example_Frame extends PlugInFrame implements ActionListener{
>>>
>>> int i=0;
>>>
>>> public Example_Frame() {
>>> super("Example_Frame");
>>> Button button = new Button("Action");
>>> button.addActionListener(this);
>>>           add(button);
>>>
>>> pack();
>>> GUI.center(this);
>>> show();
>>> }
>>>
>>> public void actionPerformed(ActionEvent event){
>>> IJ.log("Button pressed");
>>> Mouse_Listener ml = new Mouse_Listener();
>>>
>>> }
>>>
>>> }
>>>
>>>
>>>
>>> --
>>> View this message in context:
>>> http://imagej.1557.n6.nabble.com/Launching-mouse-listener-tp5000306.html
>>> Sent from the ImageJ mailing list archive at Nabble.com.
>>>
>>> --
>>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>
>
>
>
>
> --
> View this message in context: http://imagej.1557.n6.nabble.com/Launching-mouse-listener-tp5000306p5000309.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Launching mouse listener

AJBell
Dear John,

That works great, thank you. I thought it might have been to do with the referencing of the image but wasn't sure what was wrong.

Thanks again,

Andrew