独立使用mouseClicked和mouseDragged

时间:2014-03-20 14:00:24

标签: java jython mouselistener

我正在尝试在斐济(ImageJ)编写一个Jython脚本,该脚本允许用户放置一个点,并在放置该点时将其添加到感兴趣区域管理器。我可以这样做,但我也希望用户能够拖放已放置的点,而无需在感兴趣区域管理器中添加新条目。基本上我希望能够彼此独立地调用mouseClicked和mouseDragged,而当前拖动鼠标仍然会激活mouseClicked事件。 (我不知道是否将Java作为标签之一,但我觉得它与之密切相关,如果错误,我道歉。)

干杯!

class ML(MouseAdapter):
 def mouseClicked(self, event):
   canvas = event.getSource()
   imp = canvas.getImage()
   print click
   roi.runCommand("Add") 
   roi.runCommand("UseNames", "true")

class ML2(MouseAdapter):
 def mouseDragged(self, event):
   canvas = event.getSource()
   imp = canvas.getImage()
   print "move!"

roi = ij.plugin.frame.RoiManager()
listener = ML()
listener2 = ML2()

for imp in map(WindowManager.getImage, WindowManager.getIDList()):
 win = imp.getWindow()
 if win is None:
   continue
 win.getCanvas().addMouseListener(listener)
 win.getCanvas().addMouseMotionListener(listener2)

1 个答案:

答案 0 :(得分:0)

public void mouseClicked(MouseEvent arg0) {
}

/**
 * handles mouse pressed event
 */
public void mousePressed(MouseEvent arg0)
{
    // if filling we shouldn't add anything 
    if(b_Filling == true)
        return;
    // if dragging we shouldn't add anything
    if(isDragging == true)
        return;

    // handles first vertex for each polygon
    if(i_ThreeVertices == 0)
    {
        p_Start = arg0.getPoint();
        p_End = new Point();
        p_FirstVertex = new Point(p_Start);
    }
    else
    {
        // not the first vertex
        p_Start = new Point(p_End);
        p_End = arg0.getPoint();
    }

    // adds the vertex
    b_Drawing = true;
    addAPoint();
    b_Drawing = false;
    b_repaintFlag = true;
    // repaints
    this.repaint();
}

/**
 * handles mouse released event
 */
public void mouseReleased(MouseEvent arg0)
{
    // if filling we shouldn't add a vertex.
    if(b_Filling == true)
        return;

    if(b_FirstVertexInPolygon == true && isDragging == false)
    {
        // unlocks first vertex state
        b_FirstVertexInPolygon = false;
        return;
    }

    // save previous vertex and add current if mouse is dragged 
    // and more than non vertices
    if(isDragging == true && i_ThreeVertices != 0)
    {
        p_Start = new Point(p_End);
        p_End = arg0.getPoint();

        isDragging = false;
        b_Drawing = true;
        // adds a vertex
        addAPoint();
        b_Drawing = false;
    }
    else
        p_End = arg0.getPoint();
    b_repaintFlag = true;
    // repaint
    this.repaint();
}

/**
 * handles mouse dragged event
 */
public void mouseDragged(MouseEvent arg0)
{
    // repaints if not filling
    if(b_Filling == true)
        return;
    p_End = arg0.getPoint();
    isDragging = true;
    b_repaintFlag = true;
    this.repaint();
}

/**
 * handles mouse moved event
 */
public void mouseMoved(MouseEvent arg0)
{
    // if not filling, and we have at least one vertex
    // and not dragging mouse, then repaint.
    if(b_Filling == true)
        return;
    if(i_ThreeVertices == 0)
        return;
    if(isDragging == true)
        return;
    p_End = arg0.getPoint();
    b_repaintFlag = true;
    this.repaint();
}

public void mouseEntered(MouseEvent arg0){
}

public void mouseExited(MouseEvent arg0){
}