Java Geometry Algorithm drag(Robot robot, Point startPoint, Point endPoint, int button)

Here you can find the source of drag(Robot robot, Point startPoint, Point endPoint, int button)

Description

Drags from one point to another with the specified mouse button pressed.

License

Open Source License

Parameter

Parameter Description
robot a robot to use for moving the mouse, etc.
startPoint a start point of the drag
endPoint an end point of the drag
button one of InputEvent.BUTTON1_MASK , InputEvent.BUTTON2_MASK , InputEvent.BUTTON3_MASK

Exception

Parameter Description
IllegalArgumentException if button is not one of InputEvent.BUTTON1_MASK, InputEvent.BUTTON2_MASK, InputEvent.BUTTON3_MASK

Declaration

public static void drag(Robot robot, Point startPoint, Point endPoint, int button) 

Method Source Code

//package com.java2s;
/*//  ww w.j av a2 s . c  o  m
 * Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.awt.Point;

import java.awt.Robot;

import java.awt.event.InputEvent;

public class Main {
    /**
     * Drags from one point to another with the specified mouse button pressed.
     *
     * @param robot a robot to use for moving the mouse, etc.
     * @param startPoint a start point of the drag
     * @param endPoint an end point of the drag
     * @param button one of {@code InputEvent.BUTTON1_MASK},
     *     {@code InputEvent.BUTTON2_MASK}, {@code InputEvent.BUTTON3_MASK}
     *
     * @throws IllegalArgumentException if {@code button} is not one of
     *     {@code InputEvent.BUTTON1_MASK}, {@code InputEvent.BUTTON2_MASK},
     *     {@code InputEvent.BUTTON3_MASK}
     */
    public static void drag(Robot robot, Point startPoint, Point endPoint, int button) {
        if (!(button == InputEvent.BUTTON1_MASK || button == InputEvent.BUTTON2_MASK
                || button == InputEvent.BUTTON3_MASK)) {
            throw new IllegalArgumentException("invalid mouse button");
        }

        robot.mouseMove(startPoint.x, startPoint.y);
        robot.mousePress(button);
        try {
            mouseMove(robot, startPoint, endPoint);
        } finally {
            robot.mouseRelease(button);
        }
    }

    /**
     * Moves the mouse pointer from one point to another.
     * Uses Bresenham's algorithm.
     *
     * @param robot a robot to use for moving the mouse
     * @param startPoint a start point of the drag
     * @param endPoint an end point of the drag
     */
    public static void mouseMove(Robot robot, Point startPoint, Point endPoint) {
        int dx = endPoint.x - startPoint.x;
        int dy = endPoint.y - startPoint.y;

        int ax = Math.abs(dx) * 2;
        int ay = Math.abs(dy) * 2;

        int sx = signWOZero(dx);
        int sy = signWOZero(dy);

        int x = startPoint.x;
        int y = startPoint.y;

        int d = 0;

        if (ax > ay) {
            d = ay - ax / 2;
            while (true) {
                robot.mouseMove(x, y);
                robot.delay(50);

                if (x == endPoint.x) {
                    return;
                }
                if (d >= 0) {
                    y = y + sy;
                    d = d - ax;
                }
                x = x + sx;
                d = d + ay;
            }
        } else {
            d = ax - ay / 2;
            while (true) {
                robot.mouseMove(x, y);
                robot.delay(50);

                if (y == endPoint.y) {
                    return;
                }
                if (d >= 0) {
                    x = x + sx;
                    d = d - ay;
                }
                y = y + sy;
                d = d + ax;
            }
        }
    }

    private static int signWOZero(int i) {
        return (i > 0) ? 1 : -1;
    }
}

Related

  1. directVincenty(Point2D.Double point, double brng, double distance)
  2. dot(@Nonnull Point2D pA, @Nonnull Point2D pB)
  3. dot(final Point2D a, final Point2D b)
  4. dot(Point a, Point b, Point c)
  5. dotNorm(final Point2D a, final Point2D b)
  6. dx(Point a, Point b)
  7. edge(Collection points)
  8. extendLine(Point2D p0, Point2D p1, double toLength)
  9. findArea(Point2D p1, Point2D p2, Point2D p3)