我有一个Main
,Cell
和Displayer
类。Main 创建一个Cell[][]
网格,将其传递给构造Pane
s 并返回游戏Scene
的Displayer
。
我有一个deleteCells
按钮,可以在类Main
调用deleteCells
方法。但是,在我按下它之后,即使输出证明该方法已执行System.out.println
Scene
也不会更新:
单元格:
public class Cell {
private boolean status;
public Cell() {
System.out.println("DEAD CELL CREATED");
status = false;
}
public boolean getStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
}
主:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.util.Random;
public class Main extends Application {
private static int gameWidth= 800;
private static int gameHeight=600;
private static int gridSize = 20;
private static Cell[][] grid = new Cell[gridSize][gridSize];
private static Scene scene;
private static Displayer gameDisplayer;
@Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("Advanced Game of Life");
createGrid();
gameDisplayer = new Displayer(gameWidth, gameHeight, grid);
scene = gameDisplayer.getGameScene();
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
private static void createGrid() {
for (int i=0; i<gridSize; i++) {
for (int j=0; j<gridSize; j++)
grid[i][j] = new Cell();
}
}
public static void deleteCells() {
for (int i=0; i<gridSize; i++) {
for (int j=0; j<gridSize; j++) {
grid[i][j].setStatus(false);
}
}
//scene = gameDisplayer.getGameScene(); //this doesn't work
}
public static void createCell() {
Random rand = new Random();
}
}
播放器:
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
public class Displayer implements EventHandler<ActionEvent> {
private static Color ALIVE_COLOR=Color.GREEN;
private static Color DEAD_COLOR=Color.SILVER;;
private static BorderPane gamePane;
private static Pane cellsPane;
private HBox buttonsPane;
private Pane statsPane;
private Pane setupsPane;
private HBox bottomPane;
Button createCellButton;
Button deleteCellsButton;
Label cellsCountLabel;
Label setupsLabel;
private int gameWidth;
private int gameHeight;
static int gridSize = 20;
static int cellId = 1;
private Cell[][] gameGrid;
private Scene gameScene;
public Displayer(int width, int height, Cell[][] grid) {
gameWidth = width;
gameHeight = height;
gameGrid=grid;
createPanes();
createButtons();
createLabels();
setPaneStyles();
setButtonsStyles();
setLabelsStyles();
gameScene = new Scene(gamePane, gameWidth, gameHeight);
}
public Scene getGameScene() {
return gameScene;
}
private void createPanes() {
gamePane = new BorderPane();
buttonsPane = new HBox(5);
statsPane = new Pane();
cellsPane = makeGridPane(gameGrid);
setupsPane = new Pane();
bottomPane = new HBox(5);
}
private void createButtons() {
createCellButton = new Button();
deleteCellsButton = new Button();
}
private void createLabels() {
cellsCountLabel = new Label("Cells Count: " + (cellId + 1));
setupsLabel = new Label("Setups Label");
}
private void setPaneStyles() {...}
private void setButtonsStyles() {...}
private void setLabelsStyles() {...}
public void handle(ActionEvent event) {
if (event.getSource()==createCellButton) {
//Main.createCell();
}
else if (event.getSource() == deleteCellsButton) {
Main.deleteCells();
cellsCountLabel.setText("Cells Count: " + (cellId + 1));
System.out.println("Cells deleted");
}
else {
System.out.println("Unknown button");
}
}
public Pane makeGridPane(Cell[][] grid) {
Pane gridPane = new Pane();
for(int i=0; i<gridSize; i++){
for(int j=0; j<gridSize; j++){
Rectangle rect = new Rectangle();
System.out.println("grid[" + i + "][" + j +"]");
if (grid[i][j].getStatus()) {
rect.setFill(ALIVE_COLOR);
}
else {
rect.setFill(DEAD_COLOR);
}
rect.setStroke(Color.BLACK);
rect.setX(i * gridSize);
rect.setY(j * gridSize);
rect.setWidth(gridSize);
rect.setHeight(gridSize);
rect.setOnMouseClicked(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent me){
rect.setFill(Color.RED);
}
});
gridPane.getChildren().add(rect);
}
}
return gridPane;
}
}
有没有办法使Scene
本身更新,即使它是在Displayer
内部构造的,并且按钮调用Main
类中的方法?我尝试在方法中添加scene = gameDisplayer.getGameScene();
deleteCells()
但没有改变情况。我应该如何根据 MVC 处理用户的输入,以便Scene
响应更改,因为所有 GUI 元素都位于单独的类Displayer
?
编辑:
添加了editGrid()
方法以Main
:
public static void editGrid(int x, int y, boolean status) {
grid[x][y].setStatus(status);
}
更新Displayer
内部setOnMouseClicked
:
rect.setOnMouseClicked(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent me){
Main.editGrid ((int) rect.getX()/gridSize, (int) rect.getY()/gridSize, true);
}
});
在传统的MVC中,模型是"可观察的",从某种意义上说,观察者可以注册以接收数据更改的通知。视图(或控制器,具体取决于您使用的 MVC 模式的变体)观察模型中的数据,并在数据更改时相应地更新 UI 组件。
JavaFX通过定义属性和绑定API使做到这一点变得非常容易。这些属性类可以直接观察,在事件更改时触发事件以ChangeListener
,并且绑定 API 允许您表示变量之间的依赖关系。UI 组件本身是使用这些属性编写的。
因此,您可以将模型实现为
public class Cell {
private final BooleanProperty status;
public Cell() {
System.out.println("DEAD CELL CREATED");
status = new SimpleBooleanProperty(false);
}
public BooleanProperty statusProperty() {
return status ;
}
public final boolean getStatus() {
return statusProperty().get();
}
public final void setStatus(boolean status) {
statusProperty().set(status);
}
}
然后,您需要做的就是在相应单元格的状态更改时更新矩形的颜色:
public Pane makeGridPane(Cell[][] grid) {
Pane gridPane = new Pane();
for(int i=0; i<gridSize; i++){
for(int j=0; j<gridSize; j++){
Rectangle rect = new Rectangle();
System.out.println("grid[" + i + "][" + j +"]");
if (grid[i][j].getStatus()) {
rect.setFill(ALIVE_COLOR);
} else {
rect.setFill(DEAD_COLOR);
}
grid[i][j].statusProperty().addListener((obs, oldStatus, newStatus) -> {
if (newStatus) {
rect.setFill(ALIVE_COLOR);
} else {
rect.setFill(DEAD_COLOR);
}
});
rect.setStroke(Color.BLACK);
rect.setX(i * gridSize);
rect.setY(j * gridSize);
rect.setWidth(gridSize);
rect.setHeight(gridSize);
rect.setOnMouseClicked(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent me){
rect.setFill(Color.RED);
}
});
gridPane.getChildren().add(rect);
}
}
return gridPane;
}