From 25bde60d2ea189f4308a106fe47d41f478d0d55f Mon Sep 17 00:00:00 2001 From: arnaudlabourel <arnaud.labourel@univ-amu.fr> Date: Wed, 18 Oct 2023 15:57:17 +0200 Subject: [PATCH] update javafx --- build.gradle | 11 +- src/main/java/controller/Controller.java | 137 ++++++++++--------- src/main/java/model/GameOfLife.java | 2 - src/main/java/view/FillingMouseListener.java | 3 - src/main/java/view/MatrixPane.java | 7 +- src/main/java/view/MouseListener.java | 3 +- src/main/resources/view/view.fxml | 69 +++++----- src/test/java/model/GridTest.java | 4 + 8 files changed, 116 insertions(+), 120 deletions(-) diff --git a/build.gradle b/build.gradle index a8007cc..40e242e 100644 --- a/build.gradle +++ b/build.gradle @@ -13,9 +13,12 @@ repositories { } dependencies { - testImplementation('org.junit.jupiter:junit-jupiter-api:5.10.0', - 'org.assertj:assertj-core:3.24.2') - testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.0' + testRuntimeOnly("org.junit.platform:junit-platform-launcher") { + because("Only needed to run tests in a version of IntelliJ IDEA that bundles older versions") + } + testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine") + testImplementation("org.assertj:assertj-core:3.24.2") + testImplementation("org.junit.jupiter:junit-jupiter:5.10.0") } test { @@ -23,5 +26,5 @@ test { } application { - mainClassName = "GameOfLifeApplication" + mainClass.set("GameOfLifeApplication") } \ No newline at end of file diff --git a/src/main/java/controller/Controller.java b/src/main/java/controller/Controller.java index bc1809b..78234be 100644 --- a/src/main/java/controller/Controller.java +++ b/src/main/java/controller/Controller.java @@ -1,6 +1,7 @@ package controller; import javafx.fxml.FXML; +import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.ToggleButton; import javafx.scene.control.ToggleGroup; @@ -15,70 +16,74 @@ import static java.util.Objects.requireNonNull; */ public class Controller { - @FXML - private ToggleButton playToggleButton; - @FXML - private ToggleButton pauseToggleButton; - @FXML - private Label generationNumberLabel; - @FXML - private MatrixPane matrixPane; - - private GameOfLife gameOfLife; - - @FXML - private void initialize() { - initializePlayAndPauseToggleButtons(); - } - - private void initializePlayAndPauseToggleButtons() { - ToggleGroup toggleGroup = new PersistentToggleGroup(); - toggleGroup.getToggles().addAll(playToggleButton, pauseToggleButton); - pauseToggleButton.setSelected(true); - } - - - /** - * Sets {@link GameOfLife} instance. - * - * @param gameOfLife {@link GameOfLife} instance - * @throws NullPointerException if {@code gameOfLife} is {@code null} - */ - - public void setGameOfLife(GameOfLife gameOfLife) { - this.gameOfLife = requireNonNull(gameOfLife, "game of life is null"); - setGenerationNumberLabelTextProperty(); - initializeMatrixPane(); - } - - private void setGenerationNumberLabelTextProperty() { - generationNumberLabel.textProperty().bind(gameOfLife.generationNumberProperty().asString()); - } - - private void initializeMatrixPane() { - Grid grid = gameOfLife.getGrid(); - matrixPane.initialize(grid); - } - - @FXML - private void playToggleButtonAction() { - gameOfLife.play(); - } - - @FXML - private void pauseToggleButtonAction() { - gameOfLife.pause(); - } - - @FXML - private void resetButtonAction() { - gameOfLife.reset(); - pauseToggleButton.setSelected(true); - } - - @FXML - private void clearButtonAction() { - gameOfLife.clear(); - pauseToggleButton.setSelected(true); - } + @FXML + public Button clearButton; + @FXML + public Button resetButton; + @FXML + private ToggleButton playToggleButton; + @FXML + private ToggleButton pauseToggleButton; + @FXML + private Label generationNumberLabel; + @FXML + private MatrixPane matrixPane; + + private GameOfLife gameOfLife; + + @FXML + private void initialize() { + initializePlayAndPauseToggleButtons(); + } + + private void initializePlayAndPauseToggleButtons() { + ToggleGroup toggleGroup = new PersistentToggleGroup(); + toggleGroup.getToggles().addAll(playToggleButton, pauseToggleButton); + pauseToggleButton.setSelected(true); + } + + + /** + * Sets {@link GameOfLife} instance. + * + * @param gameOfLife {@link GameOfLife} instance + * @throws NullPointerException if {@code gameOfLife} is {@code null} + */ + + public void setGameOfLife(GameOfLife gameOfLife) { + this.gameOfLife = requireNonNull(gameOfLife, "game of life is null"); + setGenerationNumberLabelTextProperty(); + initializeMatrixPane(); + } + + private void setGenerationNumberLabelTextProperty() { + generationNumberLabel.textProperty().bind(gameOfLife.generationNumberProperty().asString()); + } + + private void initializeMatrixPane() { + Grid grid = gameOfLife.getGrid(); + matrixPane.initialize(grid); + } + + @FXML + private void playToggleButtonAction() { + gameOfLife.play(); + } + + @FXML + private void pauseToggleButtonAction() { + gameOfLife.pause(); + } + + @FXML + private void resetButtonAction() { + gameOfLife.reset(); + pauseToggleButton.setSelected(true); + } + + @FXML + private void clearButtonAction() { + gameOfLife.clear(); + pauseToggleButton.setSelected(true); + } } diff --git a/src/main/java/model/GameOfLife.java b/src/main/java/model/GameOfLife.java index 866d01a..bd6aa81 100644 --- a/src/main/java/model/GameOfLife.java +++ b/src/main/java/model/GameOfLife.java @@ -17,10 +17,8 @@ import static java.util.Objects.requireNonNull; * {@link GameOfLife} instances run <i>The Game of Life</i>. */ public class GameOfLife { - private final Random random = new Random(); private static final int PERIOD_IN_MILLISECONDS = 100; - private final Grid grid; private final ReadOnlyLongWrapper generationNumber = new ReadOnlyLongWrapper(); private Timeline timeline; diff --git a/src/main/java/view/FillingMouseListener.java b/src/main/java/view/FillingMouseListener.java index 7cd1efd..eaae6d8 100644 --- a/src/main/java/view/FillingMouseListener.java +++ b/src/main/java/view/FillingMouseListener.java @@ -15,13 +15,11 @@ public class FillingMouseListener implements MouseListener { @Override public void onMouseReleased(MouseEvent event, Cell cell) { - System.out.println("Filling Release"); this.matrix.resetWaitingListener(); } @Override public void onMouseEntered(MouseEvent event, Cell cell) { - System.out.println("Filling Enter"); if (!event.isPrimaryButtonDown()) { this.matrix.resetWaitingListener(); return; @@ -33,7 +31,6 @@ public class FillingMouseListener implements MouseListener { @Override public void onMousePressed(MouseEvent event, Cell cell) { - System.out.println("Filling Pressed"); cell.toggleState(); CellState state = cell.getState(); this.matrix.setMouseListener(new FillingMouseListener(this.matrix, state)); diff --git a/src/main/java/view/MatrixPane.java b/src/main/java/view/MatrixPane.java index e5c0713..f773a0c 100644 --- a/src/main/java/view/MatrixPane.java +++ b/src/main/java/view/MatrixPane.java @@ -46,10 +46,7 @@ public class MatrixPane extends GridPane{ ); cellRectangle.addEventHandler( MouseEvent.DRAG_DETECTED, - event -> { - System.out.println("Full drag start"); - this.startFullDrag(); - } + event -> this.startFullDrag() ); cellRectangle.addEventHandler( MouseDragEvent.MOUSE_DRAG_RELEASED, @@ -64,12 +61,10 @@ public class MatrixPane extends GridPane{ private MouseListener mouseListener = new WaitingMouseListener(this); void setMouseListener(MouseListener mouseListener) { - System.out.println("Change listener"); this.mouseListener = mouseListener; } void resetWaitingListener() { - System.out.println("Reset listener"); this.mouseListener = new WaitingMouseListener(this); } } diff --git a/src/main/java/view/MouseListener.java b/src/main/java/view/MouseListener.java index d227a11..abe5523 100644 --- a/src/main/java/view/MouseListener.java +++ b/src/main/java/view/MouseListener.java @@ -1,6 +1,5 @@ package view; -import javafx.event.EventType; import javafx.scene.input.MouseEvent; import model.Cell; @@ -8,6 +7,6 @@ interface MouseListener { default void onMousePressed(MouseEvent event, Cell cell) {} default void onMouseReleased(MouseEvent event, Cell cell) {} - default void onMouseEntered(MouseEvent event, Cell cell) {}; + default void onMouseEntered(MouseEvent event, Cell cell) {} } diff --git a/src/main/resources/view/view.fxml b/src/main/resources/view/view.fxml index fcd70d9..be77cca 100644 --- a/src/main/resources/view/view.fxml +++ b/src/main/resources/view/view.fxml @@ -6,7 +6,6 @@ <?import javafx.scene.control.Separator?> <?import javafx.scene.control.ToggleButton?> <?import javafx.scene.layout.AnchorPane?> -<?import javafx.scene.layout.GridPane?> <?import javafx.scene.layout.HBox?> <?import view.MatrixPane?> @@ -17,40 +16,36 @@ <padding> <Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/> </padding> - <children> - <HBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="24.0" - prefWidth="980.0" spacing="10.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" - AnchorPane.topAnchor="0.0"> - <children> - <Separator maxHeight="-Infinity" maxWidth="-Infinity" orientation="VERTICAL" - prefHeight="24.0" prefWidth="6.0"/> - <ToggleButton fx:id="playToggleButton" maxHeight="-Infinity" maxWidth="-Infinity" - mnemonicParsing="false" onAction="#playToggleButtonAction" prefHeight="24.0" - prefWidth="62.0" styleClass="button" text="Play"/> - <ToggleButton fx:id="pauseToggleButton" maxHeight="-Infinity" maxWidth="-Infinity" - mnemonicParsing="false" onAction="#pauseToggleButtonAction" prefHeight="24.0" - prefWidth="71.0" styleClass="button" text="Pause"/> - <Button fx:id="resetButton" maxHeight="-Infinity" maxWidth="-Infinity" - mnemonicParsing="false" onAction="#resetButtonAction" prefHeight="24.0" prefWidth="70.0" - text="Reset"/> - <Button fx:id="clearButton" maxHeight="-Infinity" maxWidth="-Infinity" - mnemonicParsing="false" onAction="#clearButtonAction" prefHeight="24.0" prefWidth="70.0" - text="Clear"/> - <Separator maxHeight="-Infinity" maxWidth="-Infinity" orientation="VERTICAL" - prefHeight="24.0" prefWidth="6.0"/> - <Separator maxHeight="-Infinity" maxWidth="-Infinity" orientation="VERTICAL" - prefHeight="24.0" prefWidth="6.0"/> - <Label maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="24.0" prefWidth="103.0" - text="Generation"/> - <Label fx:id="generationNumberLabel" alignment="CENTER_RIGHT" contentDisplay="TEXT_ONLY" - maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="24.0" prefWidth="99.0"/> - <Separator maxHeight="-Infinity" maxWidth="-Infinity" orientation="VERTICAL" - prefHeight="24.0" prefWidth="6.0"/> - </children> - </HBox> - <MatrixPane fx:id="matrixPane" alignment="CENTER" hgap="1.0" - maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="600.0" prefWidth="980.0" vgap="1.0" - AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" - AnchorPane.topAnchor="35.0"/> - </children> + <HBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="24.0" + prefWidth="980.0" spacing="10.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" + AnchorPane.topAnchor="0.0"> + <Separator maxHeight="-Infinity" maxWidth="-Infinity" orientation="VERTICAL" + prefHeight="24.0" prefWidth="6.0"/> + <ToggleButton fx:id="playToggleButton" maxHeight="-Infinity" maxWidth="-Infinity" + mnemonicParsing="false" onAction="#playToggleButtonAction" prefHeight="24.0" + prefWidth="62.0" styleClass="button" text="Play"/> + <ToggleButton fx:id="pauseToggleButton" maxHeight="-Infinity" maxWidth="-Infinity" + mnemonicParsing="false" onAction="#pauseToggleButtonAction" prefHeight="24.0" + prefWidth="71.0" styleClass="button" text="Pause"/> + <Button fx:id="resetButton" maxHeight="-Infinity" maxWidth="-Infinity" + mnemonicParsing="false" onAction="#resetButtonAction" prefHeight="24.0" prefWidth="70.0" + text="Reset"/> + <Button fx:id="clearButton" maxHeight="-Infinity" maxWidth="-Infinity" + mnemonicParsing="false" onAction="#clearButtonAction" prefHeight="24.0" prefWidth="70.0" + text="Clear"/> + <Separator maxHeight="-Infinity" maxWidth="-Infinity" orientation="VERTICAL" + prefHeight="24.0" prefWidth="6.0"/> + <Separator maxHeight="-Infinity" maxWidth="-Infinity" orientation="VERTICAL" + prefHeight="24.0" prefWidth="6.0"/> + <Label maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="24.0" prefWidth="103.0" + text="Generation"/> + <Label fx:id="generationNumberLabel" alignment="CENTER_RIGHT" contentDisplay="TEXT_ONLY" + maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="24.0" prefWidth="99.0"/> + <Separator maxHeight="-Infinity" maxWidth="-Infinity" orientation="VERTICAL" + prefHeight="24.0" prefWidth="6.0"/> + </HBox> + <MatrixPane fx:id="matrixPane" alignment="CENTER" hgap="1.0" + maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="600.0" prefWidth="980.0" vgap="1.0" + AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" + AnchorPane.topAnchor="35.0"/> </AnchorPane> diff --git a/src/test/java/model/GridTest.java b/src/test/java/model/GridTest.java index 56e1d97..c38c8ed 100644 --- a/src/test/java/model/GridTest.java +++ b/src/test/java/model/GridTest.java @@ -1,6 +1,7 @@ package model; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -14,6 +15,7 @@ public class GridTest { } @Test + @Disabled public void testGetNeighbours() { assertThat(grid.getNeighbors(1, 1)).isNotNull(); assertThat(grid.getNeighbors(1, 1)).hasSize(8); @@ -29,6 +31,7 @@ public class GridTest { } @Test + @Disabled public void testCountAliveNeighbours() { assertThat(grid.countAliveNeighbors(1, 1)).isEqualTo(0); grid.getCell(2, 2).setState(CellState.ALIVE); @@ -37,6 +40,7 @@ public class GridTest { } @Test + @Disabled public void testCalculateNextState() { grid.getCell(1, 0).setState(CellState.ALIVE); grid.getCell(1, 1).setState(CellState.ALIVE); -- GitLab