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