|
|
|
@ -5,7 +5,6 @@ import com.example.jmacro.wjdr.base.ScreenPoint;
|
|
|
|
|
import com.example.jmacro.wjdr.base.ScreenRect;
|
|
|
|
|
import com.example.jmacro.wjdr.util.ColorUtil;
|
|
|
|
|
import com.example.jmacro.wjdr.util.Imager;
|
|
|
|
|
import com.example.jmacro.wjdr.util.Logger;
|
|
|
|
|
import com.example.jmacro.wjdr.util.TaskUtil;
|
|
|
|
|
|
|
|
|
|
import java.awt.*;
|
|
|
|
@ -15,47 +14,62 @@ import java.io.File;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Java脚本
|
|
|
|
|
* Java脚本精灵
|
|
|
|
|
*/
|
|
|
|
|
public class JMacro {
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws AWTException {
|
|
|
|
|
JMacro jMacro = new JMacro();
|
|
|
|
|
jMacro.mouseMove(new ScreenPoint(100, 100));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 机器人
|
|
|
|
|
* 机器人操作实例
|
|
|
|
|
*/
|
|
|
|
|
private Robot robot;
|
|
|
|
|
|
|
|
|
|
private JGameThread thread;
|
|
|
|
|
private final Robot robot;
|
|
|
|
|
|
|
|
|
|
public JMacro() throws AWTException {
|
|
|
|
|
this.robot = new Robot();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public JGameThread getThread() {
|
|
|
|
|
return thread;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setThread(JGameThread thread) {
|
|
|
|
|
this.thread = thread;
|
|
|
|
|
/**
|
|
|
|
|
* 鼠标移动
|
|
|
|
|
*
|
|
|
|
|
* @param point 坐标点
|
|
|
|
|
*/
|
|
|
|
|
public void mouseMove(ScreenPoint point) {
|
|
|
|
|
mouseMove(point, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 鼠标移动
|
|
|
|
|
*
|
|
|
|
|
* @param point 屏幕点
|
|
|
|
|
* @param point 坐标点
|
|
|
|
|
* @param smooth 平滑移动
|
|
|
|
|
*/
|
|
|
|
|
public void mouseMove(ScreenPoint point) {
|
|
|
|
|
// 获取当前鼠标位置
|
|
|
|
|
Point mousePoint = MouseInfo.getPointerInfo().getLocation();
|
|
|
|
|
|
|
|
|
|
int startX = mousePoint.x;
|
|
|
|
|
int startY = mousePoint.y;
|
|
|
|
|
// 分10次移动到指定点
|
|
|
|
|
for (int i = 1; i <= 10; i++) {
|
|
|
|
|
float d = i / 10f;
|
|
|
|
|
int dx = (int) (startX + (point.getX() - startX) * d);
|
|
|
|
|
int dy = (int) (startY + (point.getY() - startY) * d);
|
|
|
|
|
robot.mouseMove(dx, dy);
|
|
|
|
|
robot.delay(RandomUtil.randomInt(30, 70));
|
|
|
|
|
public void mouseMove(ScreenPoint point, boolean smooth) {
|
|
|
|
|
if (smooth) {
|
|
|
|
|
// 获取当前鼠标位置
|
|
|
|
|
Point mousePoint = MouseInfo.getPointerInfo().getLocation();
|
|
|
|
|
|
|
|
|
|
int startX = mousePoint.x;
|
|
|
|
|
int startY = mousePoint.y;
|
|
|
|
|
|
|
|
|
|
// 求两点距离
|
|
|
|
|
double absX = Math.abs(startX - point.getX());
|
|
|
|
|
double absY = Math.abs(startY - point.getY());
|
|
|
|
|
double absZ = Math.sqrt(Math.pow(absX, 2) + Math.pow(absY, 2));
|
|
|
|
|
int times = (int) (absZ / 30 + (absZ % 30 > 0 ? 1 : 0));
|
|
|
|
|
// 分times次移动到指定点
|
|
|
|
|
for (int i = 1; i <= times; i++) {
|
|
|
|
|
float d = i * 1.0f / times;
|
|
|
|
|
int dx = (int) (startX + (point.getX() - startX) * d);
|
|
|
|
|
int dy = (int) (startY + (point.getY() - startY) * d);
|
|
|
|
|
robot.mouseMove(dx, dy);
|
|
|
|
|
robot.delay(RandomUtil.randomInt(5, 20));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
robot.mouseMove(point.getX(), point.getY());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -126,7 +140,6 @@ public class JMacro {
|
|
|
|
|
robot.mouseRelease(InputEvent.BUTTON1_MASK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 捕获指定区域屏幕
|
|
|
|
|
*/
|
|
|
|
@ -136,8 +149,6 @@ public class JMacro {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取屏幕范围
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public ScreenRect getScreenRect() {
|
|
|
|
|
Toolkit tk = Toolkit.getDefaultToolkit();
|
|
|
|
@ -149,7 +160,7 @@ public class JMacro {
|
|
|
|
|
*
|
|
|
|
|
* @param pic 图片
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @return 匹配的图片区域
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
*/
|
|
|
|
|
public ScreenRect findPic(File pic, double minSimilar) {
|
|
|
|
|
return findPic(getScreenRect(), Imager.load(pic), minSimilar);
|
|
|
|
@ -160,7 +171,7 @@ public class JMacro {
|
|
|
|
|
*
|
|
|
|
|
* @param pic 图片
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @return 匹配的图片区域
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
*/
|
|
|
|
|
public ScreenRect findPic(BufferedImage pic, double minSimilar) {
|
|
|
|
|
return findPic(getScreenRect(), pic, minSimilar);
|
|
|
|
@ -172,7 +183,7 @@ public class JMacro {
|
|
|
|
|
* @param pic 参考图
|
|
|
|
|
* @param screenRect 查找范围
|
|
|
|
|
* @param minSimilar 相似度
|
|
|
|
|
* @return
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
*/
|
|
|
|
|
public ScreenRect findPic(ScreenRect screenRect, BufferedImage pic, double minSimilar) {
|
|
|
|
|
// 当查找区域比图片还小时,直接返回失败
|
|
|
|
@ -220,26 +231,41 @@ public class JMacro {
|
|
|
|
|
|
|
|
|
|
public void waitTap() {
|
|
|
|
|
int i = RandomUtil.randomInt(100, 200);
|
|
|
|
|
// Logger.info("随机等待{}ms", i);
|
|
|
|
|
robot.delay(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void waitNormal() {
|
|
|
|
|
int i = RandomUtil.randomInt(500, 1500);
|
|
|
|
|
// Logger.info("随机等待{}ms", i);
|
|
|
|
|
robot.delay(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void waitLong() {
|
|
|
|
|
int i = RandomUtil.randomInt(2000, 5000);
|
|
|
|
|
// Logger.info("随机等待{}ms", i);
|
|
|
|
|
robot.delay(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 等待并查找图片
|
|
|
|
|
*
|
|
|
|
|
* @param rect 查找区域
|
|
|
|
|
* @param file 图例
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
*/
|
|
|
|
|
public ScreenRect waitAndFindPic(ScreenRect rect, File file, double minSimilar) {
|
|
|
|
|
return waitAndFindPic(rect, file, minSimilar, 10, TimeUnit.SECONDS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 等待并查找图片
|
|
|
|
|
*
|
|
|
|
|
* @param rect 查找区域
|
|
|
|
|
* @param file 图例
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @param time 最长等待时间
|
|
|
|
|
* @param unit 最长等待时间单位
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
*/
|
|
|
|
|
public ScreenRect waitAndFindPic(ScreenRect rect, File file, double minSimilar, long time, TimeUnit unit) {
|
|
|
|
|
return TaskUtil.timeTask(() -> {
|
|
|
|
|
while (true) {
|
|
|
|
|