|
In a plugin, I have an interactive method method that turns the mouse into
the line-drawing tool and returns a couple of co-ordinates from points on
the line. It works fine inside a class that extends PlugInFrame, and
implements ActionListener. (see "getManualLineSelection" below)
Now, if I call the method from behind a JButton (in a class that extends
Jframe and implements Plugin) it doesn't work (code skeleton below..)
final JButton startbutton = new JButton("start");
startbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// code inside here
int[][] coords = getManualLineSelection(imp)
//etc ...
});
What happens is the mouse pointer is successfully turned into a line drawer,
but when I click in the image window, nothing happens. The method times out.
Is there any way to overcome this apparent conflict?
Tony
----------------------
public int[][] getManualLineSelection(ImagePlus imp)
{
Toolbar Tb = Toolbar.getInstance();
Tb.setTool(4);
Roi roi = null;
imp.killRoi(); // erase any existing ROI
roi = waitForRoiLine(imp,50); // wait for new ROI
if(roi==null) IJ.log("timed out!");
Polygon p = roi.getPolygon();
int[][] coords = new int[2][2];
coords[0][0] = p.xpoints[0];
coords[1][0] = p.ypoints[0];
coords[0][1] = p.xpoints[1];
coords[1][1] = p.ypoints[1];
return coords;
}
public Roi waitForRoiLine(ImagePlus imp, int limit){
int COMPLETE = 3;
int elapsed = 0, interval = 500;
int timeOut = 1000*limit;
Roi roi = null;
while((roi==null) || (roi.getState()!=COMPLETE))
{
IJ.wait(interval);
roi = imp.getRoi();
elapsed += interval;
if(elapsed>timeOut) return null;
}
return roi;
}
|