javaFX Connect四,同时更新服务器和客户端线程

时间:2019-04-15 18:37:29

标签: java eclipse multithreading sockets javafx

我有4个类:用户1的服务器类,用户2的客户端类, GridPane类用于设置板,ImageView类用于跟踪网格中的每个ImageView。

我遇到的问题是,当我尝试为放下的碎片创建动画时,该动画仅出现在用户单击的窗口上,直到动画完成,然后它将其放置在用户单击的窗口的底部。其他用户窗格。

// --------问题出现在建立IVs方法的底部-------------

public class ConnectFour extends GridPane
{
    private  final int BOARD_SIZEX = 7;
    private  final int BOARD_SIZEY = 6;
    private ImageView[][] aIVs ;
    private Image obImgRed;
    private Image obImgYellow;
    private Image obCurrent;

    private boolean bMyTurn;
    private BiFunction<Integer, Integer, Integer> func;

    public ConnectFour(BiFunction<Integer, Integer, Integer> obFunc)
    {
        super();
        func = obFunc;
        this.setStyle(" -fx-background-color: lightgray; -fx-vgap: 4; -fx-hgap:4; -fx-grid-lines-visible: true");
        establishIVs();
        this.setAlignment(Pos.CENTER);

        //Place the elements onto a GridView to work with
        for (int i=0; i<BOARD_SIZEX; i++)
        {
            for (int j=0; j<BOARD_SIZEY; j++)
            {
                this.add(aIVs[i][j], i, j);
            }
        }
    }

    public void setFirstTurn(boolean bVal)
    {
        setTurn(bVal);
        //Additionally we want to set the Current Image we are playing with
        //This method will be called on start up.
        if(bVal)
        {
            this.obCurrent = this.obImgRed;
            Platform.runLater(() -> this.setStyle("-fx-background-color: green; -fx-vgap: 4; -fx-hgap:4; -fx-grid-lines-visible: true"));
        }
        else
        {
            this.obCurrent = this.obImgYellow;
            Platform.runLater(() -> this.setStyle("-fx-background-color: blue; -fx-vgap: 4; -fx-hgap:4; -fx-grid-lines-visible: true"));
        }
    }

    public void setTurn(boolean bVal)
    {
        this.bMyTurn = bVal;
        if(bVal)
        {
            Platform.runLater(() -> this.setStyle("-fx-background-color: green; -fx-vgap: 4; -fx-hgap:4; -fx-grid-lines-visible: true"));
        }
        else
        {
            Platform.runLater(() -> this.setStyle("-fx-background-color: blue; -fx-vgap: 4; -fx-hgap:4; -fx-grid-lines-visible: true"));
        }
    }

    public void setOpponentPiece(int nRow, int nCol)
    {
        Image obImg = obCurrent.equals(obImgYellow)? obImgRed : obImgYellow;
        this.aIVs[nRow][nCol].setImage(obImg);
    }

    public void setPiece(int nRow, int nCol)
    {
        this.aIVs[nRow][nCol].setImage(this.obCurrent);
    }

    private void animate(int nCol, Image obEmpty) 
    {
        int nRow = 0;
        boolean ColNotFull = false;
        while(this.aIVs[nCol][nRow].getImage().equals(obEmpty) && this.bMyTurn && nRow < 5)
        {
            System.out.printf("start of while(animate) loop\n");
            try
            {
                ColNotFull = true;
                aIVs[nCol][nRow].setImage(obCurrent);
                System.out.println("Start of sleep\n");
                Thread.sleep(1000);
                System.out.println("end of sleep\n");
                nRow++;
                if(this.aIVs[nCol][nRow].getImage().equals(obEmpty) && this.bMyTurn)
                {
                    System.out.printf("current space finish and next space clear\n");
                    aIVs[nCol][nRow-1].setImage(obEmpty);
                    if(nRow == 5) 
                    {
                        aIVs[nCol][nRow].setImage(obCurrent);
                    }
                }
            }
            catch(InterruptedException exp)
            {
            }
        }

        if(ColNotFull){
            System.out.println("animation finished\n");
            this.func.apply(nCol, nRow);
            setTurn(ColNotFull);
        }
    }

// --------------------这是发生问题的地方------------------ -

    private void establishIVs()
    {
        //Set the images
        this.obImgRed = new Image("file:images/Connect4/Circle-red.png");
        this.obImgYellow = new Image("file:images/Connect4/Circle-yellow.png");
        Image obEmpty = new Image("file:images/Empty.png");
        this.obCurrent = obImgRed;
        this.aIVs = new ImageView[BOARD_SIZEX][BOARD_SIZEY];

        for (int i=0; i < BOARD_SIZEX; i++)
        {
            int nCol = i;
            for (int j=0; j < BOARD_SIZEY; j++)
            {
                Connect4IV obIV = new Connect4IV(obEmpty, j, i,  90);
                obIV.getView().setOnMouseClicked( e -> {
                    Thread obThread = new Thread(() ->  {
                        System.out.printf("clicked\n");
                        animate(nCol, obEmpty);
                    });
                    obThread.setDaemon(true);
                    obThread.start();
                });
                this.aIVs[i][j] = obIV.getView();
            }
        }
    }
}

0 个答案:

没有答案