Can't get the thread to sleep after displaying my grid - java

I have a grid Pane which I want to make visible. I want to then pause the program for 2 seconds and make the grid invisible again. For some reason the grid becomes visible after the thread.sleep that I use in my program.
This all happens inside a button click event.
I tried moving around the thread.sleep, putting them in a new method and using multiple sleep but nothing worked.
gameGrid.setVisible(true)
gameGrid.setVisible(false)
Button event:
public void handleButtonGo(ActionEvent Event) throws IOException { //On go button press
boolean validation = true;
try {
gameGrid.setVisible(true);
placeShips();
}catch (Exception e){
labelwarning.setText(e.getMessage()); //on error the program will stop trying to place ships and refresh any ships placed so far.
validation = false;
//gameGrid.getChildren().clear();
//BoardSetup();
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
gameGrid.setVisible(false);
}
}
The grid is displayed for a millisecond after the thread.sleep.

Use PauseTransition.
public void handleButtonGo(ActionEvent Event) throws IOException { //On go button press
boolean validation = true;
try {
gameGrid.setVisible(true);
placeShips();
}catch (Exception e){
labelwarning.setText(e.getMessage()); //on error the program will stop trying to place ships and refresh any ships placed so far.
validation = false;
//gameGrid.getChildren().clear();
//BoardSetup();
PauseTransition wait = new PauseTransition(Duration.seconds(2));
wait.setOnFinished((e) -> {
/*YOUR METHOD*/
gameGrid.setVisible(false);
});
wait.play();
}
}

Related

Java: Thread.sleep mousepress delay

I've been trying to create an autoclicker in java using jnativehook.
It works fine, even compiles and runs. My problem is using Thread.sleep to try and add a delay between clicks:
bot.mousePress(InputEvent.BUTTON1_MASK);
Thread.sleep(50);
bot.mouseRelease(InputEvent.BUTTON1_MASK);
If I were to input a delay of 0 it will function fine. But given no delay it will click too fast.
When I add a delay it will click fine but, when I release the trigger key it will keep clicking for a few seconds given the time it's been clicking. A delay of 0 will not do this however.
Full code:
public class App implements NativeKeyListener{
private JPanel panel1;
private JTabbedPane tabbedPane1;
private JButton spoilerButton;
private JSlider slider1;
private JSlider slider2;
//Removed irrelevant code...
static Robot bot;
static {
try {
bot = new Robot();
} catch (Exception e) {
e.printStackTrace();
}
}
static boolean pressed;
public void click() throws InterruptedException {
try {
bot.mousePress(InputEvent.BUTTON1_MASK);
Thread.sleep(50);
bot.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (Exception e) {
e.printStackTrace();
}}
#Override
public void nativeKeyPressed(NativeKeyEvent e) {
if (NativeKeyEvent.getKeyText(e.getKeyCode()) == "Delete") {
pressed = true;
while (pressed){
try {
click();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}}
#Override
public void nativeKeyReleased(NativeKeyEvent e) {
if (NativeKeyEvent.getKeyText(e.getKeyCode())=="Delete"){
pressed=false;
}}
#Override
public void nativeKeyTyped(NativeKeyEvent e) {
}
public static void main(String[] args) {
JFrame frame = new JFrame("Autoclicker");
frame.setContentPane(new App().panel1);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setSize(400,148);
frame.setResizable(false);
try{GlobalScreen.registerNativeHook();
} catch (Exception e){
System.exit(1);}
GlobalScreen.addNativeKeyListener(new App());
}
}
Looks like the method nativeKeyPressed() gets called continuously when you keep pressing the trigger key. This results in several calls to click() method (assuming it's multi threaded) and due to the sleep() between mouse press and release (mouse click is complete when released) this can happen.
Hence you can try two options depending on the root cause:
1. move the sleep() call after mouse released. If the nativeKeyPressed gets called concurrently this won't work.
2. Use a different thread to execute the click() method when trigger is pressed. In this case, you may need to submit a Runnable object to your thread each time with the 'pressed' check and click() call in it. This will ensure it won't run after pressed becomes false.

How to cause a left click then turn off thread?

private void runInBackground() {
new Thread(new Runnable() {
#Override
public void run() {
while (running) {
try {
checkPixel();
} catch (AWTException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
public void stop() {
this.running = false;
}
public void start() {
this.running = true;
}
So I have some code someone provided me to monitor the change in color in the middle of the screen. I want to essentially turn on/off checkPixel() after I press something like F9 but how can I do this without a GUI because I can't seem to find anything that allows this. I believe KeyListeners only work with GUIs?
EDIT: Ok so instead while I'm checking for pixel changes in the thread. Once a pixel change has been detected I want to cause create a "left click action" in checkPixel() then turn off the thread. Any help with this?
I guess you want functionality provided by JNativeHook library.
The library allows for grabbing a key from the backgrond.

Platform.runLater() doesn't work for not on fx application thread

I'm trying to build "Stick Hero" board game using javafx (JDK8). I use
scene.setOnKeyPressed(e -> {
Thread thread = new Thread() {
#Override
public void run() {
if (e.getCode().equals(KeyCode.ENTER)) {
// do some graphical changes
playGame();
}
}
};
thread.start();
});
to listen to keyboard and by pressing enter the vertical line turns into a horizontal bridge and the human pass the bridge and this will continue until the length of bridge doesn't match the distance. Now i want to update UI during playGame() method is running. When i tried to do scoreLabel.setText(String.valueOf(score)); and update the scoreLabel text i encountered Not on FX application thread error. playGame() is something like
for (int i = 0; i < cycle; i++) {
goOneCycle();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
fixPosition();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
and in goOneCycle(); and fixPosition(); i do some graphical changes but it does not shown until the end of the loop.
Now i have two questions. First why i can do stuff like scoreLabel.setLayoutX(500); but i cannot do scoreLabel.setText(String.valueOf(score)); i mean in the first case i do not encounter Not on FX application thread. And how i can update scoreLabel text. Notice that i already tried to use Platform.runLater() but when i use this animations and graphical changes will not shown in playGame() method, and just the final frame is shown -all of the animations is done but it does not shown.

Robot class, mouseMove in infinite loop

I have a jbutton which I want, as soon as it's clicked to make an infinite loop of a mouseMove by a robot class. Then, to be stopped when it is clicked again. Problem is in my code when I press it for the first time, it causes the system to freeze and nothing happens when I click it again. I use:
boolean go = false
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
go = !go;
if (go)
jb.setText("Stop!");
else
jb.setText("Start!");
try {
Robot robot = new Robot();
while (go) {
robot.mouseMove(500, 500);
robot.delay(1000);
robot.mouseMove(500, 400);
}
} catch (AWTException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
Swing is a single threaded environment. This means that if anything should block this thread, Swing will be unable to respond to new events, including paint requests and it will appear that you UI has been frozen...because it has...
Take a look at Concurrency in Swing
The simplest solution might be to spawn a new thread and run your loop within it...
Start by writing a Runnable that handels the work you want done...
public class MouseRunner implements Runnable {
#Override
public void run() {
try {
Robot robot = new Robot();
while (go) {
robot.mouseMove(500, 500);
robot.delay(1000);
robot.mouseMove(500, 400);
}
} catch (AWTException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
Then when the user first clicks the button, create a Thread and start it...
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
go = !go;
if (go) {
jb.setText("Stop!");
Thread t = new Thread(new MouseRunner());
t.start();
} else {
jb.setText("Start!");
}
}
});
Beware, your go variable is likely going to need to made volatile

Program a button be pressed a maximum of once every 5 seconds in java

For a current project we need to allow a user only to press a button once every 5 or so seconds. We use a button to start a print job but we need to stop users from spamming the button and starting a dozen print jobs.
We are currently trying with the following code but it seems to que the clicks even when the button is disabled. So after a 5 second delay the clicks are registered even tough in that time the button is disabled.
private void Button1ActionPerformed(java.awt.event.ActionEvent evt) {
Button1.setEnabled(false);
pressCount++;
System.out.println("Press count: " + pressCount);
PrintJob print = new PrintJob();
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(GUIFrame.class.getName()).log(Level.SEVERE, null, ex);
}
try {
print.PrintJob();
} catch (IOException ex) {
Logger.getLogger(GUIFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
Program a button be pressed a maximum of once every 5 seconds in java
have look at JButton#setMultiClickThreshhold(long threshhold)
proper way is add Swing Action to the JButton instead of [ActionListener][4] and by using Swing Timer to block isEnable
Don't make the EDT wait for 5 seconds. You should use another thread to sleep for 5 seconds and the setting the button enabled. Something like this:
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// handle it
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Button1.setEnabled(true);
}
});
}
}).start();

Categories