目录
- 一.实现功能
 - 二.主要思路
 - 三.代码实现
 - 四.用exe4j生成.exe程序
 - 五.最终效果
 - 六.代码开源
 
一.实现功能
主线图作战结束到结算页自动点击再次前往
二.主要思路
- 判断是否进入了结算界面:记录结算界面某个像素点的RGB值,每隔3秒对这个像素点进行比对
 - 移动鼠标点击再次前往:Java提供的Robot类
 
三.代码实现
- MainFrame.java
主要实现系统托盘的图标,右键菜单栏,菜单项的响应事件 
package com.simple.azurlane.view;
import com.simple.azurlane.auto.MainLine;
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.IOException;
import java.util.Objects;
public class MainFrame {
    public MainFrame() {
        //系统托盘
        SystemTray systemTray = SystemTray.getSystemTray();
        //菜单栏
        PopupMenu pop = new PopupMenu();
        MenuItem control = new MenuItem("start");
        MenuItem exit = new MenuItem("exit");
        pop.add(control);
        pop.addSeparator();
        pop.add(exit);
        control.addActionListener(e -> {
            if (control.getLabel().equals("start")) {
                MainLine.start();
                control.setLabel("stop");
            } else {
                MainLine.stop();
                control.setLabel("start");
            }
        });
        exit.addActionListener(e -> System.exit(0));
        try {
            TrayIcon trayIcon = new TrayIcon(ImageIO.read(Objects.requireNonNull(MainFrame.class.getClassLoader().getResourceAsStream("azurlane.jpg"))), "碧蓝航线", pop);
            trayIcon.setImageAutoSize(true);
            trayIcon.setToolTip("碧蓝航线");
            systemTray.add(trayIcon);
        } catch (IOException | AWTException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        new MainFrame();
    }
}
 
- MainLine.java
主要实现像素点的比对,自动移动鼠标点击 
package com.simple.azurlane.auto;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class MainLine {
    private static ScheduledExecutorService scheduledService;
    private static final Properties properties = new Properties();
    private static Robot robot;
    static {
        try {
            robot = new Robot();
            properties.load(MainLine.class.getClassLoader().getResourceAsStream("config.properties"));
        } catch (AWTException | IOException e) {
            e.printStackTrace();
        }
    }
    static int z = Integer.parseInt(properties.getProperty("z"));
    static int x1 = Integer.parseInt(properties.getProperty("x1"));
    static int y1 = Integer.parseInt(properties.getProperty("y1"));
    static int x2 = Integer.parseInt(properties.getProperty("x2")) / z;
    static int y2 = Integer.parseInt(properties.getProperty("y2")) / z;
    static List<String> rl = Arrays.asList(properties.getProperty("r").split(","));
    static List<String> gl = Arrays.asList(properties.getProperty("g").split(","));
    static List<String> bl = Arrays.asList(properties.getProperty("b").split(","));
    private static void autoWork() {
        Color pixelColor = robot.getPixelColor(x1, y1);
        if (validatePixelColor(rl, gl, bl, pixelColor)) {
            robot.mouseMove(x2, y2);
            robot.mousePress(KeyEvent.BUTTON1_MASK);
            robot.delay(200);
            robot.mouseRelease(KeyEvent.BUTTON1_MASK);
            robot.delay(200);
            robot.mouseMove(0, 0);
        }
    }
    /**
     * 校验一组颜色是否与之指定像素点颜色匹配
     * @param pixelColor 像素点颜色
     * @return true/false
     */
    private static boolean validatePixelColor(List<String> rl, List<String> gl, List<String> bl, Color pixelColor) {
        for (int i = 0; i < rl.size(); i++) {
            if (String.valueOf(pixelColor.getRed()).equals(rl.get(i)) && String.valueOf(pixelColor.getGreen()).equals(gl.get(i)) && String.valueOf(pixelColor.getBlue()).equals(bl.get(i))) {
                return true;
            }
        }
        return false;
    }
    public static void start() {
        scheduledService = Executors.newScheduledThreadPool(1);
        scheduledService.scheduleAtFixedRate(MainLine::autoWork, 0, 3, TimeUnit.SECONDS);
    }
    public static void stop() {
        scheduledService.shutdownNow();
        scheduledService = null;
        System.gc();
    }
}
 
- config.properties
 
#识别的像素点位置
x1=1600
y1=276
#鼠标点击位置
x2=1740
y2=1294
#匹配的rgb颜色
r=99,90,90
g=130,134,121
b=189,198,198
#分辨率-缩放比例
z=2
 
四.用exe4j生成.exe程序
具体参考我的这篇文章exe4j将jar包打成exe程序
五.最终效果


六.代码开源
所有代码已上传我的github仓库



















