自动采集

wjdr
wangbing 1 year ago
parent 35af343caf
commit db389cb76d

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 444 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.8 KiB

@ -23,13 +23,18 @@ import java.util.concurrent.TimeUnit;
* @version 0.0.1
* @since 1.8
*/
public class JMacro {
public abstract class JMacro {
/**
*
*/
private final Robot robot;
/**
*
*/
protected ViewRect focusRect;
/**
*
*/
@ -41,12 +46,13 @@ public class JMacro {
this.robot = new Robot();
this.robot.setAutoDelay(100);
this.legend = new File("legend");
this.startFocus();
} catch (AWTException e) {
throw new RuntimeException(e);
}
}
public JMacro(File legend) throws AWTException {
public JMacro(File legend) {
this();
this.legend = legend;
}
@ -59,6 +65,29 @@ public class JMacro {
this.legend = legend;
}
/**
*
*
* @return
*/
public void startFocus() {
TaskUtil.asyncTask(() -> {
focusRect = TaskUtil.timeTask(this::focus, 10, TimeUnit.SECONDS);
});
}
/**
*
*
* @return
*/
public abstract ViewRect focus();
/**
*
*/
public abstract void run();
/**
*
*/
@ -127,7 +156,7 @@ public class JMacro {
public void mouseLeftClick(ViewPoint rect) {
mouseMove(rect);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
delayTap();
robot.delay(100);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
}
@ -159,11 +188,11 @@ public class JMacro {
*/
public void mouseLeftDrag(ViewPoint start, ViewPoint end, boolean smooth) {
mouseMove(start, smooth);
delayTap();
delayUnstable();
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
delayTap();
delayUnstable();
mouseMove(end, smooth);
delayTap();
delayUnstable();
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
}
@ -186,11 +215,11 @@ public class JMacro {
*/
public void mouseRightDrag(ViewPoint start, ViewPoint end, boolean smooth) {
mouseMove(start, smooth);
delayTap();
delayUnstable();
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
delayTap();
delayUnstable();
mouseMove(end, smooth);
delayTap();
delayUnstable();
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
}
@ -200,11 +229,11 @@ public class JMacro {
* @param rect
*/
public void mouseWheelClick(ViewRect rect) {
delayTap();
delayUnstable();
robot.mouseMove(rect.getCenter()[0], rect.getCenter()[1]);
delayTap();
delayUnstable();
robot.mousePress(InputEvent.BUTTON2_DOWN_MASK);
delayTap();
delayUnstable();
robot.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
}
@ -214,11 +243,11 @@ public class JMacro {
* @param rect
*/
public void mouseRightClick(ViewRect rect) {
delayTap();
delayUnstable();
robot.mouseMove(rect.getCenter()[0], rect.getCenter()[1]);
delayTap();
delayUnstable();
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
delayTap();
delayUnstable();
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
}
@ -228,9 +257,9 @@ public class JMacro {
* @param rect
*/
public void mouseLeftDoubleClick(ViewRect rect) {
delayTap();
delayUnstable();
robot.mouseMove(rect.getCenter()[0], rect.getCenter()[1]);
delayTap();
delayUnstable();
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.delay(100);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
@ -247,6 +276,13 @@ public class JMacro {
return robot.createScreenCapture(new Rectangle(viewRect.getLeft(), viewRect.getTop(), viewRect.getWidth(), viewRect.getHeight()));
}
/**
*
*/
public ViewRect getFocusRect() {
return focusRect;
}
/**
*
*/
@ -277,7 +313,7 @@ public class JMacro {
* @return
*/
public ViewRect findPic(File pic, double minSimilar) {
return findPic(getScreenRect(), ImageUtil.load(pic), minSimilar);
return findPic(getFocusRect(), ImageUtil.load(pic), minSimilar);
}
/**
@ -288,7 +324,7 @@ public class JMacro {
* @return
*/
public ViewRect findPic(BufferedImage pic, double minSimilar) {
return findPic(getScreenRect(), pic, minSimilar);
return findPic(getFocusRect(), pic, minSimilar);
}
/**
@ -328,6 +364,10 @@ public class JMacro {
* @return
*/
public ViewRect findPic(ViewRect viewRect, BufferedImage pic, double minSimilar) {
// 当聚焦区域为null时默认全屏查找
if (viewRect == null) {
viewRect = getFocusRect() != null ? getFocusRect() : getScreenRect();
}
// 当查找区域比图片还小时,直接返回失败
if (viewRect.getWidth() < pic.getWidth() || viewRect.getHeight() < pic.getHeight()) {
Logger.error("视口尺寸小于图片");
@ -373,22 +413,27 @@ public class JMacro {
}
public void delay() {
delayNormal();
delay(100);
}
public void delayTap() {
int i = RandomUtil.randomInt(100, 500);
robot.delay(i);
public void delay(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
public void delayNormal() {
int i = RandomUtil.randomInt(500, 1000);
robot.delay(i);
public void delayUnstable() {
delayUnstable(500);
}
public void delayLong() {
int i = RandomUtil.randomInt(1000, 2000);
robot.delay(i);
public void delayUnstable(long millis) {
if (millis < 200) {
delay(millis);
return;
}
delay(RandomUtil.randomLong(millis - 100, millis + 100));
}
/**
@ -399,7 +444,7 @@ public class JMacro {
* @return
*/
public ViewRect waitAndFindPic(File file, double minSimilar) {
return waitAndFindPic(getScreenRect(), file, minSimilar, 10, TimeUnit.SECONDS);
return waitAndFindPic(getFocusRect(), file, minSimilar, 10, TimeUnit.SECONDS);
}
/**
@ -439,7 +484,7 @@ public class JMacro {
* @return
*/
public ViewRect waitAndFindPic(File file, double minSimilar, long time, TimeUnit unit) {
return waitAndFindPic(getScreenRect(), file, minSimilar, time, unit);
return waitAndFindPic(getFocusRect(), file, minSimilar, time, unit);
}
/**
@ -482,7 +527,7 @@ public class JMacro {
}
while (true) {
delayTap();
delayUnstable();
ViewRect pic = findPic(rect, image, minSimilar);
if (pic != null) {
return pic;
@ -503,7 +548,7 @@ public class JMacro {
if (!legendName.endsWith(".png")) {
legendName = legendName + ".png";
}
return waitAndMatchPic(rect, new File(legend, legendName), minSimilar, 10, TimeUnit.SECONDS);
return waitAndMatchPic(rect, new File(legend, legendName), minSimilar, 3, TimeUnit.SECONDS);
}
/**
@ -515,7 +560,7 @@ public class JMacro {
* @return
*/
public ViewRect waitAndMatchPic(ViewRect rect, File file, double minSimilar) {
return waitAndMatchPic(rect, file, minSimilar, 10, TimeUnit.SECONDS);
return waitAndMatchPic(rect, file, minSimilar, 3, TimeUnit.SECONDS);
}
/**
@ -530,6 +575,7 @@ public class JMacro {
*/
public ViewRect waitAndMatchPic(ViewRect rect, File file, double minSimilar, long time, TimeUnit unit) {
return TaskUtil.timeTask(() -> {
// TODO: 2024/9/1 因为另起线程,导致发起线程已停止,但此线程未停止
while (true) {
ViewRect matchPic = matchPic(rect, file, minSimilar);
if (matchPic != null) {
@ -548,6 +594,9 @@ public class JMacro {
* @return
*/
public ViewRect matchPic(ViewRect rect, File file, double minSimilar) {
if (minSimilar > 1) {
throw new RuntimeException("this minSimilar must be less than 1");
}
if (!file.exists()) {
Logger.error("file [{}] not exist", file.getAbsolutePath());
return null;

@ -1,49 +0,0 @@
package com.example.jmacro.wjdr;
import com.example.jmacro.wjdr.base.ViewRect;
import com.example.jmacro.wjdr.util.TaskUtil;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
/**
* 线
*
* @author wangbing
* @version 0.0.1
* @since 1.8
*/
public abstract class JMacroThread implements Runnable {
/**
*
*/
protected JMacro macro;
/**
*
*/
protected ViewRect viewRect;
public JMacroThread(JMacro macro) {
this.macro = macro;
TaskUtil.asyncTask(() -> {
viewRect = TaskUtil.timeTask(this::location, 10, TimeUnit.SECONDS);
});
}
/**
*
*
* @return
*/
public abstract ViewRect location();
public ViewRect getViewRect() {
return viewRect;
}
public JMacro getMacro() {
return macro;
}
}

@ -1,6 +1,6 @@
package com.example.jmacro.wjdr;
import com.example.jmacro.wjdr.demo.DemoThread;
import com.example.jmacro.wjdr.demo.MacroForWJDR;
import com.example.jmacro.wjdr.ui.FXMLUtil;
import com.example.jmacro.wjdr.util.Logger;
import com.example.jmacro.wjdr.util.ResourceUtil;
@ -45,7 +45,7 @@ public class JMainApplication extends Application {
stage.show();
JMainApplication.primaryStage = stage;
// 服务初始化
JMainService.init(new DemoThread(new JMacro()));
JMainService.init(new MacroForWJDR());
}
public static void main(String[] args) {

@ -1,7 +1,8 @@
package com.example.jmacro.wjdr;
import com.example.jmacro.wjdr.base.ViewRect;
import com.example.jmacro.wjdr.util.Capture;
import com.example.jmacro.wjdr.tool.Capture;
import com.example.jmacro.wjdr.tool.Location;
import com.example.jmacro.wjdr.util.Dialog;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
@ -33,7 +34,7 @@ public class JMainController {
*/
@FXML
private void onCapture() {
ViewRect screen = JMainService.getInstance().getMacroThread().getViewRect();
ViewRect screen = JMainService.getInstance().getMacro().getFocusRect();
File legend = JMainService.getInstance().getMacro().getLegend();
if (screen == null) {
Dialog.alert("未定位到视口,请稍后再试!");
@ -46,6 +47,24 @@ public class JMainController {
}
}
/**
*
*/
@FXML
private void onLocation() {
ViewRect screen = JMainService.getInstance().getMacro().getFocusRect();
File legend = JMainService.getInstance().getMacro().getLegend();
if (screen == null) {
Dialog.alert("未定位到视口,请稍后再试!");
return;
}
try {
new Location(screen.getLeft(), screen.getTop());
} catch (AWTException awtException) {
awtException.printStackTrace();
}
}
/**
*
*/

@ -1,7 +1,7 @@
package com.example.jmacro.wjdr;
import cn.hutool.core.thread.ThreadUtil;
import com.example.jmacro.wjdr.demo.DemoThread;
import com.example.jmacro.wjdr.demo.MacroForWJDR;
import com.example.jmacro.wjdr.util.Dialog;
import com.example.jmacro.wjdr.util.Logger;
@ -16,11 +16,6 @@ import java.io.File;
*/
public class JMainService {
/**
*
*/
private boolean run;
/**
*
*/
@ -33,56 +28,61 @@ public class JMainService {
return instance;
}
/**
*
*/
public boolean run;
/**
* 线
*/
public DaemonThread daemonThread;
/**
* 线
*
*/
private JMacroThread macroThread;
private JMacro macro;
/**
*
*/
private JMainService() {
Logger.info("初始化服务");
this.daemonThread = new DaemonThread();
Logger.info("初始化守护线程");
this.daemonThread.setDaemon(true);
}
public void setMacroThread(JMacroThread macroThread) {
this.macroThread = macroThread;
public JMacro getMacro() {
return macro;
}
public JMacroThread getMacroThread() {
return macroThread;
public static void init(JMacro macro) {
getInstance().macro = macro;
}
public JMacro getMacro() {
return macroThread.getMacro();
}
public void createDaemon() {
this.daemonThread = new DaemonThread();
Logger.info("初始化守护线程");
this.daemonThread.setDaemon(true);
public static void init(JMacroThread macroThread) {
getInstance().macroThread = macroThread;
// 启动守护线程
this.run = true;
this.daemonThread.start();
}
public static boolean start() {
if (JMainService.getInstance().macroThread.getViewRect() == null) {
Dialog.alert("未定位到视口,请稍后再试!");
if (JMainService.getInstance().macro.getFocusRect() == null) {
JMainService.getInstance().macro.startFocus();
Dialog.alert("未聚焦到视口,请稍后再试!");
return false;
}
if (JMainService.getInstance().run) {
Logger.error("服务已启动");
return false;
}
// 启动服务
Logger.info("启动服务");
// 启动守护线程
JMainService.getInstance().run = true;
JMainService.getInstance().daemonThread.start();
// 创建守护线程
JMainService.getInstance().createDaemon();
return true;
}
@ -105,27 +105,31 @@ public class JMainService {
* @param legend
*/
public static void setLegend(File legend) {
if (getInstance().macroThread != null) {
getInstance().macroThread.getMacro().setLegend(legend);
if (getInstance().macro != null) {
getInstance().macro.setLegend(legend);
}
}
/**
* 线
*/
class DaemonThread extends Thread {
private class DaemonThread extends Thread {
@Override
public void run() {
if (macro == null) {
Logger.error("脚本未设置");
return;
}
while (run) {
Logger.info("守护线程ID={}", Thread.currentThread().getId());
ThreadUtil.sleep(1000);
if (macroThread == null) {
Logger.error("脚本线程未设置");
continue;
try {
macro.run();
ThreadUtil.sleep(1000);
} catch (Exception e) {
Logger.error("异常中断");
}
macroThread.run();
}
Logger.info("服务停止,守护线程已退出");
}
}
@ -133,7 +137,7 @@ public class JMainService {
*
*/
public static void main(String[] args) {
JMainService.init(new DemoThread(new JMacro()));
JMainService.init(new MacroForWJDR());
JMainService.start();
}
}

@ -42,4 +42,12 @@ public class ViewPoint {
public void setY(int y) {
this.y = y;
}
@Override
public String toString() {
return "ViewPoint{" +
"x=" + x +
", y=" + y +
'}';
}
}

@ -83,7 +83,7 @@ public class ViewRect {
@Override
public String toString() {
return "ScreenRect{" +
return "ViewRect{" +
"left=" + left +
", top=" + top +
", right=" + right +

@ -3,25 +3,22 @@ package com.example.jmacro.wjdr.demo;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.example.jmacro.wjdr.JMacro;
import com.example.jmacro.wjdr.JMacroThread;
import com.example.jmacro.wjdr.JMainService;
import com.example.jmacro.wjdr.base.ViewRect;
import com.example.jmacro.wjdr.demo.task.Task_;
import com.example.jmacro.wjdr.demo.task.Task_线;
import com.example.jmacro.wjdr.demo.task.Task_;
import com.example.jmacro.wjdr.demo.task.Task_;
import com.example.jmacro.wjdr.demo.task.*;
import com.example.jmacro.wjdr.util.Logger;
import com.example.jmacro.wjdr.util.TaskUtil;
import java.util.concurrent.TimeUnit;
/**
* 线
*
*
* @author wangbing
* @version 0.0.1
* @since 1.8
*/
public class DemoThread extends JMacroThread {
public class MacroForWJDR extends JMacro {
/**
*
@ -38,15 +35,11 @@ public class DemoThread extends JMacroThread {
*/
private final long workTime = 30 * 60 * 1000;
public DemoThread(JMacro macro) {
super(macro);
}
@Override
public synchronized ViewRect location() {
public synchronized ViewRect focus() {
// 定位窗口
Logger.info("定位窗口");
ViewRect mumu = macro.findPic("慕慕_工具栏", 0.95d);
ViewRect mumu = findPic("慕慕_工具栏", 0.95d);
if (mumu == null) {
Logger.error("定位窗口失败");
return null;
@ -70,12 +63,12 @@ public class DemoThread extends JMacroThread {
Logger.info("任务线程ID={}", Thread.currentThread().getId());
// 获取启动图标
Logger.info("定位启动图标");
ViewRect launch = macro.waitAndFindPic(viewRect, "启动图标", 0.9, 10, TimeUnit.SECONDS);
ViewRect launch = waitAndFindPic(focusRect, "启动图标", 0.9, 10, TimeUnit.SECONDS);
if (launch != null) {
Logger.info("启动图标坐标:", launch.toString());
Logger.info("启动程序");
macro.delay();
macro.mouseLeftClick(launch);
delay();
mouseLeftClick(launch);
} else {
Logger.error("未定位到图标,可能已进入游戏");
Logger.info("继续定位城镇");
@ -85,31 +78,31 @@ public class DemoThread extends JMacroThread {
// 定位城镇
Boolean inMain = TaskUtil.retryTask(() -> {
{ // 定位弹框,关闭弹框
ViewRect rect = macro.matchPic(viewRect, "城镇_充值_L448,36", 0.8d);
ViewRect rect = matchPic(focusRect, "城镇_充值_L448,36", 0.8d);
if (rect != null) {
macro.mouseLeftClick(rect);
mouseLeftClick(rect);
Logger.info("检测到充值广告弹框,关闭弹框");
}
}
{// 定位离线收益
ViewRect rect = macro.matchPic(viewRect, "城镇_离线收益_L204,734", 0.9);
ViewRect rect = matchPic(focusRect, "城镇_离线收益_L204,734", 0.9);
if (rect != null) {
macro.mouseLeftClick(rect);
mouseLeftClick(rect);
Logger.info("检测到离线收益弹框,关闭弹框");
}
}
// 定位城镇
{
ViewRect rect = macro.matchPic(viewRect, "城镇_L456,887", 0.9);
ViewRect rect = matchPic(focusRect, "城镇_L456,887", 0.9);
if (rect != null) {
Logger.info("当前区域【野外】");
Logger.info("返回区域【城镇】");
macro.mouseLeftClick(rect);
mouseLeftClick(rect);
return false;
}
}
{// 定位野外
ViewRect rect = macro.matchPic(viewRect, "野外_L444,888", 0.9);
ViewRect rect = matchPic(focusRect, "野外_L444,888", 0.9);
if (rect != null) {
Logger.info("当前区域【城镇】");
return true;
@ -126,22 +119,30 @@ public class DemoThread extends JMacroThread {
Logger.info("进入城镇成功");
Logger.info("领取探险奖励");
macro.delay();
new Task_线(macro, viewRect).run();
delay();
new Task_线(this, focusRect).run();
Logger.info("收留避难者");
delay();
new Task_(this, focusRect).run();
Logger.info("启动循环任务");
while (!Thread.currentThread().isInterrupted()) {
while (JMainService.getInstance().run) {
// 矿场攻击检测任务
macro.delay();
new Task_(macro, viewRect).run();
delayUnstable(1000);
new Task_(this, focusRect).run();
// 避难者
delayUnstable(1000);
new Task_(this, focusRect).run();
// 矿场攻击检测任务
macro.delay();
new Task_(macro, viewRect).run();
delayUnstable(1000);
new Task_(this, focusRect).run();
// 自动采矿任务
macro.delay();
new Task_(macro, viewRect).run();
// delayUnstable(1000);
// new Task_自动采矿(this, focusRect).run();
}
}
}

@ -1,6 +1,7 @@
package com.example.jmacro.wjdr.demo.task;
import com.example.jmacro.wjdr.JMacro;
import com.example.jmacro.wjdr.JMainService;
import com.example.jmacro.wjdr.base.ViewRect;
import com.example.jmacro.wjdr.util.Logger;
@ -25,14 +26,19 @@ public abstract class BaseTask {
}
public void run() {
Logger.info("守护线程ID={}", Thread.currentThread().getId());
if (Thread.currentThread().isInterrupted()) {
if (!JMainService.getInstance().run) {
Logger.error("服务中断");
return;
}
String name = getClass().getName();
Logger.info("》》》任务[{}]开始》》》", name);
this.task(macro, viewRect);
Logger.info("》》》任务[{}]结束》》》", name);
String name = getClass().getSimpleName();
try {
Logger.info("》》》任务[{}]开始》》》", name);
this.task(macro, viewRect);
Logger.info("》》》任务[{}]结束》》》", name);
} catch (Exception e) {
Logger.info("》》》任务[{}]异常中断》》》", name);
throw e;
}
}
/**

@ -0,0 +1,39 @@
package com.example.jmacro.wjdr.demo.task;
import com.example.jmacro.wjdr.JMacro;
import com.example.jmacro.wjdr.base.ViewRect;
import com.example.jmacro.wjdr.util.Logger;
public class Task_ extends BaseTask {
public Task_(JMacro macro, ViewRect viewRect) {
super(macro, viewRect);
}
@Override
public void task(JMacro macro, ViewRect viewRect) {
ViewRect rect = macro.waitAndMatchPic(viewRect, "城镇_避难者_L3,174", 0.9);
if (rect == null) {
Logger.info("未发现避难者");
return;
}
Logger.info("发现避难者");
Logger.info("收留避难者");
macro.mouseLeftClick(rect);
macro.delayUnstable();
ViewRect rect1 = macro.waitAndMatchPic(viewRect, "城镇_欢迎新成员_L199,773", 0.9);
if (rect1 == null) {
Logger.info("收留避难者异常");
return;
}
macro.delayUnstable();
Logger.info("欢迎新成员");
macro.mouseLeftClick(rect1);
macro.delayUnstable();
Logger.info("返回城镇");
}
}

@ -45,6 +45,18 @@ public class Task_自动采矿 extends BaseTask {
Logger.info("定位资源搜索按钮成功:{}", .toString());
macro.mouseLeftClick();
// 定位木材
// 因为搜索按钮透明,不好比对,通过定位其下面的任务图标定位
ViewRect _ = macro.waitAndMatchPic(this.viewRect, "野外_搜索_L226,879", 0.9);
if (_ == null) {
Logger.error("未检测到【野外_搜索】采矿终止");
return;
}
// todo
String[] types = new String[]{
"生肉", "木材", "煤矿", "铁矿"
};
@ -81,6 +93,7 @@ public class Task_自动采矿 extends BaseTask {
ViewRect typeRect = macro.waitAndFindPic(viewRect, new File("legend", "野外_" + type + ".png"), 0.9);
if (typeRect == null) {
Logger.error("定位【{}】图标失败", type);
return false;
}
Logger.info("定位【{}】图标成功", type);
@ -90,7 +103,7 @@ public class Task_自动采矿 extends BaseTask {
if (level == 0) {
ViewRect = macro.waitAndFindPic(viewRect, new File("legend", "野外_资源_-.png"), 0.98);
while ( != null) {
macro.delayTap();
macro.delayUnstable();
macro.mouseLeftClick();
= macro.waitAndFindPic(viewRect, new File("legend", "野外_资源_-.png"), 0.98);
}

@ -1,7 +1,8 @@
package com.example.jmacro.wjdr.util;
package com.example.jmacro.wjdr.tool;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.log.StaticLog;
import com.example.jmacro.wjdr.util.Logger;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

@ -0,0 +1,166 @@
package com.example.jmacro.wjdr.tool;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.StaticLog;
import com.example.jmacro.wjdr.util.Dialog;
import com.example.jmacro.wjdr.util.Logger;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
/**
*
*
* @author wangbing
* @version 0.0.1
* @since 1.8
*/
public class Location extends JFrame {
/**
* x
*/
private int originX;
/**
* y
*/
private int originY;
/**
* x
*/
private int x;
/**
* y
*/
private int y;
/**
*
*/
private BufferedImage capture;
/**
*
*/
private Color mask = new Color(0, 0, 0, 0.2f);
/**
*
*
* @param originX x
* @param originY y
* @throws AWTException
*/
public Location(int originX, int originY) throws AWTException {
this();
this.originX = originX;
this.originY = originY;
}
public Location() throws AWTException {
init();
}
private void init() throws AWTException {
setExtendedState(Frame.MAXIMIZED_BOTH);
setLocation(0, 0);//位置
setUndecorated(true);
setAlwaysOnTop(true);
setBackground(mask);
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
// 获取屏幕截图
Robot robot = new Robot();
Toolkit tk = Toolkit.getDefaultToolkit();
capture = robot.createScreenCapture(new Rectangle(0, 0, tk.getScreenSize().width, tk.getScreenSize().height));
// 监听窗口关闭
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// 关闭应用时要释放资源
dispose();
System.exit(0);//0正常退出1非正常退出
}
});
addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
super.mouseReleased(e);
x = e.getX();
y = e.getY();
Logger.info("屏幕坐标:[{},{}]", x, y);
Logger.info("相对坐标:[{},{}]", x - originX, y - originY);
close();
Dialog.confirm(StrUtil.format("屏幕坐标:[{},{}]\n相对坐标[{},{}]", x, y, x - originX, y - originY));
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
super.mouseMoved(e);
x = e.getX();
y = e.getY();
repaint();
}
});
addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_ESCAPE: {
StaticLog.info("exit.");
close();
}
break;
}
}
});
setVisible(true);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(capture, 0, 0, capture.getWidth(), capture.getHeight(), this);
// 在背景图上加个蒙层
g2d.setColor(mask);
g2d.fillRect(0, 0, capture.getWidth(), capture.getHeight());
g2d.setColor(Color.WHITE);
g2d.fillRect(x + 5, y + 5, 120, 35);
g2d.setColor(Color.DARK_GRAY);
g2d.drawString(StrUtil.format("屏幕坐标[{},{}]", x, y), x + 10, y + 20);
g2d.drawString(StrUtil.format("相对坐标[{},{}]", x - originX, y - originY), x + 10, y + 35);
}
public void close() {
setVisible(false);
dispose();
}
/**
*
*
* @param args
* @throws AWTException
*/
public static void main(String[] args) throws AWTException {
// 运行坐标图例截图工具
new Location(5, 5);
}
}

@ -36,6 +36,10 @@
<HBox alignment="CENTER_LEFT" layoutX="10.0" layoutY="126.0" prefHeight="35.0" prefWidth="200.0" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="115.0">
<children>
<Button fx:id="capture" focusTraversable="false" mnemonicParsing="false" onMouseClicked="#onCapture" prefHeight="30.0" prefWidth="60.0" text="采图" />
<Button fx:id="location" focusTraversable="false" mnemonicParsing="false" onMouseClicked="#onLocation" prefHeight="30.0" prefWidth="60.0" text="采点">
<HBox.margin>
<Insets left="10.0" />
</HBox.margin></Button>
</children>
<padding>
<Insets left="6.0" />

Loading…
Cancel
Save

Powered by TurnKey Linux.