javafx中TicTacToe的java更新UI
在这里,我有一段代码,下面是我从互联网上收集的,用于理解Tictatcoe游戏的java代码。我理解代码是如何编写的以及它的逻辑。它在一个窗口中播放,其中两个用户根据单击的鼠标按钮进行区分。现在我想做一个类似的游戏,可以通过一个服务器和一个客户端构建的网络来玩
蒂克塔克托。java
package sample;
import java.util.ArrayList;
import java.util.List;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Main extends Application {
private boolean playable = true;
private boolean turnX = true;
private Tile[][] board = new Tile[3][3];
private List<Combo> combos = new ArrayList<>();
private Pane root = new Pane();
private Parent createContent() {
root.setPrefSize(600, 600);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Tile tile = new Tile(); //each tile is a rectangular block
tile.setTranslateX(j * 200);
tile.setTranslateY(i * 200);
root.getChildren().add(tile);
board[j][i] = tile;
}
}
// horizontal
for (int y = 0; y < 3; y++) {
combos.add(new Combo(board[0][y], board[1][y], board[2][y]));
}
// vertical
for (int x = 0; x < 3; x++) {
combos.add(new Combo(board[x][0], board[x][1], board[x][2]));
}
// diagonals
combos.add(new Combo(board[0][0], board[1][1], board[2][2]));
combos.add(new Combo(board[2][0], board[1][1], board[0][2]));
return root;
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setScene(new Scene(createContent()));
primaryStage.show();
}
private void checkState() {
for (Combo combo : combos) {
if (combo.isComplete()) {
playable = false;
playWinAnimation(combo);
break;
}
}
}
private void playWinAnimation(Combo combo) {
Line line = new Line();
line.setStartX(combo.tiles[0].getCenterX());
line.setStartY(combo.tiles[0].getCenterY());
line.setEndX(combo.tiles[0].getCenterX());
line.setEndY(combo.tiles[0].getCenterY());
root.getChildren().add(line);
Timeline timeline = new Timeline();
timeline.getKeyFrames().add(new KeyFrame(Duration.seconds(1),
new KeyValue(line.endXProperty(), combo.tiles[2].getCenterX()),
new KeyValue(line.endYProperty(), combo.tiles[2].getCenterY())));
timeline.play();
}
private class Combo {
private Tile[] tiles;
public Combo(Tile... tiles) {
this.tiles = tiles;
}
public boolean isComplete() {
if (tiles[0].getValue().isEmpty())
return false;
return tiles[0].getValue().equals(tiles[1].getValue())
&& tiles[0].getValue().equals(tiles[2].getValue());
}
}
private class Tile extends StackPane {
private Text text = new Text();
private boolean clicked = false;;
public Tile() {
Rectangle border = new Rectangle(200, 200);
border.setFill(null);
border.setStroke(Color.BLACK);
text.setFont(Font.font(72));
setAlignment(Pos.CENTER);
getChildren().addAll(border, text);
setOnMouseClicked(event -> {
if (!playable || clicked)
return;
if (event.getButton() == MouseButton.PRIMARY) {
if (!turnX)
return;
if(!clicked){
drawX();
turnX = false;
clicked = true;
checkState();
}
}
else if (event.getButton() == MouseButton.SECONDARY) {
if (turnX)
return;
if(!clicked){
drawO();
turnX = true;
clicked = true;
checkState();
}
}
});
}
public double getCenterX() {
return getTranslateX() + 100;
}
public double getCenterY() {
return getTranslateY() + 100;
}
public String getValue() {
return text.getText();
}
private void drawX() {
text.setText("X");
}
private void drawO() {
text.setText("O");
}
}
public static void main(String[] args) {
launch(args);
}
}
我想在不同的文件中构建服务器和客户端。服务器。java将包含服务器socket、输入输出流和接受来自socket的连接。我对此没有异议。类似地,客户端。java还将具有socket和必要的流。服务器文件和客户端文件都将具有TicTacToe。java。我可以将代码设置到这一点
我的问题:
我无法通过socket网络更新UI。我怎么做?我在平台上看了一些代码。runLater()但不太理解它们。我需要洞察力
如果有人能给我一个指导,告诉我如何为此编写代码并实现我想要的,这样我就可以解决我的问题,这将对我非常有帮助
谢谢
# 1 楼答案
我为使用平台所做的一切。RunLater函数是创建一个后台线程来接收来自客户机/服务器的数据包。然后使用平台更新UI。runLater函数