Example usage for java.awt.event MouseWheelEvent getScrollAmount

List of usage examples for java.awt.event MouseWheelEvent getScrollAmount

Introduction

In this page you can find the example usage for java.awt.event MouseWheelEvent getScrollAmount.

Prototype

public int getScrollAmount() 

Source Link

Document

Returns the number of units that should be scrolled per click of mouse wheel rotation.

Usage

From source file:Main.java

public static MouseEvent convertMouseEvent(Component source, MouseEvent sourceEvent, Component destination) {
    Point p = SwingUtilities.convertPoint(source, new Point(sourceEvent.getX(), sourceEvent.getY()),
            destination);//from w  w w .jav  a2  s  .co  m
    Component newSource;

    if (destination != null)
        newSource = destination;
    else
        newSource = source;

    MouseEvent newEvent;
    if (sourceEvent instanceof MouseWheelEvent) {
        MouseWheelEvent sourceWheelEvent = (MouseWheelEvent) sourceEvent;
        newEvent = new MouseWheelEvent(newSource, sourceWheelEvent.getID(), sourceWheelEvent.getWhen(),
                sourceEvent.getModifiers() | sourceEvent.getModifiersEx(), p.x, p.y,
                sourceWheelEvent.getClickCount(), sourceWheelEvent.isPopupTrigger(),
                sourceWheelEvent.getScrollType(), sourceWheelEvent.getScrollAmount(),
                sourceWheelEvent.getWheelRotation());
    } else if (sourceEvent instanceof MenuDragMouseEvent) {
        MenuDragMouseEvent sourceMenuDragEvent = (MenuDragMouseEvent) sourceEvent;
        newEvent = new MenuDragMouseEvent(newSource, sourceMenuDragEvent.getID(), sourceMenuDragEvent.getWhen(),
                sourceEvent.getModifiers() | sourceEvent.getModifiersEx(), p.x, p.y,
                sourceMenuDragEvent.getClickCount(), sourceMenuDragEvent.isPopupTrigger(),
                sourceMenuDragEvent.getPath(), sourceMenuDragEvent.getMenuSelectionManager());
    } else {
        newEvent = new MouseEvent(newSource, sourceEvent.getID(), sourceEvent.getWhen(),
                sourceEvent.getModifiers() | sourceEvent.getModifiersEx(), p.x, p.y,
                sourceEvent.getClickCount(), sourceEvent.isPopupTrigger(), sourceEvent.getButton());
    }
    return newEvent;
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(200, 200);//from  w  w  w .  java2s.co  m
    JTextArea textArea = new JTextArea();
    textArea.addMouseWheelListener(new MouseWheelListener() {
        public void mouseWheelMoved(MouseWheelEvent e) {
            if (e.getWheelRotation() < 0) {
                System.out.println("Up... " + e.getWheelRotation());
            } else {
                System.out.println("Down... " + e.getWheelRotation());
            }
            System.out.println("ScrollAmount: " + e.getScrollAmount());

            if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
                System.out.println("MouseWheelEvent.WHEEL_UNIT_SCROLL");
            }

            if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {
                System.out.println("MouseWheelEvent.WHEEL_BLOCK_SCROLL");
            }
        }
    });

    getContentPane().add(textArea);
}

From source file:Main.java

public void mouseWheelMoved(MouseWheelEvent e) {
    String message;//  www. j av  a  2  s .c o m
    int notches = e.getWheelRotation();
    if (notches < 0) {
        message = "Mouse wheel moved UP " + -notches + " notch(es)" + newline;
    } else {
        message = "Mouse wheel moved DOWN " + notches + " notch(es)" + newline;
    }
    if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
        message += "    Scroll type: WHEEL_UNIT_SCROLL" + newline;
        message += "    Scroll amount: " + e.getScrollAmount() + " unit increments per notch" + newline;
        message += "    Units to scroll: " + e.getUnitsToScroll() + " unit increments" + newline;
        message += "    Vertical unit increment: " + scrollPane.getVerticalScrollBar().getUnitIncrement(1)
                + " pixels" + newline;
    } else { // scroll type == MouseWheelEvent.WHEEL_BLOCK_SCROLL
        message += "    Scroll type: WHEEL_BLOCK_SCROLL" + newline;
        message += "    Vertical block increment: " + scrollPane.getVerticalScrollBar().getBlockIncrement(1)
                + " pixels" + newline;
    }
    saySomething(message, e);
}

From source file:MouseWheelEventDemo.java

public void mouseWheelMoved(MouseWheelEvent e) {
    String message;/*from  www.  j  a  v  a 2s.  c om*/
    int notches = e.getWheelRotation();
    if (notches < 0) {
        message = "Mouse wheel moved UP " + -notches + " notch(es)" + newline;
    } else {
        message = "Mouse wheel moved DOWN " + notches + " notch(es)" + newline;
    }
    if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
        message += "    Scroll type: WHEEL_UNIT_SCROLL" + newline;
        message += "    Scroll amount: " + e.getScrollAmount() + " unit increments per notch" + newline;
        message += "    Units to scroll: " + e.getUnitsToScroll() + " unit increments" + newline;
        message += "    Vertical unit increment: " + scrollPane.getVerticalScrollBar().getUnitIncrement(1)
                + " pixels" + newline;
    } else { //scroll type == MouseWheelEvent.WHEEL_BLOCK_SCROLL
        message += "    Scroll type: WHEEL_BLOCK_SCROLL" + newline;
        message += "    Vertical block increment: " + scrollPane.getVerticalScrollBar().getBlockIncrement(1)
                + " pixels" + newline;
    }
    saySomething(message, e);
}

From source file:events.MouseWheelEventDemo.java

public void mouseWheelMoved(MouseWheelEvent e) {
    String message;//w  ww  .j av  a 2  s  .  c o  m
    int notches = e.getWheelRotation();
    if (notches < 0) {
        message = "Mouse wheel moved UP " + -notches + " notch(es)" + NEWLINE;
    } else {
        message = "Mouse wheel moved DOWN " + notches + " notch(es)" + NEWLINE;
    }
    if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
        message += "    Scroll type: WHEEL_UNIT_SCROLL" + NEWLINE;
        message += "    Scroll amount: " + e.getScrollAmount() + " unit increments per notch" + NEWLINE;
        message += "    Units to scroll: " + e.getUnitsToScroll() + " unit increments" + NEWLINE;
        message += "    Vertical unit increment: " + scrollPane.getVerticalScrollBar().getUnitIncrement(1)
                + " pixels" + NEWLINE;
    } else { //scroll type == MouseWheelEvent.WHEEL_BLOCK_SCROLL
        message += "    Scroll type: WHEEL_BLOCK_SCROLL" + NEWLINE;
        message += "    Vertical block increment: " + scrollPane.getVerticalScrollBar().getBlockIncrement(1)
                + " pixels" + NEWLINE;
    }
    eventOutput(message, e);
}

From source file:corelyzer.ui.CorelyzerGLCanvas.java

private void scrollSceneWithScrollWheel(final MouseWheelEvent e) {
    int scrollAmount = e.getScrollAmount();
    int wheelRotation = e.getWheelRotation();
    int ample = 3;

    Point currentPos = e.getPoint();
    int dY = ample * scrollAmount * wheelRotation;

    float sy;// ww w.  ja v  a2s  .co  m
    float h;

    canvasLock.lock();
    {
        h = SceneGraph.getCanvasHeight(canvasId);

        sy = h / canvas.getHeight();

        SceneGraph.panScene(0, dY * sy);

        this.convertMousePointToSceneSpace(currentPos, scenePos);
        SceneGraph.positionMouse(scenePos[0], scenePos[1]);
    }
    canvasLock.unlock();
    prePos = currentPos;
}

From source file:corelyzer.ui.CorelyzerGLCanvas.java

private void zoomSceneWithScrollWheel(final MouseWheelEvent e) {
    // FIXME MouseWheelEvent info
    /*//from   w  w w.jav  a  2  s.co  m
     * System.out.println(e);
     * 
     * String mesg = "Component: " + e.getComponent(); mesg += ", id: " +
     * e.getID(); mesg += ", when: " + e.getWhen(); mesg += ", modifiers: "
     * + e.getModifiers(); mesg += ", x: " + e.getX(); mesg += ", y: " +
     * e.getY(); mesg += ", clickCount: " + e.getClickCount(); mesg +=
     * ", scrollType: " + e.getScrollType(); mesg += ", scrollAmount: " +
     * e.getScrollAmount(); mesg += ", wheelRot: " + e.getWheelRotation();
     * 
     * System.out.println(mesg);
     */
    // --

    int scrollAmount = e.getScrollAmount();
    int wheelRotation = e.getWheelRotation();
    Point mousePos = e.getPoint();

    /*
     * // int scrollType = e.getScrollType(); // int scrollUnit =
     * e.getUnitsToScroll();
     * 
     * System.out.println("---- ScrollAmount:   " + scrollAmount);
     * System.out.println("---- ScrollType:     " + scrollType);
     * System.out.println("---- ScrollUnit:     " + scrollUnit);
     * System.out.println("---- WheelRotation:  " + wheelRotation);
     * System.out.println("---- Mouse Location: " + mousePos);
     */
    // determine what point to zoom in or out on

    float cp[] = { 0.0f, 0.0f }; // canvas position x,y
    float sc[] = { 0.0f, 0.0f }; // scene center x,y
    float s; // scale factor
    // float w; // canvas width

    canvasLock.lock();
    {
        this.convertMousePointToSceneSpace(mousePos, cp);

        sc[0] = SceneGraph.getSceneCenterX();
        sc[1] = SceneGraph.getSceneCenterY();

        /*
         * System.out.println("Mouse Position Translated to " + cp[0] + ", "
         * + cp[1]);
         * 
         * System.out.println("Difference from center " + (cp[0] - sc[0]) +
         * ", " + (cp[1] - sc[1]));
         */

        // each amount is approximately 5%

        if (wheelRotation <= 0) {
            s = (float) Math.pow(.95, scrollAmount);
        } else {
            s = (float) Math.pow(1.05f, scrollAmount);
        }

        // System.out.println("Scale Factor " + s);

        SceneGraph.scaleScene(s);

        // mouse in same place in screen, used to determine pan amount
        float ncp[] = { 0.0f, 0.0f };

        this.convertMousePointToSceneSpace(mousePos, ncp);

        // System.out.println("New Pos of mouse point after scale " + ncp[0]
        // +
        // ", " + ncp[1]);

        ncp[0] = ncp[0] - cp[0];
        ncp[1] = ncp[1] - cp[1];

        // System.out.println("Difference of " + ncp[0] + ", " + ncp[1]);
        SceneGraph.panScene(-ncp[0], -ncp[1]);
    }
    canvasLock.unlock();
}