鼠标自动移动

import java.awt.*;
import java.util.Random;


public class Mouse {
public static final int MILLISECOND = 1;
public static final int SECOND = 1000;
public static final int MINUTE = 60 * 1000;
public static final int HOUR = 60 * 60 * 1000;
public static final int DAY = 24 * 60 * 60 * 1000;

public static final String MILLISECOND_UNIT = "毫秒";
public static final String SECOND_UNIT = "秒";
public static final String MINUTE_UNIT = "分钟";
public static final String HOUR_UNIT = "小时";
public static final String DAY_UNIT = "天";

//屏幕分辨率
public static DisplayMode displayMode = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode();
public static int resolutionHeight = displayMode.getHeight();
public static int resolutionWidth = displayMode.getWidth();

/**
* @param time 总时间
* @param unitTime 单位时间
*/
public static void mouseMove(int time, int unitTime) {
try {
long startTime = System.currentTimeMillis();

if (time < unitTime) {
System.out.println("总时间不能小于单位时间!!!");
return;
}

String unit = checkUnit(unitTime);
System.out.printf("以时间单位为”%s“进行倒计时!!!%n", unit);

Robot robot = new Robot();
Random random = new Random();
int timeLeft = time;
int count = time % unitTime != 0 ? time / unitTime + 1 : time / unitTime;
for (int i = 0; i < count; i++) {
System.out.printf("鼠标停止移动倒计时还有%s!!!%n", getTimeLeft(timeLeft));

int x = random.nextInt(resolutionWidth);
int y = random.nextInt(resolutionHeight);
robot.setAutoDelay((int) unitTime);
robot.mouseMove(x, y);

timeLeft = timeLeft - unitTime;
if (timeLeft < unitTime) {
unitTime = timeLeft;
}
}
System.out.println("鼠标停止移动!!!");
System.out.printf("耗时:%s!!!", getTimeLeft((int) (System.currentTimeMillis() - startTime)));
} catch (AWTException e) {
e.printStackTrace();
}
}

/**
* @param time 总时间
* @param unitTime 单位时间
*/
public static void mouseSwipe(int time, int unitTime) {

try {
long startTime = System.currentTimeMillis();

if (time < unitTime) {
System.out.println("总时间不能小于单位时间!!!");
return;
}

int count = time % unitTime != 0 ? time / unitTime + 1 : time / unitTime;
System.out.printf("鼠标滑动次数共:%s", count);
int x = 0, y = 0, xFlag = 0, yFlag = 0, start = 0;
int timeLeft = time;
;
Robot robot = new Robot();
while (start++ < count) {

if (x < resolutionWidth && xFlag == 0) {
x++;
} else if (xFlag == 0) {
xFlag = 1;
} else {
x--;
if (x == 0) {
xFlag = 0;
}
}

if (y < resolutionHeight && yFlag == 0) {
y++;
} else if (yFlag == 0) {
yFlag = 1;
} else {
y--;
if (y == 0) {
yFlag = 0;
}
}

robot.setAutoDelay(unitTime);
robot.mouseMove(x, y);

timeLeft = timeLeft - unitTime;
if (timeLeft < unitTime) {
unitTime = timeLeft;
}

System.out.printf("滑动第%s次!!!%n", start);
}
System.out.printf("耗时:%s!!!", getTimeLeft((int) (System.currentTimeMillis() - startTime)));
} catch (AWTException e) {
e.printStackTrace();
}

}

public static String checkUnit(int unitTime) {
String unitStr;
switch (unitTime) {
case MILLISECOND:
unitStr = MILLISECOND_UNIT;
break;
case SECOND:
unitStr = SECOND_UNIT;
break;
case MINUTE:
unitStr = MINUTE_UNIT;
break;
case HOUR:
unitStr = HOUR_UNIT;
break;
case DAY:
unitStr = DAY_UNIT;
break;
default:
throw new RuntimeException("时间单位不正确!!!");
}
return unitStr;
}

public static String getTimeLeft(int time) {
StringBuilder builder = new StringBuilder();

if (time >= DAY) {
builder.append(time / DAY).append(DAY_UNIT);
time = time % DAY;
}

if (time >= HOUR) {
builder.append(time / HOUR).append(HOUR_UNIT);
time = time % HOUR;
}

if (time >= MINUTE) {
builder.append(time / MINUTE).append(MINUTE_UNIT);
time = time % MINUTE;
}

if (time >= SECOND) {
builder.append(time / SECOND).append(SECOND_UNIT);
time = time % SECOND;
}

if (time != 0) {
builder.append(time).append(MILLISECOND_UNIT);
}

return builder.toString();
}

public static void main(String[] args) {

mouseMove(DAY, MINUTE);
// mouseSwipe(HOUR, 10 * MILLISECOND);
}

}
posted @ 2022-09-28 20:00  Yblue  阅读(159)  评论(0编辑  收藏  举报