tricky+frustrating: changing mouse handler while mouse pressed doesn't work

Discussion in 'Java' started by Claus Atzenbeck, Nov 25, 2004.

  1. Hello,

    I have a quite tricky problem, on that I work on for 3 days now without
    success. It's frustrating. *sigh* :-(

    I am using Piccolo, a zoomable UI framework
    <http://www.cs.umd.edu/hcil/jazz/download/>, to create a 2D space on which
    I can place different kinds of nodes, e.g., pictures. However, my problem
    is more general and not Piccolo based.

    Every node on this 2D space has its own mouse event handler attached. With
    the mouse event handler you can click on a node and drag it to another
    place.

    Every node has internally its unique index ID. If two nodes intersect, then
    the one with a higher index is displayed above a node with lower index.

    Currently, you can drag a node anywhere, even underneath another node with
    higher index.

    WHAT I WANT:
    Whenever a node of higher index comes between the dragged node and the
    cursor, I want to release the original node and apply all mouse events to
    the higher placed node.

    If a user would do this manually, he/she would release the mouse button and
    click again. However, the user is not doing this, but the machine should;
    the user constantly just holds the mouse button pressed.

    This turns out to be very difficult to handle, because the user does *not*
    release the mouse button. The input handler of the node directly underneath
    the cursor (that *should* handle the mouse events then), does not get
    activated. :-(

    Any expert here who can see how to solve this problem?

    Here is the code. Don't worry about the Piccolo stuff. I think it is a
    general Java question about mouse events and robots (I use java.awt.Robot
    -- is this the right approach?). I have marked the problematic part:

    ******** <code> *******
    public void mouseDragged(PInputEvent aEvent) {
    // This tells other handlers that this event is now already handled now
    aEvent.setHandled(true);

    // Find the cursor position and the node position.
    Point2D cursorPos = aEvent.getPosition();
    PBounds nodePosition = aEvent.getPickedNode().getGlobalFullBounds();

    // Check if the cursor is within the node's bounds. This is
    // just for cases when the dragged node is slower than the cursor.
    if (nodePosition.contains(cursorPos.getX(), cursorPos.getY()) {

    // This retrieves the node directly underneath the cursor
    PNode nodeUnderneathCursor = aEvent.getPickedNode().getRoot()
    .getDefaultInputManager().getMouseOver()
    .getPickedNode();

    // If there is a node underneath the cursor + its not the background
    if (! nodeUnderneathCursor.equals(aEvent.getPickedNode())
    && ! nodeUnderneathCursor.getClass().equals(PCamera.class)) {

    // HERE IS THE PROBLEMATIC PART!! <<<<<<<<<<<<<<<<<<<<<<<<<<<<
    // Have in mind that there constantly comes a mouseDragged event
    // in from the user's pressed mouse button!
    try {
    Robot robot = new Robot();
    // this should release the mouse button for the original node
    robot.mouseRelease(InputEvent.BUTTON1_MASK);

    // Tell other handlers that this one doesn't want to handle anymore
    aEvent.setHandled(false);

    // this should perform a click; the position is right on the node
    // that is between the cursor and the original dragged node
    robot.mousePress(InputEvent.BUTTON1_MASK);
    } catch (AWTException e) {
    e.printStackTrace();
    }
    }
    }
    // Move the node to the new cursor position
    Dimension2D delta = aEvent.getDeltaRelativeTo(aEvent.getPickedNode());
    aEvent.getPickedNode().translate(delta.getWidth(), delta.getHeight());
    }
    ******* </code> *******

    Thanks a lot for any hint!
    Claus
    Claus Atzenbeck, Nov 25, 2004
    #1
    1. Advertising

  2. Hi,

    to summarize my previous mail:

    How can I completely ignore incomming mouse events (basically mouseDragged)
    from the "real" mouse in order to call:

    java.awt.Robot robot = new robot();
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    robot.mousePress(InputEvent.BUTTON1_MASK);

    Any hint is appreciated!

    Claus
    Claus Atzenbeck, Nov 26, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Child
    Replies:
    4
    Views:
    411
    Hans Kesting
    Apr 29, 2005
  2. Rune
    Replies:
    6
    Views:
    1,250
    Richie Hindle
    Jan 29, 2004
  3. Replies:
    9
    Views:
    530
    CBFalconer
    Apr 25, 2006
  4. mihai
    Replies:
    6
    Views:
    141
    John Joyce
    Jul 5, 2007
  5. Jeroni Paul

    Mouse movement while a button is pressed

    Jeroni Paul, Sep 4, 2003, in forum: Javascript
    Replies:
    2
    Views:
    114
    Jeroni Paul
    Sep 5, 2003
Loading...

Share This Page