Example usage for java.awt.event MouseEvent MOUSE_PRESSED

List of usage examples for java.awt.event MouseEvent MOUSE_PRESSED

Introduction

In this page you can find the example usage for java.awt.event MouseEvent MOUSE_PRESSED.

Prototype

int MOUSE_PRESSED

To view the source code for java.awt.event MouseEvent MOUSE_PRESSED.

Click Source Link

Document

The "mouse pressed" event.

Usage

From source file:ExText.java

/**
 * Responds to a button1 event (press, release, or drag). On a press, the
 * method adds a wakeup criterion to the behavior's set, callling for the
 * behavior to be awoken on each frame. On a button prelease, this criterion
 * is removed from the set.// ww  w. j a  va  2  s .c o  m
 * 
 * @param mouseEvent
 *            the MouseEvent to respond to
 */
public void onButton1(MouseEvent mev) {
    if (subjectTransformGroup == null)
        return;

    int x = mev.getX();
    int y = mev.getY();

    if (mev.getID() == MouseEvent.MOUSE_PRESSED) {
        // Mouse button pressed: record position and change
        // the wakeup criterion to include elapsed time wakeups
        // so we can animate.
        previousX = x;
        previousY = y;
        initialX = x;
        initialY = y;

        // Swap criterion... parent class will not reschedule us
        mouseCriterion = mouseAndAnimationCriterion;

        // Change to a "move" cursor
        if (parentComponent != null) {
            savedCursor = parentComponent.getCursor();
            parentComponent.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        }
        return;
    }
    if (mev.getID() == MouseEvent.MOUSE_RELEASED) {
        // Mouse button released: restore original wakeup
        // criterion which only includes mouse activity, not
        // elapsed time
        mouseCriterion = savedMouseCriterion;

        // Switch the cursor back
        if (parentComponent != null)
            parentComponent.setCursor(savedCursor);
        return;
    }

    previousX = x;
    previousY = y;
}

From source file:ExText.java

/**
 * Responds to a button2 event (press, release, or drag). On a press, the
 * method records the initial cursor location. On a drag, the difference
 * between the current and previous cursor location provides a delta that
 * controls the amount by which to rotate in X and Y.
 * //from w  w  w  .j ava  2 s . c  om
 * @param mouseEvent
 *            the MouseEvent to respond to
 */
public void onButton2(MouseEvent mev) {
    if (subjectTransformGroup == null)
        return;

    int x = mev.getX();
    int y = mev.getY();

    if (mev.getID() == MouseEvent.MOUSE_PRESSED) {
        // Mouse button pressed: record position
        previousX = x;
        previousY = y;
        initialX = x;
        initialY = y;

        // Change to a "rotate" cursor
        if (parentComponent != null) {
            savedCursor = parentComponent.getCursor();
            parentComponent.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
        }
        return;
    }
    if (mev.getID() == MouseEvent.MOUSE_RELEASED) {
        // Mouse button released: do nothing

        // Switch the cursor back
        if (parentComponent != null)
            parentComponent.setCursor(savedCursor);
        return;
    }

    //
    // Mouse moved while button down: create a rotation
    //
    // Compute the delta in X and Y from the previous
    // position. Use the delta to compute rotation
    // angles with the mapping:
    //
    //   positive X mouse delta --> negative Y-axis rotation
    //   positive Y mouse delta --> negative X-axis rotation
    //
    // where positive X mouse movement is to the right, and
    // positive Y mouse movement is **down** the screen.
    //
    int deltaX = x - previousX;
    int deltaY = 0;

    if (Math.abs(y - initialY) > DELTAY_DEADZONE) {
        // Cursor has moved far enough vertically to consider
        // it intentional, so get it's delta.
        deltaY = y - previousY;
    }

    if (deltaX > UNUSUAL_XDELTA || deltaX < -UNUSUAL_XDELTA || deltaY > UNUSUAL_YDELTA
            || deltaY < -UNUSUAL_YDELTA) {
        // Deltas are too huge to be believable. Probably a glitch.
        // Don't record the new XY location, or do anything.
        return;
    }

    double xRotationAngle = -deltaY * XRotationFactor;
    double yRotationAngle = -deltaX * YRotationFactor;

    //
    // Build transforms
    //
    transform1.rotX(xRotationAngle);
    transform2.rotY(yRotationAngle);

    // Get and save the current transform matrix
    subjectTransformGroup.getTransform(currentTransform);
    currentTransform.get(matrix);
    translate.set(matrix.m03, matrix.m13, matrix.m23);

    // Translate to the origin, rotate, then translate back
    currentTransform.setTranslation(origin);
    currentTransform.mul(transform2, currentTransform);
    currentTransform.mul(transform1);
    currentTransform.setTranslation(translate);

    // Update the transform group
    subjectTransformGroup.setTransform(currentTransform);

    previousX = x;
    previousY = y;
}

From source file:ExText.java

/**
 * Responds to a button3 event (press, release, or drag). On a drag, the
 * difference between the current and previous cursor location provides a
 * delta that controls the amount by which to translate in X and Y.
 * // ww  w  .j  a  v a 2 s  . c  om
 * @param mouseEvent
 *            the MouseEvent to respond to
 */
public void onButton3(MouseEvent mev) {
    if (subjectTransformGroup == null)
        return;

    int x = mev.getX();
    int y = mev.getY();

    if (mev.getID() == MouseEvent.MOUSE_PRESSED) {
        // Mouse button pressed: record position
        previousX = x;
        previousY = y;

        // Change to a "move" cursor
        if (parentComponent != null) {
            savedCursor = parentComponent.getCursor();
            parentComponent.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
        }
        return;
    }
    if (mev.getID() == MouseEvent.MOUSE_RELEASED) {
        // Mouse button released: do nothing

        // Switch the cursor back
        if (parentComponent != null)
            parentComponent.setCursor(savedCursor);
        return;
    }

    //
    // Mouse moved while button down: create a translation
    //
    // Compute the delta in X and Y from the previous
    // position. Use the delta to compute translation
    // distances with the mapping:
    //
    //   positive X mouse delta --> positive X-axis translation
    //   positive Y mouse delta --> negative Y-axis translation
    //
    // where positive X mouse movement is to the right, and
    // positive Y mouse movement is **down** the screen.
    //
    int deltaX = x - previousX;
    int deltaY = y - previousY;

    if (deltaX > UNUSUAL_XDELTA || deltaX < -UNUSUAL_XDELTA || deltaY > UNUSUAL_YDELTA
            || deltaY < -UNUSUAL_YDELTA) {
        // Deltas are too huge to be believable. Probably a glitch.
        // Don't record the new XY location, or do anything.
        return;
    }

    double xTranslationDistance = deltaX * XTranslationFactor;
    double yTranslationDistance = -deltaY * YTranslationFactor;

    //
    // Build transforms
    //
    translate.set(xTranslationDistance, yTranslationDistance, 0.0);
    transform1.set(translate);

    // Get and save the current transform
    subjectTransformGroup.getTransform(currentTransform);

    // Translate as needed
    currentTransform.mul(transform1);

    // Update the transform group
    subjectTransformGroup.setTransform(currentTransform);

    previousX = x;
    previousY = y;
}

From source file:com.projity.contrib.calendar.JXXMonthView.java

/**
 * {@inheritDoc}//  w w  w.j  av  a 2  s  .  co  m
 */
protected void processMouseEvent(MouseEvent e) {
    if (e.getID() == MouseEvent.MOUSE_PRESSED) {
        selectFromEvent(e);
    } else if (e.getID() == MouseEvent.MOUSE_RELEASED) {
        if (_asKirkWouldSay_FIRE) {
            fireActionPerformed();
        }
        _asKirkWouldSay_FIRE = false;
    }
    super.processMouseEvent(e);
}

From source file:ExText.java

/**
 * Initialize the behavior.// w ww  . j  a  va  2 s  .  co  m
 */
public void initialize() {
    // Wakeup when the mouse is dragged or when a mouse button
    // is pressed or released.
    mouseEvents = new WakeupCriterion[3];
    mouseEvents[0] = new WakeupOnAWTEvent(MouseEvent.MOUSE_DRAGGED);
    mouseEvents[1] = new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED);
    mouseEvents[2] = new WakeupOnAWTEvent(MouseEvent.MOUSE_RELEASED);
    mouseCriterion = new WakeupOr(mouseEvents);
    wakeupOn(mouseCriterion);
}

From source file:ExText.java

/**
 * Process a new wakeup. Interpret mouse button presses, releases, and mouse
 * drags.//from w w w . jav  a 2 s. c om
 * 
 * @param criteria
 *            The wakeup criteria causing the behavior wakeup.
 */
public void processStimulus(Enumeration criteria) {
    WakeupCriterion wakeup = null;
    AWTEvent[] event = null;
    int whichButton = BUTTONNONE;

    // Process all pending wakeups
    while (criteria.hasMoreElements()) {
        wakeup = (WakeupCriterion) criteria.nextElement();
        if (wakeup instanceof WakeupOnAWTEvent) {
            event = ((WakeupOnAWTEvent) wakeup).getAWTEvent();

            // Process all pending events
            for (int i = 0; i < event.length; i++) {
                if (event[i].getID() != MouseEvent.MOUSE_PRESSED
                        && event[i].getID() != MouseEvent.MOUSE_RELEASED
                        && event[i].getID() != MouseEvent.MOUSE_DRAGGED)
                    // Ignore uninteresting mouse events
                    continue;

                //
                // Regretably, Java event handling (or perhaps
                // underlying OS event handling) doesn't always
                // catch button bounces (redundant presses and
                // releases), or order events so that the last
                // drag event is delivered before a release.
                // This means we can get stray events that we
                // filter out here.
                //
                if (event[i].getID() == MouseEvent.MOUSE_PRESSED && buttonPressed != BUTTONNONE)
                    // Ignore additional button presses until a release
                    continue;

                if (event[i].getID() == MouseEvent.MOUSE_RELEASED && buttonPressed == BUTTONNONE)
                    // Ignore additional button releases until a press
                    continue;

                if (event[i].getID() == MouseEvent.MOUSE_DRAGGED && buttonPressed == BUTTONNONE)
                    // Ignore drags until a press
                    continue;

                MouseEvent mev = (MouseEvent) event[i];
                int modifiers = mev.getModifiers();

                //
                // Unfortunately, the underlying event handling
                // doesn't do a "grab" operation when a mouse button
                // is pressed. This means that once a button is
                // pressed, if another mouse button or a keyboard
                // modifier key is pressed, the delivered mouse event
                // will show that a different button is being held
                // down. For instance:
                //
                // Action Event
                //  Button 1 press Button 1 press
                //  Drag with button 1 down Button 1 drag
                //  ALT press -
                //  Drag with ALT & button 1 down Button 2 drag
                //  Button 1 release Button 2 release
                //
                // The upshot is that we can get a button press
                // without a matching release, and the button
                // associated with a drag can change mid-drag.
                //
                // To fix this, we watch for an initial button
                // press, and thenceforth consider that button
                // to be the one held down, even if additional
                // buttons get pressed, and despite what is
                // reported in the event. Only when a button is
                // released, do we end such a grab.
                //

                if (buttonPressed == BUTTONNONE) {
                    // No button is pressed yet, figure out which
                    // button is down now and how to direct events
                    if (((modifiers & InputEvent.BUTTON3_MASK) != 0)
                            || (((modifiers & InputEvent.BUTTON1_MASK) != 0)
                                    && ((modifiers & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK))) {
                        // Button 3 activity (META or CTRL down)
                        whichButton = BUTTON3;
                    } else if ((modifiers & InputEvent.BUTTON2_MASK) != 0) {
                        // Button 2 activity (ALT down)
                        whichButton = BUTTON2;
                    } else {
                        // Button 1 activity (no modifiers down)
                        whichButton = BUTTON1;
                    }

                    // If the event is to press a button, then
                    // record that that button is now down
                    if (event[i].getID() == MouseEvent.MOUSE_PRESSED)
                        buttonPressed = whichButton;
                } else {
                    // Otherwise a button was pressed earlier and
                    // hasn't been released yet. Assign all further
                    // events to it, even if ALT, META, CTRL, or
                    // another button has been pressed as well.
                    whichButton = buttonPressed;
                }

                // Distribute the event
                switch (whichButton) {
                case BUTTON1:
                    onButton1(mev);
                    break;
                case BUTTON2:
                    onButton2(mev);
                    break;
                case BUTTON3:
                    onButton3(mev);
                    break;
                default:
                    break;
                }

                // If the event is to release a button, then
                // record that that button is now up
                if (event[i].getID() == MouseEvent.MOUSE_RELEASED)
                    buttonPressed = BUTTONNONE;
            }
            continue;
        }

        if (wakeup instanceof WakeupOnElapsedFrames) {
            onElapsedFrames((WakeupOnElapsedFrames) wakeup);
            continue;
        }
    }

    // Reschedule us for another wakeup
    wakeupOn(mouseCriterion);
}

From source file:FourByFour.java

public void initialize() {
    x = 0;// www .ja  v  a2  s  .com
    y = 0;
    x_last = 0;
    y_last = 0;
    x_angle = 0;
    y_angle = 0;
    x_factor = .02;
    y_factor = .02;

    mouseEvents = new WakeupCriterion[2];
    mouseEvents[0] = new WakeupOnAWTEvent(MouseEvent.MOUSE_DRAGGED);
    mouseEvents[1] = new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED);
    mouseCriterion = new WakeupOr(mouseEvents);
    wakeupOn(mouseCriterion);
}

From source file:FourByFour.java

public void processStimulus(Enumeration criteria) {
    WakeupCriterion wakeup;//w  w w  .ja va2  s . com
    AWTEvent[] event;
    int id;
    int dx, dy;

    while (criteria.hasMoreElements()) {
        wakeup = (WakeupCriterion) criteria.nextElement();
        if (wakeup instanceof WakeupOnAWTEvent) {
            event = ((WakeupOnAWTEvent) wakeup).getAWTEvent();
            for (int i = 0; i < event.length; i++) {
                id = event[i].getID();
                if (id == MouseEvent.MOUSE_DRAGGED) {

                    x = ((MouseEvent) event[i]).getX();
                    y = ((MouseEvent) event[i]).getY();

                    dx = x - x_last;
                    dy = y - y_last;

                    x_angle = dy * y_factor;
                    y_angle = dx * x_factor;

                    transformX.rotX(x_angle);
                    transformY.rotY(y_angle);

                    modelTrans.mul(transformX, modelTrans);
                    modelTrans.mul(transformY, modelTrans);

                    transformGroup.setTransform(modelTrans);

                    x_last = x;
                    y_last = y;
                } else if (id == MouseEvent.MOUSE_PRESSED) {

                    x = x_last = ((MouseEvent) event[i]).getX();
                    y = y_last = ((MouseEvent) event[i]).getY();

                    Point3d eyePos = new Point3d();
                    canvas3D.getCenterEyeInImagePlate(eyePos);

                    Point3d mousePos = new Point3d();
                    canvas3D.getPixelLocationInImagePlate(x, y, mousePos);

                    Transform3D transform3D = new Transform3D();
                    canvas3D.getImagePlateToVworld(transform3D);

                    transform3D.transform(eyePos);
                    transform3D.transform(mousePos);

                    Vector3d mouseVec;
                    if (parallel) {
                        mouseVec = new Vector3d(0.f, 0.f, -1.f);
                    } else {
                        mouseVec = new Vector3d();
                        mouseVec.sub(mousePos, eyePos);
                        mouseVec.normalize();
                    }

                    pickRay.set(mousePos, mouseVec);
                    sceneGraphPath = branchGroup.pickAllSorted(pickRay);

                    if (sceneGraphPath != null) {
                        for (int j = 0; j < sceneGraphPath.length; j++) {
                            if (sceneGraphPath[j] != null) {
                                Node node = sceneGraphPath[j].getObject();
                                if (node instanceof Shape3D) {
                                    try {
                                        ID posID = (ID) node.getUserData();
                                        if (posID != null) {
                                            int pos = posID.get();
                                            positions.set(pos, Positions.HUMAN);
                                            canvas2D.repaint();
                                            break;
                                        }
                                    } catch (CapabilityNotSetException e) {
                                        // Catch all CapabilityNotSet
                                        // exceptions and
                                        // throw them away, prevents
                                        // renderer from
                                        // locking up when encountering
                                        // "non-selectable"
                                        // objects.
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    wakeupOn(mouseCriterion);
}

From source file:org.kalypso.mt.input.MTMouseInput.java

@Override
public void processAction() {
    if (invocCounter++ % 6 == 0 || eventQueue.isEmpty())
        return;//from   w w  w  . j a v  a 2s.c  o m

    // purge all events waiting if mouse is blocked
    if (m_mouseBlocks > 0) {
        eventQueue.clear();
        return;
    }

    // otherwise process events
    final long time = System.currentTimeMillis();
    while (!eventQueue.isEmpty()) {
        final long dist = time - eventQueue.peekFirst().timestamp;

        if (dist > 200) {
            // ok, process this one
            final MTMouseEventData e = eventQueue.pollFirst();
            switch (e.event.getID()) {
            case MouseEvent.MOUSE_PRESSED:
                this.mousePressed(e.event);
                break;
            case MouseEvent.MOUSE_RELEASED:
                this.mouseReleased(e.event);
                break;
            case MouseEvent.MOUSE_CLICKED:
                this.mouseClicked(e.event);
                break;
            case MouseEvent.MOUSE_DRAGGED:
                this.mouseDragged(e.event);
                break;
            case MouseEvent.MOUSE_MOVED:
                this.mouseMoved(e.event);
                break;
            }

        } else {
            break;
        }

    }

}

From source file:org.pmedv.blackboard.commands.CreateBoardCommand.java

private void handleMouseEvent(MouseEvent e, BoardEditor editor) {
    if (e.getID() == MouseEvent.MOUSE_PRESSED) {
        log.info(e);/*from  w  w  w. ja  v a2s. c  om*/
        TabWindow tw = DockingUtil.getTabWindowFor(editor.getView());
        advisor.setCurrentEditorArea(tw);
        EditorUtils.setToolbarButtonState(editor);
        ctx.getBean(ApplicationWindow.class).getRasterCombo().setSelectedItem(new Integer(editor.getRaster()));
        editor.updateStatusBar();
        editor.getView().requestFocus();
        editor.notifyListeners(EventType.EDITOR_CHANGED);
    }
}