From e8a2d783797916ae3b96b76380b01b1c31d796d7 Mon Sep 17 00:00:00 2001 From: Justine Yannis Date: Mon, 17 Oct 2022 18:28:55 +0200 Subject: [PATCH] =?UTF-8?q?S=C3=A9paration=20listener=20et=20vue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- projetAgile/Makefile | 14 +++++++---- .../Controller/GrilleMouseListener.java | 4 +-- .../Controller/ModelEventListener.java | 25 +++++++++++++++++++ .../fr/iutfbleau/projetAgile/View/Grille.java | 16 +++--------- .../projetAgile/View/Puissance4Panel.java | 4 +++ 5 files changed, 42 insertions(+), 21 deletions(-) create mode 100644 projetAgile/src/fr/iutfbleau/projetAgile/Controller/ModelEventListener.java diff --git a/projetAgile/Makefile b/projetAgile/Makefile index 550fa7e..e76d48d 100644 --- a/projetAgile/Makefile +++ b/projetAgile/Makefile @@ -19,8 +19,8 @@ Events : build/$(PACKAGE_PATH)/Controller/EventListener/GridChangedListener.clas Model : build/$(PACKAGE_PATH)/Model/GrilleModel.class -Controller : build/$(PACKAGE_PATH)/Controller/GrilleMouseListener.class - +Controller : build/$(PACKAGE_PATH)/Controller/GrilleMouseListener.class \ + build/$(PACKAGE_PATH)/Controller/ModelEventListener.class #------- Events ------ build/$(PACKAGE_PATH)/Controller/EventListener/PlayerEvent.class : src/$(PACKAGE_PATH)/Controller/EventListener/PlayerEvent.java @@ -47,6 +47,13 @@ build/$(PACKAGE_PATH)/Controller/GrilleMouseListener.class : src/$(PACKAGE_PATH) build/$(PACKAGE_PATH)/Model/GrilleModel.class javac $(JAVAC_OPT) $< +build/$(PACKAGE_PATH)/Controller/ModelEventListener.class : src/$(PACKAGE_PATH)/Controller/ModelEventListener.java \ + build/$(PACKAGE_PATH)/Controller/EventListener/GridChangedListener.class \ + build/$(PACKAGE_PATH)/Controller/EventListener/GridEvent.class \ + build/$(PACKAGE_PATH)/Controller/EventListener/PlayerEvent.class \ + build/$(PACKAGE_PATH)/View/Grille.class + javac $(JAVAC_OPT) $< + #------- Modele ------ build/$(PACKAGE_PATH)/Model/GrilleModel.class : src/$(PACKAGE_PATH)/Model/GrilleModel.java \ @@ -66,9 +73,6 @@ build/$(PACKAGE_PATH)/View/Pion.class : src/$(PACKAGE_PATH)/View/Pion.java \ build/$(PACKAGE_PATH)/View/Grille.class : src/$(PACKAGE_PATH)/View/Grille.java \ build/$(PACKAGE_PATH)/View/Pion.class \ - build/$(PACKAGE_PATH)/Controller/EventListener/GridChangedListener.class \ - build/$(PACKAGE_PATH)/Controller/EventListener/GridEvent.class \ - build/$(PACKAGE_PATH)/Controller/EventListener/PlayerEvent.class \ build/$(PACKAGE_PATH)/Utils/Constants.class javac $(JAVAC_OPT) $< diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/Controller/GrilleMouseListener.java b/projetAgile/src/fr/iutfbleau/projetAgile/Controller/GrilleMouseListener.java index 091cd28..6c6332c 100644 --- a/projetAgile/src/fr/iutfbleau/projetAgile/Controller/GrilleMouseListener.java +++ b/projetAgile/src/fr/iutfbleau/projetAgile/Controller/GrilleMouseListener.java @@ -14,13 +14,11 @@ public class GrilleMouseListener extends MouseInputAdapter{ public GrilleMouseListener(Grille grille, GrilleModel modele) { this.grille = grille; this.modele = modele; - this.grille.addMouseListener(this); - this.modele.addGridListener(grille); this.grille.init(this.modele.getTab()); } @Override - public void mouseClicked(MouseEvent e) { + public void mousePressed(MouseEvent e) { if(e.getButton() == MouseEvent.BUTTON1) { if(this.modele.getGameStatus() == PLAYING) { int column = (e.getX() * this.modele.getColumn() / grille.getWidth()); diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/Controller/ModelEventListener.java b/projetAgile/src/fr/iutfbleau/projetAgile/Controller/ModelEventListener.java new file mode 100644 index 0000000..667bbea --- /dev/null +++ b/projetAgile/src/fr/iutfbleau/projetAgile/Controller/ModelEventListener.java @@ -0,0 +1,25 @@ +package fr.iutfbleau.projetAgile.Controller; + +import fr.iutfbleau.projetAgile.Controller.EventListener.*; +import fr.iutfbleau.projetAgile.Utils.Constants; +import fr.iutfbleau.projetAgile.View.Grille; + +public class ModelEventListener implements GridChangedListener { + + private Grille grille; + + public ModelEventListener(Grille g) { + this.grille = g; + } + + @Override + public void gridChanged(GridEvent e) { + this.grille.addPlayerPiece(e.getColumn(), e.getRow(), e.getPlayer()); + } + + @Override + public void playerChanged(PlayerEvent e) { + System.out.println("Tour du joueur : " + (e.getNewPlayer() == Constants.PLAYER_ONE ? "Rouge" : "Jaune")); + } + +} diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/View/Grille.java b/projetAgile/src/fr/iutfbleau/projetAgile/View/Grille.java index 6411a6c..0ff1557 100644 --- a/projetAgile/src/fr/iutfbleau/projetAgile/View/Grille.java +++ b/projetAgile/src/fr/iutfbleau/projetAgile/View/Grille.java @@ -5,7 +5,7 @@ import java.awt.*; import fr.iutfbleau.projetAgile.Controller.EventListener.*; import fr.iutfbleau.projetAgile.Utils.Constants; -public class Grille extends JPanel implements GridChangedListener{ +public class Grille extends JPanel{ private Pion grille[][]; private int column; @@ -55,22 +55,12 @@ public class Grille extends JPanel implements GridChangedListener{ } - protected void addPlayerPiece(int column, int row, int player) { + public void addPlayerPiece(int column, int row, int player) { Color c = player == Constants.PLAYER_ONE ? Constants.PLAYER_ONE_COLOR : Constants.PLAYER_TWO_COLOR; this.grille[column][row].setPlayer(player); this.grille[column][row].repaint(); } - - @Override - public void gridChanged(GridEvent e) { - this.addPlayerPiece(e.getColumn(), e.getRow(), e.getPlayer()); - } - - @Override - public void playerChanged(PlayerEvent e) { - System.out.println("Tour du joueur : " + (e.getNewPlayer() == Constants.PLAYER_ONE ? "Rouge" : "Jaune")); - } - + //Fonction de test (pourra être modifié) public void reset() { for (int x = 0; x < this.column; x++) { diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/View/Puissance4Panel.java b/projetAgile/src/fr/iutfbleau/projetAgile/View/Puissance4Panel.java index b67ee3e..37c4f0f 100644 --- a/projetAgile/src/fr/iutfbleau/projetAgile/View/Puissance4Panel.java +++ b/projetAgile/src/fr/iutfbleau/projetAgile/View/Puissance4Panel.java @@ -3,6 +3,7 @@ package fr.iutfbleau.projetAgile.View; import javax.swing.*; import java.awt.*; import fr.iutfbleau.projetAgile.Controller.GrilleMouseListener; +import fr.iutfbleau.projetAgile.Controller.ModelEventListener; import fr.iutfbleau.projetAgile.Model.GrilleModel; public class Puissance4Panel extends JPanel{ @@ -21,6 +22,9 @@ public class Puissance4Panel extends JPanel{ GrilleModel gm = new GrilleModel(); GrilleMouseListener listener = new GrilleMouseListener(this.grille, gm); + ModelEventListener mel = new ModelEventListener(this.grille); + this.grille.addMouseListener(listener); + gm.addGridListener(mel); this.init(); //this.grille.requestFocusInWindow();