From 21e2bdb16329a67b767fe656a14767c9717cd358 Mon Sep 17 00:00:00 2001 From: Lyanis Souidi Date: Fri, 28 Apr 2023 22:14:21 +0200 Subject: [PATCH] Ajout de classes pour l'affichage manuel de la simulation --- src/Algo.java | 17 +++++++ src/ManualSimulationController.java | 70 +++++++++++++++++++++++++++++ src/ManualSimulationView.java | 35 +++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 src/Algo.java create mode 100644 src/ManualSimulationController.java create mode 100644 src/ManualSimulationView.java diff --git a/src/Algo.java b/src/Algo.java new file mode 100644 index 0000000..8258902 --- /dev/null +++ b/src/Algo.java @@ -0,0 +1,17 @@ +/** + * Interface for the algorithms + * @version 1.0 + * @author Amir Daouadi + * @author Lyanis Souidi + */ +public interface Algo { + /** + * Makes the next move of the algorithm + */ + void nextMove(); + + /** + * Resets the algorithm + */ + void reset(); +} diff --git a/src/ManualSimulationController.java b/src/ManualSimulationController.java new file mode 100644 index 0000000..0944b69 --- /dev/null +++ b/src/ManualSimulationController.java @@ -0,0 +1,70 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +/** + * Controller for the manual simulation view. + * @version 1.0 + * @author Amir Daouadi + * @author Lyanis Souidi + */ +public class ManualSimulationController { + /** + * The simulation model + */ + private Simulation model; + /** + * The manual simulation view + */ + private ManualSimulationView view; + /** + * The grid model + */ + private Grid grid; + /** + * The algorithm used for the simulation + */ + private Algo algo; + /** + * The restart button + */ + private Button restartButton = new Button("Recommencer"); + /** + * The next button + */ + private Button nextButton = new Button("Coup suivant"); + + /** + * Constructor + * @param model The simulation model + * @param view The manual simulation view + * @param algo The algorithm used for the simulation + */ + public ManualSimulationController(Simulation model, ManualSimulationView view, Algo algo) { + this.model = model; + this.view = view; + this.algo = algo; + + JPanel buttons = new JPanel(); + + restartButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + algo.reset(); + } + }); + buttons.add(restartButton); + + nextButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + algo.nextMove(); + if (model.isSuccess()) { + nextButton.setEnabled(false); + } + } + }); + buttons.add(nextButton); + + this.view.add(buttons, BorderLayout.NORTH); + } +} diff --git a/src/ManualSimulationView.java b/src/ManualSimulationView.java new file mode 100644 index 0000000..f4e25f0 --- /dev/null +++ b/src/ManualSimulationView.java @@ -0,0 +1,35 @@ +import java.awt.*; + +/** + * The manual simulation view + * @version 1.0 + * @author Amir Daouadi + * @author Lyanis Souidi + */ +public class ManualSimulationView extends GridView { + /** + * The simulation model + */ + private Simulation model; + + /** + * Constructor + * @param window The window + * @param model The simulation model + */ + public ManualSimulationView(Window window, Simulation model) { + super(window); + } + + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + + g.setColor(Color.BLACK); + g.setFont(new Font("Arial", Font.PLAIN, 20)); + FontMetrics metrics = g.getFontMetrics(g.getFont()); + + String movesStr = "Coups : " + this.model.getMoves(); + g.drawString(movesStr, 5, 5); + } +}