如何在 javaFX 动画期间删除以前的样式



我正在制作一个生命游戏版本,但在删除应亡的单元格的背景颜色时遇到问题。


代码(摘录(:

public class Life extends Application {
private static int DIM = 32;
private Cell[][] cell = new Cell[DIM][DIM];
private boolean[][] nextState = new boolean[DIM][DIM];
private Timeline animation = new Timeline(new KeyFrame(Duration.millis(300), e -> step()));
private RadioButton rbLife = new RadioButton("Life");
private RadioButton rbHLife = new RadioButton("HighLife");
private Button step = new Button("Step");
private Button playStop = new Button("Play");
private Button clear = new Button("Clear");
private Slider slRate = new Slider();
private Label rate = new Label("Rate: ");

@Override
public void start(Stage primaryStage) {
GridPane pane = new GridPane();
pane.setStyle("-fx-background-color: black");
for (int i = 0; i < DIM; i++)
for (int j = 0; j < DIM; j++)
pane.add(cell[i][j] = new Cell(), j, i);
BorderPane borderPane = new BorderPane();
borderPane.setCenter(pane);
borderPane.setBottom(getHBox());
public void step() {
for (int i=0; i < DIM; i++) {
for (int j = 0; j < DIM; j++) {
computeNextState(i, j);
}
}
for (int i=0; i < DIM; i++) {
for (int j = 0; j < DIM; j++) {
cell[i][j].updateState(nextState[i][j], i, j);
}
}
}
public boolean[][] computeNextState(int row, int col) {
int liveCount = 0;
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++)
if (cell[(row + i + DIM) % DIM][(col + j + DIM) % DIM].isAlive())
liveCount++;
if (cell[row][col].isAlive())
liveCount--;
if (rbLife.isSelected()) {
if (liveCount == 3 || cell[row][col].isAlive() && liveCount == 2) {
nextState[row][col] = true;
}
else
nextState[row][col] = false;
}
return nextState;
}
public class Cell extends Pane {
private char token = ' ';
public Cell() {
setStyle("-fx-border-color: white; -fx-border-width: .17;");
this.setPrefSize(100, 100);
this.setOnMouseClicked(e -> handleMouseClick());
}
public boolean isAlive() {
if (token == 'a') {
return true;
}
else
return false;
}
public char getToken() {
return token;
}
public void setToken(char c) {
token = c;
}
private void handleMouseClick() {
setStyle("-fx-background-color: green");
setToken('a');
}
public void updateState(boolean state, int row, int col) {
if (state == true) {
setToken('a');
setStyle("-fx-background-color: green");
}
else
setToken(' ');
//cell[row][col].setBackground(Background.EMPTY);
}
}

在updateState((中,我尝试将背景设置为黑色,null,并使用getStyleClass((.clear((清除样式。

使用 setBackground(( 将背景设置为空会将它们重新涂成黑色,但随后单元格在下一代中无法重新涂成绿色。我在这里错过了什么?

我省略了我认为无关紧要的部分。感谢您的任何提示!

updateState()我尝试将背景设置为黑色,null,

这不起作用,因为 CSS 样式的优先级高于background属性。

getStyleClass().clear()清除样式。

这不起作用,因为它只是清除节点的样式类。不过,您使用的是内联样式。


在这两种情况下,您应该以相同的方式设置背景:

if (state) {
setToken('a');
setStyle("-fx-background-color: green");
} else {
setToken(' ');
setStyle("-fx-border-color: white; -fx-border-width: .17;"); // or null if you don't want to revert to the original style
}

或者假设您从传递给setStyle的所有参数中删除背景属性:

private static final FALSE_BACKGROUND = new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY));
private static final TRUE_BACKGROUND = new Background(new BackgroundFill(Color.GREEN, CornerRadii.EMPTY, Insets.EMPTY));
...
if (state) {
setToken('a');
setBackground(TRUE_BACKGROUND);
} else {
setToken(' ');
setBackground(FALSE_BACKGROUND);
}

最新更新