/** * Double click on a position in the image and have that position move * to a predefined place * Author : Nico Stuurman (with requests by Zilu Wu), 2008-2010 * * To use, run the script, then type: * mL_.aim(x, y); * with x and y being the x and y coordinates that you want the position to move to * To enter coordinates in microns, use the following instead: * mL_.aimUm(x, y); * * When needed, stop the script usings: * mL_.stop(); */ import ij.ImagePlus; import ij.WindowManager; import ij.gui.ImageCanvas; import ij.gui.ImageWindow; import ij.gui.Toolbar; import java.awt.Event; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import javax.swing.JOptionPane; import mmcorej.CMMCore; import mmcorej.MMCoreJ; public class MyListener implements MouseListener, MouseMotionListener { private CMMCore core_; private ImageCanvas canvas_; private static boolean isRunning_ = false; private boolean mirrorX_; private boolean mirrorY_; private boolean transposeXY_; private boolean correction_; private int lastX_, lastY_; private int aimX_, aimY_; public MyListener(CMMCore core) { core_ = core; aimX_ = 256; aimY_ = 256; } public aim(int x, int y) { aimX_ = x; aimY_ = y; } public aimUm(double x, double y) { double pixSizeUm = core_.getPixelSizeUm(); if (! (pixSizeUm > 0.0)) { JOptionPane.showMessageDialog(null, "Please provide pixel size calibration data before using this function"); return; } aimX_ = (int) (x / pixSizeUm); aimY_ = (int) (y / pixSizeUm); } public void start () { if (isRunning_) return; isRunning_ = true; // Get a handle to the AcqWindow win = gui.getSnapLiveWin(); if (win != null) { ImagePlus img = win.getImagePlus(); attach(img.getWindow()); } else { gui.message("Failed to attach Mouse Listener"); } getOrientation(); } public void stop() { if (canvas_ != null) { canvas_.removeMouseListener(this); canvas_.removeMouseMotionListener(this); } isRunning_ = false; } public boolean isRunning() { return isRunning_; } public void attach(ImageWindow win) { if (!isRunning_) return; canvas_ = win.getCanvas(); canvas_.addMouseListener(this); canvas_.addMouseMotionListener(this); if (Toolbar.getInstance() != null) { //TO DO: Set appropriate tool Toolbar.getInstance().setTool(Toolbar.HAND); } gui.message("Attached MouseListener"); } public void getOrientation() { String camera = core_.getCameraDevice(); if (camera == null) { JOptionPane.showMessageDialog(null, "This function does not work without a camera"); return; } try { String tmp = core_.getProperty(camera, "TransposeCorrection"); if (tmp.equals("0")) correction_ = false; else correction_ = true; tmp = core_.getProperty(camera, MMCoreJ.getG_Keyword_Transpose_MirrorX()); if (tmp.equals("0")) mirrorX_ = false; else mirrorX_ = true; tmp = core_.getProperty(camera, MMCoreJ.getG_Keyword_Transpose_MirrorY()); if (tmp.equals("0")) mirrorY_ = false; else mirrorY_ = true; tmp = core_.getProperty(camera, MMCoreJ.getG_Keyword_Transpose_SwapXY()); if (tmp.equals("0")) transposeXY_ = false; else transposeXY_ = true; } catch(Exception exc) { exc.printStackTrace(); JOptionPane.showMessageDialog(null, "Exception encountered. Please report to the Micro-Manager support team"); return; } } public void mouseClicked(MouseEvent e) { // right click and single click: ignore int nc = e.getClickCount(); if ((e.getModifiers() & Event.META_MASK) != 0 || nc < 2) return; // Get needed info from core getOrientation(); String xyStage = core_.getXYStageDevice(); if (xyStage == null) return; double pixSizeUm = core_.getPixelSizeUm(); if (! (pixSizeUm > 0.0)) { JOptionPane.showMessageDialog(null, "Please provide pixel size calibration data before using this function"); return; } int width = (int) core_.getImageWidth(); int height = (int) core_.getImageHeight(); // Get coordinates of event // TODO: correct for ImageJ magnification of the screen! int x = e.getX(); int y = e.getY(); int cX = canvas_.offScreenX(x); int cY = canvas_.offScreenY(y); // calculate needed relative movement double tmpXUm = (aimX_ - cX) * pixSizeUm; double tmpYUm = (aimY_ - cY) * pixSizeUm; double mXUm = tmpXUm; double mYUm = tmpYUm; // if camera does not correct image orientation, we'll correct for it here: if (!correction_) { // Order: swapxy, then mirror axis if (transposeXY_) {mXUm = tmpYUm; mYUm = tmpXUm;} if (mirrorX_) {mXUm = -mXUm;} if (mirrorY_) {mYUm = -mYUm;} } // Move the stage try { core_.setRelativeXYPosition(xyStage, mXUm, mYUm); } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); return; } gui.message("Moving the stage: " + mXUm + " " + mYUm); } // Mouse listener implementation public void mousePressed(MouseEvent e) { // Get the starting coordinate for the dragging int x = e.getX(); int y = e.getY(); lastX_ = canvas_.offScreenX(x); lastY_ = canvas_.offScreenY(y); } public void mouseDragged(MouseEvent e) { if ((e.getModifiers() & Event.META_MASK) != 0) // right click: ignore return; // Get needed info from core getOrientation(); String xyStage = core_.getXYStageDevice(); if (xyStage == null) return; try { if (core_.deviceBusy(xyStage)) return; } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); return; } double pixSizeUm = core_.getPixelSizeUm(); if (! (pixSizeUm > 0.0)) { JOptionPane.showMessageDialog(null, "Please provide pixel size calibration data before using this function"); return; } // Get coordinates of event int x = e.getX(); int y = e.getY(); int cX = canvas_.offScreenX(x); int cY = canvas_.offScreenY(y); // calculate needed relative movement double tmpXUm = cX - lastX_; double tmpYUm = cY - lastY_; tmpXUm *= pixSizeUm; tmpYUm *= pixSizeUm; double mXUm = tmpXUm; double mYUm = tmpYUm; // if camera does not correct image orientation, we'll correct for it here: if (!correction_) { // Order: swapxy, then mirror axis if (transposeXY_) {mXUm = tmpYUm; mYUm = tmpXUm;} if (mirrorX_) {mXUm = -mXUm;} if (mirrorY_) {mYUm = -mYUm;} } // Move the stage try { core_.setRelativeXYPosition(xyStage, mXUm, mYUm); } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); return; } lastX_ = cX; lastY_ = cY; } public void mouseReleased(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseMoved(MouseEvent e) {} } mL_ = new MyListener(mmc); mL_.start();