0

I'm trying to create a game Menu using javafx. I have now the front page that works. I have a couple of items that should lead me to the next scenes.

I use :

 private List<Pair<String, Runnable>> menuData = Arrays.asList(
        new Pair<String, Runnable>("Un Joueur", OptionMenu::),
        new Pair<String, Runnable>("Multijoueuer", () -> {}),
        new Pair<String, Runnable>("Options du jeu", () -> {}),
        new Pair<String, Runnable>("Quitter",Platform::exit)
);

to create my items and Platform::exit to quite everything.

The question is: How can I create a Runnable like my OptionMenu::something that leads me to my next page that extends Application. I would like it to be close to that :

public abstract static class OptionMenu extends Application implements Runnable{

///////// définition de la taille du menu ///////////
private static final int WIDTH = 1324;
private static final int HEIGHT = 604;

/////////////////////////////////////////////////////

//////// création des differents clicables //////////
private List<Pair<String, Runnable>> menuData = Arrays.asList(
        new Pair<String, Runnable>("Un Joueur", () -> {}),
        new Pair<String, Runnable>("Multijoueuer", () -> {}),
        new Pair<String, Runnable>("Options du jeu", () -> {}),
        new Pair<String, Runnable>("Quitter",Platform::exit)
);

//////////////////////////////////////////////////////

private Pane root = new Pane();
private VBox menuBox = new VBox(-5);


private void addBackground(){
    ImageView imageView = new ImageView(new Image(getClass().getResource("MenuImages/bkground.jpg").toExternalForm()));
    imageView.setFitWidth(WIDTH);
    imageView.setFitHeight(HEIGHT);

    root.getChildren().add(imageView);
}

private void addTitle() {
    BomberBallTitle title = new BomberBallTitle("Options du jeu");
    title.setTranslateX(WIDTH / 2 - title.getTitleWidth() / 2);
    title.setTranslateY(HEIGHT / 3);

    root.getChildren().add(title);
}


private void addMenu(double x, double y) {
    menuBox.setTranslateX(x);
    menuBox.setTranslateY(y);
    menuData.forEach(data -> {

        BomberBallMenuItem item = new BomberBallMenuItem(data.getKey());
        item.setOnAction(data.getValue());
        item.setTranslateX(-300);

        Rectangle clip = new Rectangle(300, 30);
        clip.translateXProperty().bind(item.translateXProperty().negate());

        item.setClip(clip);

        menuBox.getChildren().addAll(item);
    });

    root.getChildren().add(menuBox);
}

private Parent createContent() {
    addBackground();
    addTitle();
    double lineX = WIDTH / 2 - 100;
    double lineY = HEIGHT / 3 + 50;

    addMenu(lineX + 5, lineY + 5);

    return root;
}

public void run() {
    launch();
}



@Override
public void start(Stage primaryStage) throws Exception{
    Scene scene = new Scene(createContent());
    primaryStage.setTitle("BomberBall Menu");
    primaryStage.setScene(scene);
    primaryStage.show();
}
Dmi7ry
  • 1,777
  • 1
  • 13
  • 25
  • Perhaps take a look at this (somewhat) related question and links in its answer [Loading new fxml in the same scene](https://stackoverflow.com/questions/18619394/loading-new-fxml-in-the-same-scene). I think your question appears to be more about how to perform navigation and change scene content in a JavaFX application, rather than the specific thing it seems to ask regarding runnable pairs. – jewelsea Dec 06 '18 at 00:13
  • You can only have a single Application instance in an Application (read about the [Application lifecycle](https://docs.oracle.com/javase/8/javafx/api/javafx/application/Application.html) and the launch method documentation), so your menu shouldn't instantiate another class which extends Application. Also, a class that extends Application usually wouldn't be abstract. – jewelsea Dec 06 '18 at 00:14

0 Answers0