mousePressedに関するスレッドがたくさんあることはわかっていますが、私の問題の答えはまだ見つかりません。私は楽しく経験しただけでプログラムを作ったが、私はRobotクラスのmousePressedメソッドで問題に遭遇した。これは私のコードです:Java mousePressが正しく機能していない
//Program to say "Hi!" by moving cursor
//https://sketch.io/sketchpad/
//Open up sketchpad, run OCursor, then click on the sketchpad
import java.awt.*;
import javax.swing.*;
import java.awt.event.InputEvent;
public class OCursor {
public static void main(String[] args) {
try {
// These are the screen coordinates
int cd1 = 400;
int cd2 = 600;
int cd3 = 700;
int cd4 = 750;
int cd5 = 800;
int cd6 = 1000;
int cd7 = 1100;
int cd8 = 1200;
int cd9 = 1400;
// This is the time and amount of steps
int t = 300, n = 5000;
// 5 second delay to click on sketchpad
Robot robot = new Robot();
robot.delay(5000);
// Move and click the cursor
robot.mouseMove(cd2, cd5);
robot.mousePress(InputEvent.BUTTON1_MASK);
mouseGlide(cd2, cd5, cd2, cd1, t, n);
mouseGlide(cd2, cd2, cd5, cd2, t, n);
mouseGlide(cd5, cd5, cd5, cd1, t, n);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(100);
Robot robot2 = new Robot();
robot2.mouseMove(cd6, cd1);
robot2.mousePress(InputEvent.BUTTON1_MASK);
mouseGlide(cd6, cd1, cd8, cd1, t, n);
mouseGlide(cd7, cd1, cd7, cd5, t, n);
mouseGlide(cd6, cd5, cd8, cd5, t, n);
robot2.mouseRelease(InputEvent.BUTTON1_MASK);
robot2.delay(100);
Robot robot3 = new Robot();
robot3.mouseMove(cd9, cd5);
robot3.mousePress(InputEvent.BUTTON1_MASK);
mouseGlide(cd9, cd5, cd9, cd4, t, n);
robot3.mouseRelease(InputEvent.BUTTON1_MASK);
robot3.delay(100);
robot3.mouseMove(cd9, cd3);
robot3.mousePress(InputEvent.BUTTON1_MASK);
mouseGlide(cd9, cd3, cd9, cd1, t, n);
robot3.mouseRelease(InputEvent.BUTTON1_MASK);
robot3.delay(100);
}
catch (AWTException err) {
err.printStackTrace();
}
}
public static void mouseGlide(int x1, int y1, int x2, int y2, int t, int n) {
// "t" being time and "n" being amount of steps, with more steps being smoother
// mouseGlide code borrowed from http://stackoverflow.com/questions/9387483/how-to-move-a-mouse-smoothly-throughout-the-screen-by-using-java
try {
Robot robot = new Robot();
double dx = (x2 - x1)/((double) n);
double dy = (y2 - y1)/((double) n);
double dt = t/((double) n);
for (int step = 1; step <= n; step++) {
Thread.sleep((int) dt);
robot.mouseMove((int) (x1 + dx * step), (int) (y1 + dy * step));
}
}
catch (AWTException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
問題は私が持っているmousePressは実際に適切に解放されません。それはまったく解放されないか、またはそれが押されなければならないときに解放するでしょう。私はそれをリリースするために必要なたびにmouseMoveを使うだけでそれを克服しましたが、これは必要ではありません。私は各mouseRelease後に遅延を入れて聞いたが、それを修正する必要がありますが、それは何もしていません。なぜ正しく動作しないのか、mouseReleaseの代わりにmouseMoveを使用しなければならない理由はわかりません。私はまた、すべての手紙に新しいロボットを使ってみましたが、違いはありません。
描画する各文字の間にmouseMoveを入れずにコードを実行すると、正しく動作しません。
また、他の人のコードをmouseGlideに使用しましたが、これは私が使ったことのないロボットクラスをテストしているためです。