Modifications

This commit is contained in:
Lyanis SOUIDI 2023-04-28 17:34:42 +02:00
parent e2a5d20014
commit e428691761
Signed by: Lyanis SOUIDI
GPG Key ID: 251ADD56CFE6A854
7 changed files with 192 additions and 164 deletions

11
src/Editor.java Normal file
View File

@ -0,0 +1,11 @@
public class Editor {
private Grid gridModel;
public Editor(Grid gridModel) {
this.gridModel = gridModel;
}
public Grid getGrid() {
return this.gridModel;
}
}

140
src/EditorController.java Normal file
View File

@ -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());
}
}
}

22
src/EditorView.java Normal file
View File

@ -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;
}
}

View File

@ -1,6 +1,5 @@
public class Grid { public class Grid {
private Square[][] squares; private Square[][] squares;
private int squareSize;
private Thesee thesee = new Thesee(); private Thesee thesee = new Thesee();
public Grid(int size) { public Grid(int size) {

View File

@ -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 { 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) { public GridController(Grid model, GridView view) {
this.model = model; view.setGrid(model);
this.view = view; new TheseeController(model.getThesee(), 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 File

@ -1,14 +1,13 @@
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.MouseEvent;
public class GridView extends JPanel { public class GridView extends JPanel {
private Window window; public Window window;
private Grid model; protected Grid model;
private int gridSize; protected int gridSize;
private int gridStartX; protected int gridStartX;
private int gridStartY; protected int gridStartY;
private int squareSize; protected int squareSize;
private Font font; private Font font;
private final String exit = ""; private final String exit = "";
private final String thesee = "Θ"; private final String thesee = "Θ";
@ -19,26 +18,14 @@ public class GridView extends JPanel {
public GridView(Window window) { public GridView(Window window) {
super(); super();
this.window = window; this.window = window;
this.setOpaque(false);
this.setPreferredSize(new Dimension(700, 500));
} }
public void setGrid(Grid model) { public void setGrid(Grid model) {
this.model = 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() { private void calculateProportions() {
this.gridSize = Math.min((getHeight() - 50), getWidth()) - 10; this.gridSize = Math.min((getHeight() - 50), getWidth()) - 10;
this.gridStartX = (getWidth() - this.gridSize) / 2; this.gridStartX = (getWidth() - this.gridSize) / 2;

View File

@ -58,17 +58,18 @@ public class HomeView extends JPanel {
if (taille > 3 && taille < 21) { if (taille > 3 && taille < 21) {
String[] options = {"Remplir aléatoirement", "Partir d'une grille vide"}; 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]); 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) { switch (choix) {
case 0: case 0:
// afficher la grille aléatoirement // afficher la grille aléatoirement
grille.random(); editorController.random();
window.setContentPane(gridView); window.setContentPane(editorView);
window.validate(); window.validate();
break; break;
case 1: case 1:
window.setContentPane(gridView); window.setContentPane(editorView);
window.validate(); window.validate();
break; break;
default: default:
@ -109,9 +110,9 @@ public class HomeView extends JPanel {
if (choix == JFileChooser.APPROVE_OPTION) { if (choix == JFileChooser.APPROVE_OPTION) {
File fichier = fileChooser.getSelectedFile(); File fichier = fileChooser.getSelectedFile();
try { try {
GridView gridView = new GridView(window); EditorView editorView = new EditorView(window);
new GridController(FileManager.importGrid(fichier), gridView); new EditorController(new Editor(FileManager.importGrid(fichier)), editorView);
window.setContentPane(gridView); window.setContentPane(editorView);
window.validate(); window.validate();
} catch (Exception ex) { } catch (Exception ex) {
JOptionPane.showMessageDialog(panel, ex.getMessage(), "Erreur", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(panel, ex.getMessage(), "Erreur", JOptionPane.ERROR_MESSAGE);