Witam
Siedze dosyc krótko w JavaFX i o ile wiem jak to zrobic w JavaSwing to nie wiem jak to wykonac w FX. Chodzi o to ze w klasie main wyswietla mi sie okno do podania danych i po kliknieciu accept powinien odpalic sie kod z Classy Board.
package sample;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Spinner;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Main extends Application {
private Scene scene;
public static Stage primaryStage;
Board board = new Board();
public static int x;
public static int y;
private Parent createContent() {
GridPane lobby = new GridPane();
lobby.setHgap(2);
lobby.setVgap(2);
lobby.setPadding(new Insets(10, 20, 10, 20));
Text row = new Text("Row");
lobby.add(row, 0, 0);
Text column = new Text("Column");
lobby.add(column, 0, 1);
Spinner rowspinner = new Spinner(10, 100, 1);
lobby.add(rowspinner, 1, 0);
Spinner columnspinner = new Spinner(10, 100, 1);
lobby.add(columnspinner, 1, 1);
Button accept = new Button("Accept");
lobby.add(accept, 1, 2);
accept.setOnAction(__ ->
{
x = (int)rowspinner.getValue();
y = (int)columnspinner.getValue();
try {
board.start(Board.boardStage);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("jestem tu");
});
return lobby;
}
public void restart(Stage x){
Platform.runLater( () -> {
try {
new Main().start(new Stage());
} catch (Exception e) {
e.printStackTrace();
}
});
}
@Override
public void start(Stage stage) throws Exception {
primaryStage = stage;
scene = new Scene(createContent());
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Class Board
package sample;
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.List;
public class Board {
private static final int TILE_SIZE = 40;
public static final int W = Main.x * TILE_SIZE;
private static final int H = Main.y * TILE_SIZE;
private static final int X_TILES = W / TILE_SIZE;
private static final int Y_TILES = H / TILE_SIZE;
private Tile[][] grid = new Tile[X_TILES][Y_TILES];
private Scene scene;
public static Stage boardStage;
//Main main = new Main();
public Parent createContent() {
Pane root = new Pane();
root.setPrefSize(W, H);
for (int y = 0; y < Y_TILES; y++) {
for (int x = 0; x < X_TILES; x++) {
Tile tile = new Tile(x, y, Math.random() < 0.1);
grid[x][y] = tile;
root.getChildren().add(tile);
}
}
for (int y = 0; y < Y_TILES; y++) {
for (int x = 0; x < X_TILES; x++) {
Tile tile = grid[x][y];
if (tile.hasBomb)
continue;
long bombs = getNeighbors(tile).stream().filter(t -> t.hasBomb).count();
if (bombs > 0)
tile.text.setText(String.valueOf(bombs));
}
}
return root;
}
private List<Tile> getNeighbors(Tile tile) {
List<Tile> neighbors = new ArrayList<>();
int[] points = new int[] {
-1, -1,
-1, 0,
-1, 1,
0, -1,
0, 1,
1, -1,
1, 0,
1, 1
};
for (int i = 0; i < points.length; i++) {
int dx = points[i];
int dy = points[++i];
int newX = tile.x + dx;
int newY = tile.y + dy;
if (newX >= 0 && newX < X_TILES
&& newY >= 0 && newY < Y_TILES) {
neighbors.add(grid[newX][newY]);
}
}
return neighbors;
}
private class Tile extends StackPane {
private int x, y;
private boolean hasBomb;
private boolean isOpen = false;
private Rectangle border = new Rectangle(TILE_SIZE - 2, TILE_SIZE - 2);
private Text text = new Text();
public Tile(int x, int y, boolean hasBomb) {
this.x = x;
this.y = y;
this.hasBomb = hasBomb;
border.setStroke(Color.LIGHTGRAY);
text.setFont(Font.font(18));
text.setText(hasBomb ? "X" : "");
text.setVisible(false);
getChildren().addAll(border, text);
setTranslateX(x * TILE_SIZE);
setTranslateY(y * TILE_SIZE);
setOnMouseClicked(e -> open());
}
public void open() {
if (isOpen)
return;
if (hasBomb) {
System.out.println("Game Over");
//main.restart(boardStage);
return;
}
isOpen = true;
text.setVisible(true);
border.setFill(null);
if (text.getText().isEmpty()) {
getNeighbors(this).forEach(Tile::open);
}
}
}
public void start(Stage stage) throws Exception {
boardStage = stage;
scene = new Scene(createContent());
stage.setScene(scene);
stage.show();
}
}