JavaFX仅使用调试和断点更新图形

时间:2016-06-23 09:23:32

标签: java user-interface debugging javafx breakpoints

我正在通过RMI使用javaFX为棋盘游戏创建GUI。 我有很多问题,因为在找到更新我的GUI的方法后,如果我没有在某一行中放置切换断点,我就无法真正更新GUI。

如果我不是在调试模式下启动我的客户端,那么图形似乎没有进行更新,它们只是出现而且没有任何反应。

此外,如果我没有将切换放在我的代码的指定行中,并将其放在另一行(在方法调用序列的更深处),则没有任何反应。所以客户端线程不会停止;就好像它不会调用它对断点进行调用一样。 我在同一台机器上启动我的客户端,只有一个(使用切换断点)得到更新。

现在我将向您展示我的代码。

javafx从名为RMIGUIView.java的文章开始

@Override
public synchronized void run() {
    waiting = true;
    pause();
    guiDisplayer = new GUIDisplayer();
    (new Thread(() -> guiDisplayer.startGUI())).start();
    waiting = false;
    do {
        state.acceptView(this);
    } while(!endGame);
}

然后我开始使用GUI(在启动方法期间,我按照此处的建议将控制台保存在控制器上 Passing Parameters JavaFX FXML

public class GUIDisplayer extends Application {

private StartTurnState currentState;
private Stage stage;

public void start(Stage stage) throws Exception {
    GUIController controller = new GUIController();
    FXMLLoader fxmlLoader = new FXMLLoader();
    Parent root = fxmlLoader.load(getClass().getResource("sceneMap.fxml"));
    stage.setTitle("Council of Four");
    Scene newScene = new Scene(root);
    stage.setScene(newScene);
    stage.show();
    controller.setStage(stage);
}

public static void startGUI() {
    Application.launch();
} }
gui开始之后

我在我的RMIGUIView.java中返回并执行此操作

@Override
public void visit(StartTurnState currentState) {
    GUIController guiController = new GUIController();
    guiController.updateGUI(currentState);
}

这里是我的控制器类,如果我想要一些事情,我必须把切换断点放在那里(请在课堂评论中阅读)

public class GUIController {

@FXML
ArrayList<ImageView> citiesImagesList;
@FXML
Group group;
@FXML
TableView<Player> players;
@FXML
TableColumn<Player, String> playerName;
@FXML
TableColumn<Player, String> victoryPoints;
@FXML
TableColumn<Player, String> coins;
@FXML
TableColumn<Player, String> assistants;
@FXML
TableColumn<Player, String> nobilityPoints;
@FXML
ImageView A;
@FXML
ImageView B;
@FXML
ImageView C;
@FXML
ImageView D;
@FXML
ImageView E;
@FXML
ImageView F;
@FXML
ImageView G;
@FXML
ImageView H;
@FXML
ImageView I;
@FXML
ImageView J;
@FXML
ImageView K;
@FXML
ImageView L;
@FXML
ImageView M;
@FXML
ImageView N;
@FXML
ImageView O;
@FXML
ImageView king;

private static StartTurnState currentState;
private static GUIController self = null;
private static Stage stage;

public GUIController() {
    self = this;
}

private void placeRewardToken(Group group, List<ImageView> citiesImagesList) {
    Map<String, City> citiesMap = GUIController.currentState.getGameMap().getCitiesMap();
    for (ImageView imageView : citiesImagesList) {
        City city = citiesMap.get(imageView.getId().toString());
        if (city instanceof NormalCity) {
            RewardToken rewardToken = ((NormalCity) city).getRewardToken();
            Text text = new Text();
            text.setText(rewardToken.toString());
            text.setX(imageView.getLayoutX() + 5.00);
            text.setY(imageView.getLayoutY());
            text.toFront();
            text.setFont(Font.font("Verdana", 12));
            text.setFill(Color.MAGENTA);
            group.getChildren().add(text);
        }

    }

}

private void setPlayerSet() {
    Platform.setImplicitExit(false);
    Scene scene = stage.getScene();
    ObservableList<Player> playersList = FXCollections.observableArrayList();
    playersList.addAll(GUIController.currentState.getPlayerSet().getPlayers());
    TableView<Player> playerTable = (TableView<Player>) scene.lookup("#players");
    TableColumn<Player, String> playersName = ((TableColumn<Player, String>) playerTable.getColumns().get(0));
    playersName.setCellValueFactory(new PropertyValueFactory<Player, String>("name"));
    TableColumn<Player, String> victoryPoints = ((TableColumn<Player, String>) playerTable.getColumns().get(1));
    victoryPoints.setCellValueFactory(new PropertyValueFactory<Player, String>("victoryPoints"));
    TableColumn<Player, String> coins = ((TableColumn<Player, String>) playerTable.getColumns().get(2));
    coins.setCellValueFactory(new PropertyValueFactory<Player, String>("coins"));
    TableColumn<Player, String> assistants = ((TableColumn<Player, String>) playerTable.getColumns().get(3));
    assistants.setCellValueFactory(new PropertyValueFactory<Player, String>("assistants"));
    TableColumn<Player, String> nobilityPoints = ((TableColumn<Player, String>) playerTable.getColumns().get(4));
    nobilityPoints.setCellValueFactory(new PropertyValueFactory<Player, String>("nobilityTrackPoints"));
    playerTable.setItems(playersList);
}

private void placeKing(ImageView king, List<ImageView> citiesImagesList) {
    for (ImageView imageView : citiesImagesList) {
        if (imageView.getId().toString().equals(GUIController.currentState.getKing().getPosition().getName())) {
            king.setX(imageView.getLayoutX());
            king.setY(imageView.getLayoutY());
        }
    }
}

@FXML
public void initialize() {
    self = this;
}

public void setStage(Stage stage) {
    this.stage = stage;
}

public void updateGUI(StartTurnState state) {
    GUIController.currentState = state;
    updateMap(); //here I should set my toggle breakpoint
}

private void updateMap() {
    Scene scene = stage.getScene();
    List<ImageView> citiesImagesList = new ArrayList<>();
    citiesImagesList.add((ImageView) scene.lookup("#A"));
    citiesImagesList.add((ImageView) scene.lookup("#B"));
    citiesImagesList.add((ImageView) scene.lookup("#C"));
    citiesImagesList.add((ImageView) scene.lookup("#D"));
    citiesImagesList.add((ImageView) scene.lookup("#E"));
    citiesImagesList.add((ImageView) scene.lookup("#F"));
    citiesImagesList.add((ImageView) scene.lookup("#G"));
    citiesImagesList.add((ImageView) scene.lookup("#H"));
    citiesImagesList.add((ImageView) scene.lookup("#I"));
    citiesImagesList.add((ImageView) scene.lookup("#J"));
    citiesImagesList.add((ImageView) scene.lookup("#K"));
    citiesImagesList.add((ImageView) scene.lookup("#L"));
    citiesImagesList.add((ImageView) scene.lookup("#M"));
    citiesImagesList.add((ImageView) scene.lookup("#N"));
    citiesImagesList.add((ImageView) scene.lookup("#O"));
    ImageView king = (ImageView) scene.lookup("#king");
    Group group = (Group) scene.lookup("#group");
    setPlayerSet(); //if I try setting here my toggle the thread doesent stop as the method updateGui() is never called
    placeKing(king, citiesImagesList);
    placeRewardToken(group, citiesImagesList);
} }

我希望你理解我的问题。如果你不明白,我会编辑我的问题。我在Stackoverflow上找不到这样的东西。

提前感谢您的帮助。

修改 我试图按照James_D的建议重写代码 这是我对GUI更新的新调用。

    @Override
public void visit(StartTurnState currentState) {
    guiDisplayer.setState(currentState);
    Platform.runLater(()-> guiDisplayer.run());
}

我还修改了GUIDisplayer类

public class GUIDisplayer extends Application implements Runnable{

private StartTurnState currentState;
private Stage stage;
static GUIController guiController;

public void start(Stage stage) throws Exception {
    GUIController controller = new GUIController();
    FXMLLoader fxmlLoader = new FXMLLoader();
    Parent root = fxmlLoader.load(getClass().getResource("sceneMap.fxml"));
    stage.setTitle("Council of Four");
    Scene newScene = new Scene(root);
    stage.setScene(newScene);
    stage.show();
    controller.setStage(stage);
    setController(controller);
}

public static void startGUI() {
    Application.launch();
}

public void setState(State state) {
    currentState = (StartTurnState) state;
}

public static void setController(GUIController controller) {
    guiController = controller;
}

@Override
public void run() {
    guiController.doUpdateGUI(currentState);

} }

runLater方法调用似乎被忽略了,也许我在这种情况下被javadoc描述

  

如果在JavaFX运行时关闭后调用此方法,则将忽略该调用:将不会执行Runnable并且不会抛出任何异常。

如果我删除了runLater并添加了切换括号,我仍然可以修改GUI。

1 个答案:

答案 0 :(得分:2)

在尝试从其他线程(javafx应用程序线程除外)更新gui时,您应该使用Paltform.runLater( () -> doUpdateGUI() ); 比你gui将适当更新。 检查javafx concurrency