java - 鼠标事件: the order of operations

标签 java events javafx

我的鼠标事件有问题。有些代码似乎是在其他代码之前完成的。我用蒙特卡洛模拟编写了一个围棋游戏。

holder.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if(mouseX > 0 && mouseY > 0 && player1){
                    boolean[][] availableFields =  GameBoard.checkAvailableFields(GameBoard.stoneColor.WHITE);

                    if (availableFields[arrayX][arrayY] == true) {
                        System.out.println("White rock on " + arrayX + ", " + arrayY);
                        putRock(arrayX, arrayY, GameBoard.stoneColor.WHITE);
                        gameBoard.setField(arrayX, arrayY, GameBoard.stoneColor.WHITE);
                        player1 = false;
                        System.out.println("Rock put");
                    } else{
                        System.out.println("no put");
                    }

                    gameBoard.printFields();

                    MonteCarlo montecarlo = new MonteCarlo(gameBoard);
                    int field = montecarlo.nextMove();
                    int x = field % 5;
                    int y = field / 5;
                    arrayToMouseXY(x, y);
                    arrayX = x;
                    arrayY = y;
                    System.out.println("Black rock on " + field + ": " + arrayX + ", " + arrayY);
                    putRock(arrayX, arrayY, GameBoard.stoneColor.BLACK);
                    gameBoard.setField(arrayX, arrayY, GameBoard.stoneColor.BLACK);
                    player1 = true;

                    gameBoard.printFields();
                }
            }
        }); 

函数putRock(int x, int y, GameBoard.stoneColor color)在屏幕上绘制图像。问题是,在计算整个蒙特卡罗模拟之前,不会绘制第一 block 岩石。我怎样才能防止这种情况发生?

最佳答案

您可以尝试在后台线程上运行模拟,并在后台线程完成后立即使用模拟结果的步骤更新 GUI。

示例:

// Task for your simulation
Task<Integer> task = new Task<Integer>(){

    @Override
    protected Integer call() throws Exception {
        MonteCarlo montecarlo = new MonteCarlo(gameBoard);
        return montecarlo.nextMove();
    }

};

// If you want to disable the GUI while your simulation runs (root is the root Node of your Scene graph)
// root.setDisable(true); 

// When the task finished, update the GUI with the step
task.setOnSucceeded(new EventHandler<WorkerStateEvent>() {

    @Override
    public void handle(WorkerStateEvent event) {
        int field = task.getValue();
        int x = field % 5;
        int y = field / 5;
        arrayToMouseXY(x, y);
        arrayX = x;
        arrayY = y;
        System.out.println("Black rock on " + field + ": " + arrayX + ", " + arrayY);
        putRock(arrayX, arrayY, GameBoard.stoneColor.BLACK);
        gameBoard.setField(arrayX, arrayY, GameBoard.stoneColor.BLACK);
        player1 = true;

        gameBoard.printFields();
        // "release" the GUI
        // root.setDisable(false); 
    }
});

// Start the thread
Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();

关于java - 鼠标事件: the order of operations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37647729/

相关文章:

java - 使用java mail multipart MIME仅读取最新回复的内容

javascript - keydown 事件只触发一次

python - Python 中按下按钮时的 Action

JavaFX:在折线图的节点上设置单击监听器

java - 如何拦截自己创建的JdbcTemplate实例

java - 谷歌应用程序引擎和Java : images thumb

java - 随机爬山法

c# - 在另一个函数中调用表单加载事件

javafx - 如何创建复杂对象的 ListView 并允许编辑对象上的字段?

java - JavaFX 中的复选框单元格工厂 + Tableview