1

I've recently made a java swing application to play oxo and I'm trying to make it again in javaFX using the MVC design pattern. I'm strugling as to what should be handled in which part of my application.

How it looked in swing (see picture link)

The design in javaFX using fxml (see picture link)

The folder structure is (see picture link)

To make this abstract question more concrete: I handle the click in my controller but what do I do with it then? Do I handle my game logic there? Or should I just determine the source and send it up to my main app to do the rest?

https://i.stack.imgur.com/aSJ6F.jpg

Main:

package be.vincent_nagy.oxo;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
    FXMLLoader loader = new FXMLLoader(getClass().getResource("oxo.fxml"));

    Parent root = loader.load();
    Scene mainScene = new Scene(root,600,600);

    primaryStage.setTitle("OXO Spel");
    primaryStage.setScene(mainScene);

    primaryStage.show();

    //Hier geef ik de controller toegang tot de main app.
    OxoController controller = loader.getController();
    controller.setMain(this);

}


public static void main(String[] args) {
    launch(args);
}
}







Controller:
package be.vincent_nagy.oxo;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;

public class OxoController {

@FXML
private GridPane mainGrid;

@FXML
private Button button5;

@FXML
private Button button3;

@FXML
private Button button2;

@FXML
private Button button4;

@FXML
private Button button6;

@FXML
private Button button7;

@FXML
private Button button8;

@FXML
private Button button9;

@FXML
private Button button1;

//Referentie naar de main applicatie
private Main main;

//Constructor wordt opgeroepen voor de initialize() methode
public OxoController(){

}


// tot de @FXML properties van deze controller
@FXML
private void initialize(){

}

//Referentie naar de main applicatie. Aanroeping vanuit main
public void setMain(Main main){
    this.main = main;
}


@FXML
void handleButtonAction(ActionEvent event) {
    if(event.getSource() instanceof Button){
        Button src = (Button) event.getSource();

        //send the src up to main or handle the game code here?
    }
}


}






FXML:
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<GridPane fx:id="mainGrid" alignment="center" gridLinesVisible="true" minHeight="600.0" minWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="be.vincent_nagy.oxo.OxoController">
   <rowConstraints>
      <RowConstraints maxHeight="200.0" minHeight="200.0" prefHeight="200.0" />
      <RowConstraints maxHeight="200.0" minHeight="200.0" prefHeight="200.0" />
      <RowConstraints maxHeight="200.0" minHeight="200.0" prefHeight="200.0" />
   </rowConstraints>
   <columnConstraints>
      <ColumnConstraints maxWidth="200.0" minWidth="200.0" prefWidth="200.0" />
      <ColumnConstraints maxWidth="200.0" minWidth="200.0" prefWidth="200.0" />
      <ColumnConstraints maxWidth="200.0" minWidth="200.0" prefWidth="200.0" />
   </columnConstraints>
   <children>
      <Button fx:id="button5" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
      <Button fx:id="button3" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2" />
      <Button fx:id="button2" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" />
      <Button fx:id="button4" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="1" />
      <Button fx:id="button6" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="1" />
      <Button fx:id="button7" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="2" />
      <Button fx:id="button8" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="2" />
      <Button fx:id="button9" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="2" />
      <Button fx:id="button1" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="200.0" prefWidth="200.0" />
   </children>
</GridPane>

https://pastebin.com/dgfQJiEK

Vinagy
  • 133
  • 2
  • 11
  • You need to wait for the specialist @James_D – Yahya Jun 06 '17 at 18:18
  • 1
    The question is really too broad, and perhaps too opinion based, but the main observations I have are that: 1. your `Main` class should be solely concerned with application lifecycle (starting and stopping the application): it really shouldn't do anything other than initialize the first view, the model, and tie the pieces together, and if needed release any resources in the `stop()` method. It really has nothing to do with the MVC structure at all. 2. You have an FXML file (view) and a controller class (controller), and you mention **M**VC. Where is the model? That's the most important part. – James_D Jun 06 '17 at 19:21
  • See https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx for an example of MVC. But note this is a very loosely-defined pattern: there are many different variants. You should read https://martinfowler.com/eaaDev/uiArchs.html for a pretty good overview of MVC/MVP/MVVM and others. – James_D Jun 06 '17 at 19:24
  • Most of your logic will be in handleButtonAction do to the type of game this is and the way your have it setup. You will need something to keep up with whose turn it is(x or o) and change the turn after a button press. You will also have to check the game state for win/lose/draw after a button press. – SedJ601 Jun 06 '17 at 19:28
  • So if I understand it correctly I can place all logic in controllers and the main app will only be used to hook all controlers together? – Vinagy Jun 07 '17 at 05:21

0 Answers0