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();
}
}
}
}