Here you can find the source of animateMouse(Robot robot, JComponent jc, boolean moveMouse, boolean clickOnArrival)
Parameter | Description |
---|---|
robot | the robot used to move the mouse. |
jc | the component to move the mouse to |
moveMouse | whether to animate the mouse movement |
clickOnArrival | whether to click the mouse once it arrives at its destination |
protected static void animateMouse(Robot robot, JComponent jc, boolean moveMouse, boolean clickOnArrival)
//package com.java2s; /*/*from www.j a va 2 s.c om*/ * @(#)BlogHelper.java * * $Date: 2014-11-27 07:50:51 +0100 (Do, 27 Nov 2014) $ * * Copyright (c) 2014 by Jeremy Wood. * All rights reserved. * * The copyright of this software is owned by Jeremy Wood. * You may not use, copy or modify this software, except in * accordance with the license agreement you entered into with * Jeremy Wood. For details see accompanying license terms. * * This software is probably, but not necessarily, discussed here: * https://javagraphics.java.net/ * * That site should also contain the most recent official version * of this software. (See the SVN repository for more details.) */ import java.awt.MouseInfo; import java.awt.Point; import java.awt.PointerInfo; import java.awt.Robot; import java.awt.event.InputEvent; import javax.swing.JComponent; import javax.swing.SwingUtilities; public class Main { /** Move the mouse to a point provided. * * @param robot the robot used to move the mouse. * @param jc the component to move the mouse to * @param p the point (relative to the JComponent) to move the mouse to. * @param moveMouse whether to animate the mouse movement * @param clickCountOnArrival how many clicks to simulate on arrival */ protected static void animateMouse(Robot robot, JComponent jc, Point p, boolean moveMouse, int clickCountOnArrival) { Point copy = new Point(p.x, p.y); SwingUtilities.convertPointToScreen(copy, jc); if (moveMouse) { PointerInfo pi = MouseInfo.getPointerInfo(); Point currentLoc = pi.getLocation(); double ax = copy.x - currentLoc.x; double bx = currentLoc.x; double ay = copy.y - currentLoc.y; double by = currentLoc.y; for (double t = 0; t < 1; t += .05) { currentLoc.x = (int) (ax * t + bx); currentLoc.y = (int) (ay * t + by); robot.mouseMove(currentLoc.x, currentLoc.y); beat(40 * 4); } } robot.mouseMove(copy.x, copy.y); beat(40 * 4); if (clickCountOnArrival > 0) { while (clickCountOnArrival > 0) { robot.mousePress(InputEvent.BUTTON1_MASK); beat(20); robot.mouseRelease(InputEvent.BUTTON1_MASK); clickCountOnArrival--; } beat(200 * 4); } } /** Move the mouse to the center of the component provided. * * @param robot the robot used to move the mouse. * @param jc the component to move the mouse to * @param moveMouse whether to animate the mouse movement * @param clickOnArrival whether to click the mouse once it arrives at its destination */ protected static void animateMouse(Robot robot, JComponent jc, boolean moveMouse, boolean clickOnArrival) { Point p = new Point(jc.getWidth() / 2, jc.getHeight() / 2); animateMouse(robot, jc, p, moveMouse, clickOnArrival ? 1 : 0); } /** Pause for a certain number of milliseconds. * This is used in creating screen capture animations. * @param ms the milliseconds to wait. */ protected static void beat(int ms) { try { Thread.sleep(ms); } catch (Exception e) { } } }