start a user-defined ROI

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

start a user-defined ROI

Dax-3
Hi there,

I would like to write a plugin that allows the user to draw a ROI and hit a key to signal when he's done. Following that, he'll be able to select the starting point for a new ROI and then draw this ROI, hit a key and so on.

Unfortunately I am having trouble to start a user-defined ROI as long as the key listener is still active. Anyone got an idea why? Or perhaps an alternative for signalling when the user is done?
I've tried removing the key listener, but somehow this didn't really work, either. Plus, then I won't be able to know when the new selection is drawn!
Below is a skeletonized version of the code I've got so far.

Thanks! Winnie

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

public class Branch_Select implements PlugInFilter, KeyListener {
        ImagePlus imp;
        ImageProcessor ip;

        public int setup(String arg, ImagePlus imp) {
                this.imp = imp;
                return DOES_ALL;
        }

        public void run(ImageProcessor ip) {
                ImageWindow win = imp.getWindow();
                ImageCanvas canvas = win.getCanvas();
                canvas.addKeyListener(this);
             // instruct the user on what to do
       }

        public void keyPressed(KeyEvent e) {

                Roi roi = imp.getRoi();
                if (roi==null)
                        throw new IllegalArgumentException("ROI required");
                if (!(roi instanceof PolygonRoi))
                        throw new IllegalArgumentException("Irregular area or line selection required");

              // do something with the ROI

              selectNext(x,y);
                return;
        }

        public void keyReleased(KeyEvent e) {}
        public void keyTyped(KeyEvent e) {}

        public void selectNext(int x, int y) {

                PolygonRoi proi = new PolygonRoi(x,y,imp);
                imp.setRoi(proi);
//           I have tried removing the key listener like this:
// ImageWindow win = imp.getWindow();
// ImageCanvas canvas = win.getCanvas();
// canvas.removeKeyListener(this);
       }