-3
public class Main {

    public static void main(String[] args) {
        GUI gui = new GUI();
        GameHandler gameHandler = new GameHandler();
        while (!gui.shouldStop()) {
            while (gui.isRunning()) {
                gameHandler.run();
            }
        }
    }

}


import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class GUI extends JFrame implements KeyListener, WindowListener {

    private JLabel label;
    private boolean run = false;
    private boolean stop = false;

    public GUI() {
        label = new JLabel("Not running.");
        JPanel panel = new JPanel();
        panel.setSize(300, 200);
        panel.add(label);
        setSize(300, 200);
        add(panel);
        addKeyListener(this);
        addWindowListener(this);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setResizable(false);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public boolean isRunning() {
        return run;
    }

    public boolean shouldStop() {
        return stop;
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
            String text = label.getText().equalsIgnoreCase("Not running.") ? "Running!" : "Not running.";
            label.setText(text);
            run = !run;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }

    @Override
    public void windowOpened(WindowEvent e) {
    }

    @Override
    public void windowClosing(WindowEvent e) {
        stop = true;
    }

    @Override
    public void windowClosed(WindowEvent e) {
        stop = true;
    }

    @Override
    public void windowIconified(WindowEvent e) {
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
    }

    @Override
    public void windowActivated(WindowEvent e) {
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
    }
}


import java.awt.*;

public class GameHandler {

    private Elektra elektra;
    private Button popupExitButton;

    public GameHandler() {
        elektra = new Elektra();
        popupExitButton = new Button(Button.EXIT_POPUP_IMAGE_PATH);
    }

    public void run() {
        if (elektra.isAlive()) {
            try {
                new Robot().mouseMove(20, 90);
            } catch (AWTException e) {
                e.printStackTrace();
            }
        } else if (popupExitButton.isVisible()) {
            try {
                new Robot().mouseMove(90, 90);
            } catch (AWTException e) {
                e.printStackTrace();
            }
        }
    }

}

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Button {

    BufferedImage image;

    public Button(String fileName) {
        try {
            image = ImageIO.read(ClassLoader.getSystemResourceAsStream(fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public boolean isVisible() {
        try {
            return new ImageSearcher(new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()))).containsSubImage(image);
        } catch (AWTException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static final String EXIT_POPUP_IMAGE_PATH = "images/exitPopupButton.png";

}

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Elektra {

    String imagePath = "images/elektra.png";
    BufferedImage image;

    public Elektra() {
        try {
            image = ImageIO.read(ClassLoader.getSystemResourceAsStream(imagePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public boolean isAlive() {
        try {
            return new ImageSearcher(new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()))).containsSubImage(image);
        } catch (AWTException e) {
            e.printStackTrace();
        }
        return false;
    }

}

我的程序应该检查屏幕上的某个图像,如果找到它应该移动鼠标。问题是,除非我记录消息,否则它永远不会起作用。我不知道为什么。

如果我添加 System.out.println("blah"); 在主类的每个 while 循环中,它都有效。但如果我不包括它们,它不会。有人可以解释一下吗...?为什么会发生这种情况对我来说毫无意义。当我使用调试器时也会发生这种情况。它在调试时有效,但在运行时无效。

它何时起作用的示例:

public class Main {

    public static void main(String[] args) {
        GUI gui = new GUI();
        GameHandler gameHandler = new GameHandler();
        while (!gui.shouldStop()) {
            System.out.println("..");
            while (gui.isRunning()) {
                System.out.println("..");
                gameHandler.run();
            }
        }
    }

}
4

3 回答 3

2

这个MSCCE 的行为是否符合您的预期?(我必须承认我并不完全清楚它应该做什么。似乎和 glug-glug 盒子一样有用。)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

class Main {

    public static void main(String[] args) {
        GameHandler gameHandler = new GameHandler();
        GUI gui = new GUI(gameHandler);
        gameHandler.run();
    }
}

class GUI extends JFrame implements KeyListener {

    private JLabel label;
    private boolean run = false;
    private boolean stop = false;
    GameHandler gameHandler;

    public GUI(GameHandler gameHandler) {
        this.gameHandler = gameHandler;
        label = new JLabel("Not running.");
        JPanel panel = new JPanel();
        panel.setSize(300, 200);
        panel.add(label);
        setSize(300, 200);
        add(panel);
        addKeyListener(this);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setResizable(false);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public boolean isRunning() {
        return run;
    }

    public boolean shouldStop() {
        return stop;
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
            String text = label.getText().equalsIgnoreCase("Not running.") ? "Running!" : "Not running.";
            label.setText(text);
            run = !run;
            if (run) {
                gameHandler.run();
            } else {
                gameHandler.stop();
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }
}

class GameHandler {

    private Elektra elektra;
    private Button popupExitButton;
    Timer timer;

    public GameHandler() {
        elektra = new Elektra();
        popupExitButton = new Button("Button.EXIT_POPUP_IMAGE_PATH");
        timer = new Timer(400, listener);
    }

    public void run() {
        timer.start();
    }

    public void stop() {
        timer.stop();
    }

    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            System.out.println("elektra.isAlive(): " + elektra.isAlive());
            if (elektra.isAlive()) {
                try {
                    new Robot().mouseMove(20, 90);
                } catch (AWTException e) {
                    e.printStackTrace();
                }
            } else if (popupExitButton.isVisible()) {
                try {
                    new Robot().mouseMove(90, 90);
                } catch (AWTException e) {
                    e.printStackTrace();
                }
            }
        }
    };

}

class Button {

    BufferedImage image;

    public Button(String fileName) {
        try {
            //image = ImageIO.read(ClassLoader.getSystemResourceAsStream(fileName));
            image = new Robot().createScreenCapture(new Rectangle(0,0,200,200));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isVisible() {
        try {
            return new ImageSearcher(new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()))).containsSubImage(image);
        } catch (AWTException e) {
            e.printStackTrace();
        }
        return false;
    }

}

class Elektra {

    BufferedImage image;

    public Elektra() {
        try {
            image = new Robot().createScreenCapture(new Rectangle(0,0,200,50));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isAlive() {
        try {
            return new ImageSearcher(
                new Robot().createScreenCapture(
                    new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())))
                    .containsSubImage(image);
        } catch (AWTException e) {
            e.printStackTrace();
        }
        return false;
    }

}

class ImageSearcher {

    BufferedImage mainImage;

    public ImageSearcher(BufferedImage image) {
        mainImage = image;
    }

    public boolean containsSubImage(BufferedImage subImage) {
        int mainWidth = mainImage.getWidth();
        int mainHeight = mainImage.getHeight();
        int width = subImage.getWidth();
        int height = subImage.getHeight();
        for (int xOffset = 0; xOffset < mainWidth && xOffset + width < mainWidth; xOffset++) {
            for (int yOffset = 0; yOffset < mainHeight && yOffset + height < mainHeight; yOffset++) {
                if (picturesEquivalent(subImage, xOffset, yOffset)) {
                    return true;
                }
            }
        }
        return false;
    }

    private boolean picturesEquivalent(BufferedImage subImage, int xOffset, int yOffset) {
        int mainWidth = mainImage.getWidth();
        int mainHeight = mainImage.getHeight();
        int width = subImage.getWidth();
        int height = subImage.getHeight();
        for (int x = 0; x < width && xOffset + x < mainWidth; x++) {
            for (int y = 0; y < height && yOffset + y < mainHeight; y++) {
                int mainRGB = mainImage.getRGB(xOffset + x, yOffset + y);
                int subRGB = subImage.getRGB(x, y);
                if (mainRGB != subRGB) {
                    return false;
                }
            }
        }
        return true;
    }
}
于 2012-05-19T01:55:19.823 回答
1

我在互联网上找不到任何东西来专门支持这一点,但我认为你的打印语句减慢了程序的速度,足以让一切以正确的顺序运行。Swing 请求从单个线程触发,因此事件很容易被捆绑,正如 Hover 所说,特别是如果您有很多输入事件,例如 KeyEvents 等。要解决此问题,您可以尝试 Thread.sleep,但最好将程序重写为不使用该 while 循环或不使用 swing。

于 2012-05-19T01:27:22.637 回答
0

使您的应用程序正确地多线程。

尽可能避免在事件调度线程中工作(google this!)。它应该只用于对 UI 进行更改。永远不要在其中运行等待循环,你甚至可能会死锁你的程序。

花更多的时间来规划您的应用程序何时工作以及何时(以及如何)等待事情发生。您不希望您的应用程序浪费 100% 的 CPU。

于 2012-05-19T11:40:41.650 回答