From e42869176189371bcb9cc0e427ee1c366c84b5b6 Mon Sep 17 00:00:00 2001 From: Lyanis Souidi Date: Fri, 28 Apr 2023 17:34:42 +0200 Subject: [PATCH] Modifications --- src/Editor.java | 11 +++ src/EditorController.java | 140 ++++++++++++++++++++++++++++++++++++++ src/EditorView.java | 22 ++++++ src/Grid.java | 1 - src/GridController.java | 136 +----------------------------------- src/GridView.java | 29 +++----- src/HomeView.java | 17 ++--- 7 files changed, 192 insertions(+), 164 deletions(-) create mode 100644 src/Editor.java create mode 100644 src/EditorController.java create mode 100644 src/EditorView.java diff --git a/src/Editor.java b/src/Editor.java new file mode 100644 index 0000000..3af53ea --- /dev/null +++ b/src/Editor.java @@ -0,0 +1,11 @@ +public class Editor { + private Grid gridModel; + + public Editor(Grid gridModel) { + this.gridModel = gridModel; + } + + public Grid getGrid() { + return this.gridModel; + } +} diff --git a/src/EditorController.java b/src/EditorController.java new file mode 100644 index 0000000..9103fa0 --- /dev/null +++ b/src/EditorController.java @@ -0,0 +1,140 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.Random; + +public class EditorController extends GridController { + private Editor model; + private EditorView view; + private enum Mode { DISABLED, WALL, THESEE, EXIT } + private Mode editMode = Mode.DISABLED; + private JButton editTheseeButton = new JButton("Placer Joueur"); + private JButton editExitButton = new JButton("Placer Sortie"); + private JButton editWallButton = new JButton("Enlever/Ajouter Murs"); + + public EditorController(Editor model, EditorView view) { + super(model.getGrid(), view); + this.model = model; + this.view = view; + + this.view.addMouseListener(new MouseAdapter() { + public void mouseClicked(MouseEvent e) { + edit(view.click(e)); + } + }); + + JPanel buttons = new JPanel(); + + editWallButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (editMode == Mode.DISABLED) { + editWallButton.setText("Mode Dessin"); + setEditMode(Mode.WALL); + } else { + editWallButton.setText("Enlever/Ajouter Murs"); + setEditMode(Mode.DISABLED); + } + } + }); + buttons.add(editWallButton); + + editTheseeButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (editMode == Mode.DISABLED) { + editTheseeButton.setText("Mode Dessin"); + setEditMode(Mode.THESEE); + } else { + editTheseeButton.setText("Placer Joueur"); + setEditMode(Mode.DISABLED); + } + } + }); + buttons.add(editTheseeButton); + + editExitButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (editMode == Mode.DISABLED) { + editExitButton.setText("Mode Dessin"); + setEditMode(Mode.EXIT); + } else { + editExitButton.setText("Placer Sortie"); + setEditMode(Mode.DISABLED); + } + } + }); + buttons.add(editExitButton); + this.view.add(buttons, BorderLayout.NORTH); + } + + private void edit(Square square) { + if (square != null) { + if (this.editMode == Mode.WALL) { + if (square.isWall()) { + square.setEmpty(); + } else { + try { + square.setWall(); + } catch (Exception ex) { + JOptionPane.showMessageDialog(this.view, ex.getMessage(), "Erreur", JOptionPane.ERROR_MESSAGE); + } + } + this.view.repaint(); + } else if (this.editMode == Mode.THESEE) { + try { + this.model.getGrid().getThesee().setSquare(square); + } catch (Exception ex) { + JOptionPane.showMessageDialog(this.view, ex.getMessage(), "Erreur", JOptionPane.ERROR_MESSAGE); + } + this.view.repaint(); + } else if (this.editMode == Mode.EXIT) { + try { + square.setExit(); + } catch (Exception ex) { + JOptionPane.showMessageDialog(this.view, ex.getMessage(), "Erreur", JOptionPane.ERROR_MESSAGE); + } + this.view.repaint(); + } + } + } + + /** + * Sets the edit mode. + * @param mode The mode to set. + */ + private void setEditMode(Mode mode) { + this.editMode = mode; + if (mode != Mode.DISABLED) { + this.editTheseeButton.setEnabled(mode == Mode.THESEE); + this.editExitButton.setEnabled(mode == Mode.EXIT); + this.editWallButton.setEnabled(mode == Mode.WALL); + } else { + this.editTheseeButton.setEnabled(true); + this.editExitButton.setEnabled(true); + this.editWallButton.setEnabled(true); + } + } + + /** + * Randomly fills the grid with an exit, Thésée and walls. + */ + public void random() { + try { + Random rand = new Random(); + Grid gridModel = this.model.getGrid(); + + gridModel.getSquare(rand.nextInt(gridModel.getSize()), rand.nextInt(gridModel.getSize())).setExit(); + gridModel.getThesee().setSquare(gridModel.getSquare(rand.nextInt(gridModel.getSize()), rand.nextInt(gridModel.getSize()))); + + for (int i = 0; i < gridModel.getSize(); i++) { + for (int j = 0; j < gridModel.getSize(); j++) { + if (!gridModel.getSquare(i, j).isExit() && !gridModel.getSquare(i, j).isThesee() && rand.nextInt(3) == 0) gridModel.getSquare(i, j).setWall(); + } + } + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } +} diff --git a/src/EditorView.java b/src/EditorView.java new file mode 100644 index 0000000..479fd41 --- /dev/null +++ b/src/EditorView.java @@ -0,0 +1,22 @@ +import java.awt.*; +import java.awt.event.MouseEvent; + +public class EditorView extends GridView { + public EditorView(Window window) { + super(window); + this.setBackground(new Color(193, 190, 180)); + } + public Square click(MouseEvent e) { + if ((e.getX() < this.gridStartX) || (e.getX() > (this.gridStartX + this.gridSize)) || (e.getY() < this.gridStartY) || (e.getY() > (this.gridStartY + this.gridSize))) return null; + int x = (e.getX() - this.gridStartX) / this.squareSize; + int y = (e.getY() - this.gridStartY) / this.squareSize; + if ((x >= 0) && (x < this.model.getSize()) && (y >= 0) && (y < this.model.getSize())) { + try { + return this.model.getSquare(y, x); + } catch (Exception ex) { + return null; + } + } + return null; + } +} diff --git a/src/Grid.java b/src/Grid.java index 62fc904..f85faf0 100644 --- a/src/Grid.java +++ b/src/Grid.java @@ -1,6 +1,5 @@ public class Grid { private Square[][] squares; - private int squareSize; private Thesee thesee = new Thesee(); public Grid(int size) { diff --git a/src/GridController.java b/src/GridController.java index 2f0b046..46d5ddb 100644 --- a/src/GridController.java +++ b/src/GridController.java @@ -1,138 +1,6 @@ -import javax.swing.*; -import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.util.Random; - public class GridController { - private Grid model; - private GridView view; - private enum Mode { DISABLED, WALL, THESEE, EXIT } - private Mode editMode = Mode.DISABLED; - private JButton editTheseeButton; - private JButton editExitButton; - private JButton editWallButton; - public GridController(Grid model, GridView view) { - this.model = model; - this.view = view; - - this.view.setGrid(this.model); - - TheseeController theseeController = new TheseeController(this.model.getThesee(), this.view); - - this.view.setPreferredSize(new Dimension(700, 500)); - this.view.setBackground(new Color(193, 190, 180)); - - this.view.addMouseListener(new MouseAdapter() { - public void mouseClicked(MouseEvent e) { - Square square = view.click(e); - - if (square != null) { - if (editMode == Mode.WALL) { - if (square.isWall()) { - square.setEmpty(); - } else { - try { - square.setWall(); - } catch (Exception ex) { - JOptionPane.showMessageDialog(view, ex.getMessage(), "Erreur", JOptionPane.ERROR_MESSAGE); - } - } - view.repaint(); - } else if (editMode == Mode.THESEE) { - try { - model.getThesee().setSquare(square); - } catch (Exception ex) { - JOptionPane.showMessageDialog(view, ex.getMessage(), "Erreur", JOptionPane.ERROR_MESSAGE); - } - view.repaint(); - } else if (editMode == Mode.EXIT) { - try { - square.setExit(); - } catch (Exception ex) { - JOptionPane.showMessageDialog(view, ex.getMessage(), "Erreur", JOptionPane.ERROR_MESSAGE); - } - view.repaint(); - } - } - } - }); - - editWallButton = new JButton("Enlever/Ajouter Murs"); - editWallButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - if (editMode == Mode.DISABLED) { - editMode = Mode.WALL; - editWallButton.setText("Mode Dessin"); - editTheseeButton.setEnabled(false); - editExitButton.setEnabled(false); - } else { - editMode = Mode.DISABLED; - editWallButton.setText("Enlever/Ajouter Murs"); - editTheseeButton.setEnabled(true); - editExitButton.setEnabled(true); - } - } - }); - this.view.add(editWallButton); - - editTheseeButton = new JButton("Placer Joueur"); - editTheseeButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - if (editMode == Mode.DISABLED) { - editMode = Mode.THESEE; - editTheseeButton.setText("Mode Dessin"); - editWallButton.setEnabled(false); - editExitButton.setEnabled(false); - } else { - editMode = Mode.DISABLED; - editTheseeButton.setText("Placer Joueur"); - editWallButton.setEnabled(true); - editExitButton.setEnabled(true); - } - } - }); - this.view.add(editTheseeButton); - - editExitButton = new JButton("Placer Sortie"); - editExitButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - if (editMode == Mode.DISABLED) { - editMode = Mode.EXIT; - editExitButton.setText("Mode Dessin"); - editWallButton.setEnabled(false); - editTheseeButton.setEnabled(false); - } else { - editMode = Mode.DISABLED; - editExitButton.setText("Placer Sortie"); - editWallButton.setEnabled(true); - editTheseeButton.setEnabled(true); - } - } - }); - this.view.add(editExitButton); - } - - /** - * Randomly fills the grid with walls, an exit and Thésée - */ - public void random() { - try { - Random rand = new Random(); - for (int i = 0; i < this.model.getSize(); i++) { - for (int j = 0; j < this.model.getSize(); j++) { - if (rand.nextInt(3) == 0) this.model.getSquare(i, j).setWall(); - } - } - - this.model.getSquare(rand.nextInt(this.model.getSize()), rand.nextInt(this.model.getSize())).setExit(); - - this.model.getThesee().setSquare(this.model.getSquare(rand.nextInt(this.model.getSize()), rand.nextInt(this.model.getSize()))); - } catch (Exception e) { - System.out.println(e.getMessage()); - } + view.setGrid(model); + new TheseeController(model.getThesee(), view); } } diff --git a/src/GridView.java b/src/GridView.java index f49651b..4ab9adc 100644 --- a/src/GridView.java +++ b/src/GridView.java @@ -1,14 +1,13 @@ import javax.swing.*; import java.awt.*; -import java.awt.event.MouseEvent; public class GridView extends JPanel { - private Window window; - private Grid model; - private int gridSize; - private int gridStartX; - private int gridStartY; - private int squareSize; + public Window window; + protected Grid model; + protected int gridSize; + protected int gridStartX; + protected int gridStartY; + protected int squareSize; private Font font; private final String exit = "∩"; private final String thesee = "Θ"; @@ -19,26 +18,14 @@ public class GridView extends JPanel { public GridView(Window window) { super(); this.window = window; + this.setOpaque(false); + this.setPreferredSize(new Dimension(700, 500)); } public void setGrid(Grid model) { this.model = model; } - public Square click(MouseEvent e) { - if ((e.getX() < this.gridStartX) || (e.getX() > (this.gridStartX + this.gridSize)) || (e.getY() < this.gridStartY) || (e.getY() > (this.gridStartY + this.gridSize))) return null; - int x = (e.getX() - this.gridStartX) / this.squareSize; - int y = (e.getY() - this.gridStartY) / this.squareSize; - if ((x >= 0) && (x < this.model.getSize()) && (y >= 0) && (y < this.model.getSize())) { - try { - return this.model.getSquare(y, x); - } catch (Exception ex) { - return null; - } - } - return null; - } - private void calculateProportions() { this.gridSize = Math.min((getHeight() - 50), getWidth()) - 10; this.gridStartX = (getWidth() - this.gridSize) / 2; diff --git a/src/HomeView.java b/src/HomeView.java index 83253e5..f7c0a42 100644 --- a/src/HomeView.java +++ b/src/HomeView.java @@ -58,17 +58,18 @@ public class HomeView extends JPanel { if (taille > 3 && taille < 21) { String[] options = {"Remplir aléatoirement", "Partir d'une grille vide"}; int choix = JOptionPane.showOptionDialog(panel, "Choisissez comment remplir la grille :", "Remplissage de la grille", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]); - GridView gridView = new GridView(window); - GridController grille = new GridController(new Grid(taille), gridView); + + EditorView editorView = new EditorView(window); + EditorController editorController = new EditorController(new Editor(new Grid(taille)), editorView); switch (choix) { case 0: // afficher la grille aléatoirement - grille.random(); - window.setContentPane(gridView); + editorController.random(); + window.setContentPane(editorView); window.validate(); break; case 1: - window.setContentPane(gridView); + window.setContentPane(editorView); window.validate(); break; default: @@ -109,9 +110,9 @@ public class HomeView extends JPanel { if (choix == JFileChooser.APPROVE_OPTION) { File fichier = fileChooser.getSelectedFile(); try { - GridView gridView = new GridView(window); - new GridController(FileManager.importGrid(fichier), gridView); - window.setContentPane(gridView); + EditorView editorView = new EditorView(window); + new EditorController(new Editor(FileManager.importGrid(fichier)), editorView); + window.setContentPane(editorView); window.validate(); } catch (Exception ex) { JOptionPane.showMessageDialog(panel, ex.getMessage(), "Erreur", JOptionPane.ERROR_MESSAGE);