← Back to team overview

sikuli-driver team mailing list archive

[Question #667296]: Sikuli UI Freeze

 

New question #667296 on Sikuli:
https://answers.launchpad.net/sikuli/+question/667296

Hey,
i am new to Java and sikuli and i do need some help.
I work with Java 8 and the Intellij IDEA.

This is my Main.java class:
package main;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 300));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }

}


And this is my Controller.java:


package main;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.util.Duration;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Location;
import org.sikuli.script.Match;
import org.sikuli.script.Screen;

import java.awt.*;
import java.awt.event.InputEvent;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.ThreadLocalRandom;

public class Controller {

    public Label lbl_score;
    public Button btn_startstop;
    public int score = 0;
    public boolean bool_isWorking= false;


    public void initialize() {
       //pasted this from stackoverflow
        Timeline fiveSecondsWonder = new Timeline(new KeyFrame(Duration.seconds(20), new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                testFunction();
            }
        }));
        fiveSecondsWonder.setCycleCount(Timeline.INDEFINITE);
        fiveSecondsWonder.play();
    }

    public void setBtn_startstop(ActionEvent e) {
        if (bool_isWorking) {
            bool_isWorking= false;
            btn_startstop.setText("Start");
        } else {
            bool_isWorking= true;
            btn_startstop.setText("Stop");
        }
    }

    public void testFunction() {
        if (bool_isWorking) {

            if (bool_isWorking) {
                System.out.println("picture1");
                searchforImage("picture1");
            }

            if (bool_isWorking) {
                System.out.println("picture2");
                searchforImage("picture2");
            }

    }


    public void searchforImage(String picture) {

        boolean found = false;
        int clickPositionX = 0;
        int clickPositionY = 0;
        Screen s = new Screen();

        try {
           
//searches if the image appears and creates "random" coordinations as i dont want the program to click on the exact same spot every time, but using s.click instead of s.find did not help
            Path resurceDirectory = Paths.get("src", "main", "pictures");
            Match m = s.find(resurceDirectory + "/" + picture + ".png");
            
            int x = m.x;
            int y = m.y;

            Location n = m.getCenter();
            int v = n.x;
            int w = n.y;

            int k = v - x;
            int g = x + 2 * k;

            int o = w - y;
            int u = y + 2 * o;

            clickPositionX = ThreadLocalRandom.current().nextInt(x, g + 1);
            clickPositionY = ThreadLocalRandom.current().nextInt(y, u + 1);

            System.out.println("gefunden!");
            found = true;



        } catch (FindFailed e) {
            // TODO Auto-generated catch block
            // e.printStackTrace();
        }

        if (found) {
            mousemove(clickPositionX, clickPositionY);
        }
    }


    //makes the mousemove less annoying
    public void mousemove(int x, int y) {
        if (bool_isWorking) {
            PointerInfo a = MouseInfo.getPointerInfo();
            java.awt.Point b = a.getLocation();
            int xOrig = (int) b.getX();
            int yOrig = (int) b.getY();

            try {
                Robot r = new Robot();

                r.mouseMove(x, y);
                // press the left mouse button
                r.mousePress(InputEvent.BUTTON1_MASK);
                // release the left mouse button
                r.mouseRelease(InputEvent.BUTTON1_MASK);

                // move the mouse back to the original position

//avoids that the mouse gets stuck between my monitors
                r.mouseMove(1873, 610);
                r.mouseMove(xOrig, yOrig);

            } catch (Exception e) {
                //System.out.println(e.toString());
            }
        }
    }
}


I hope you guys can at least kinda understand my code. The problem i have is that once i run the code my UI starts to freeze while sikuli is searching for an image. Any way to work around this?

Thanks for your help!


-- 
You received this question notification because your team Sikuli Drivers
is an answer contact for Sikuli.