From 90024948f902415d0b9a8e41b00b69d3c956685b Mon Sep 17 00:00:00 2001 From: Justine Yannis Date: Thu, 13 Oct 2022 15:51:42 +0200 Subject: [PATCH] Event custom --- projetAgile/Makefile | 24 +++++++++-- .../EventListener/GridChangedListener.java | 8 ++++ .../Controlers/EventListener/GridEvent.java | 25 +++++++++++ .../Controlers/EventListener/PlayerEvent.java | 20 +++++++++ .../Controlers/GrilleMouseListener.java | 2 +- .../Controlers/ObservateurMenuSouris.java | 2 + .../projetAgile/Model/GrilleModel.java | 42 ++++++++++++++++--- .../fr/iutfbleau/projetAgile/View/Grille.java | 25 ++++++----- 8 files changed, 129 insertions(+), 19 deletions(-) create mode 100644 projetAgile/src/fr/iutfbleau/projetAgile/Controlers/EventListener/GridChangedListener.java create mode 100644 projetAgile/src/fr/iutfbleau/projetAgile/Controlers/EventListener/GridEvent.java create mode 100644 projetAgile/src/fr/iutfbleau/projetAgile/Controlers/EventListener/PlayerEvent.java diff --git a/projetAgile/Makefile b/projetAgile/Makefile index 9ab04c5..a826fea 100644 --- a/projetAgile/Makefile +++ b/projetAgile/Makefile @@ -10,12 +10,28 @@ View : build/$(PACKAGE_PATH)/View/Pion.class \ Utils : build/$(PACKAGE_PATH)/Utils/Constants.class +Events : build/$(PACKAGE_PATH)/Controlers/EventListener/GridChangedListener.class \ + build/$(PACKAGE_PATH)/Controlers/EventListener/GridEvent.class \ + build/$(PACKAGE_PATH)/Controlers/EventListener/PlayerEvent.class + +build/$(PACKAGE_PATH)/Controlers/EventListener/PlayerEvent.class : src/$(PACKAGE_PATH)/Controlers/EventListener/PlayerEvent.java + javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/Controlers/EventListener/PlayerEvent.java + +build/$(PACKAGE_PATH)/Controlers/EventListener/GridEvent.class : src/$(PACKAGE_PATH)/Controlers/EventListener/GridEvent.java + javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/Controlers/EventListener/GridEvent.java + +build/$(PACKAGE_PATH)/Controlers/EventListener/GridChangedListener.class : src/$(PACKAGE_PATH)/Controlers/EventListener/GridChangedListener.java \ + build/$(PACKAGE_PATH)/Controlers/EventListener/GridEvent.class \ + build/$(PACKAGE_PATH)/Controlers/EventListener/GridEvent.class + javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/Controlers/EventListener/GridChangedListener.java + build/$(PACKAGE_PATH)/Controlers/GrilleMouseListener.class : src/$(PACKAGE_PATH)/Controlers/GrilleMouseListener.java \ build/$(PACKAGE_PATH)/View/Grille.class \ build/$(PACKAGE_PATH)/Model/GrilleModel.class javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/Controlers/GrilleMouseListener.java -build/$(PACKAGE_PATH)/Model/GrilleModel.class : src/$(PACKAGE_PATH)/Model/GrilleModel.java +build/$(PACKAGE_PATH)/Model/GrilleModel.class : src/$(PACKAGE_PATH)/Model/GrilleModel.java \ + Events javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/Model/GrilleModel.java build/$(PACKAGE_PATH)/Utils/Constants.class : src/$(PACKAGE_PATH)/Utils/Constants.java @@ -27,7 +43,8 @@ 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)/Utils/Constants.class + build/$(PACKAGE_PATH)/Utils/Constants.class \ + Events javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/View/Grille.java build/$(PACKAGE_PATH)/View/Puissance4Panel.class : src/$(PACKAGE_PATH)/View/Puissance4Panel.java \ @@ -38,7 +55,8 @@ build/$(PACKAGE_PATH)/View/Puissance4Panel.class : src/$(PACKAGE_PATH)/View/Pui build/$(PACKAGE_PATH)/View/TestGrille.class : src/$(PACKAGE_PATH)/View/TestGrille.java \ build/$(PACKAGE_PATH)/View/Puissance4Panel.class \ build/$(PACKAGE_PATH)/Model/GrilleModel.class \ - build/$(PACKAGE_PATH)/Controlers/GrilleMouseListener.class + build/$(PACKAGE_PATH)/Controlers/GrilleMouseListener.class \ + Events javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/View/TestGrille.java diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/Controlers/EventListener/GridChangedListener.java b/projetAgile/src/fr/iutfbleau/projetAgile/Controlers/EventListener/GridChangedListener.java new file mode 100644 index 0000000..0c108a4 --- /dev/null +++ b/projetAgile/src/fr/iutfbleau/projetAgile/Controlers/EventListener/GridChangedListener.java @@ -0,0 +1,8 @@ +package fr.iutfbleau.projetAgile.Controlers.EventListener; + +import java.util.EventListener; + +public interface GridChangedListener extends EventListener{ + void gridChanged(GridEvent e); + void playerChanged(PlayerEvent e); +} diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/Controlers/EventListener/GridEvent.java b/projetAgile/src/fr/iutfbleau/projetAgile/Controlers/EventListener/GridEvent.java new file mode 100644 index 0000000..4e501e2 --- /dev/null +++ b/projetAgile/src/fr/iutfbleau/projetAgile/Controlers/EventListener/GridEvent.java @@ -0,0 +1,25 @@ +package fr.iutfbleau.projetAgile.Controlers.EventListener; + +public class GridEvent { + private int column; + private int row; + private int player; + + public GridEvent(int column, int row, int player) { + this.column = column; + this.row = row; + this.player = player; + } + + public int getColumn() { + return this.column; + } + + public int getRow() { + return this.row; + } + + public int getPlayer() { + return this.player; + } +} diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/Controlers/EventListener/PlayerEvent.java b/projetAgile/src/fr/iutfbleau/projetAgile/Controlers/EventListener/PlayerEvent.java new file mode 100644 index 0000000..56c2cfb --- /dev/null +++ b/projetAgile/src/fr/iutfbleau/projetAgile/Controlers/EventListener/PlayerEvent.java @@ -0,0 +1,20 @@ +package fr.iutfbleau.projetAgile.Controlers.EventListener; + +public class PlayerEvent { + + private int oldPlayer; + private int newPlayer; + + public PlayerEvent(int oldPlayer, int newPlayer) { + this.newPlayer = newPlayer; + this.oldPlayer = oldPlayer; + } + + public int getNewPlayer() { + return newPlayer; + } + + public int getOldPlayer() { + return oldPlayer; + } +} diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/Controlers/GrilleMouseListener.java b/projetAgile/src/fr/iutfbleau/projetAgile/Controlers/GrilleMouseListener.java index d766d14..f46dc13 100644 --- a/projetAgile/src/fr/iutfbleau/projetAgile/Controlers/GrilleMouseListener.java +++ b/projetAgile/src/fr/iutfbleau/projetAgile/Controlers/GrilleMouseListener.java @@ -14,7 +14,7 @@ public class GrilleMouseListener extends MouseInputAdapter{ public GrilleMouseListener(Grille grille, GrilleModel modele) { this.grille = grille; this.modele = modele; - this.modele.addObserver(grille); + this.modele.addGridListener(grille); } @Override diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/Controlers/ObservateurMenuSouris.java b/projetAgile/src/fr/iutfbleau/projetAgile/Controlers/ObservateurMenuSouris.java index 70bd05a..144afcc 100644 --- a/projetAgile/src/fr/iutfbleau/projetAgile/Controlers/ObservateurMenuSouris.java +++ b/projetAgile/src/fr/iutfbleau/projetAgile/Controlers/ObservateurMenuSouris.java @@ -3,6 +3,8 @@ package fr.iutfbleau.projetAgile.Controlers; import java.awt.event.MouseListener; import java.awt.event.*; import javax.swing.JFrame; +import fr.iutfbleau.projetAgile.View.*; + public class ObservateurMenuSouris implements MouseListener{ private BoutonsMenu bout; private JFrame fenetre; diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/Model/GrilleModel.java b/projetAgile/src/fr/iutfbleau/projetAgile/Model/GrilleModel.java index 59a778f..7320f33 100644 --- a/projetAgile/src/fr/iutfbleau/projetAgile/Model/GrilleModel.java +++ b/projetAgile/src/fr/iutfbleau/projetAgile/Model/GrilleModel.java @@ -1,13 +1,17 @@ package fr.iutfbleau.projetAgile.Model; -import java.util.Observable; + +import javax.swing.event.EventListenerList; +import fr.iutfbleau.projetAgile.Controlers.EventListener.*; import fr.iutfbleau.projetAgile.Utils.Constants; -public class GrilleModel extends Observable { +public class GrilleModel { private int[][] grille; private boolean partyEnd; private int playerTurn; private int column, row; + private EventListenerList listeners = new EventListenerList(); + public GrilleModel() { this.init(); } @@ -23,14 +27,12 @@ public class GrilleModel extends Observable { } } - public boolean addPawn(int column) { for (int row = this.row - 1; row >= 0; row--) { if (grille[column][row] == Constants.EMPTY_PLAYER) { int[] args = {column, row, this.playerTurn}; grille[column][row] = this.playerTurn; - this.setChanged(); - this.notifyObservers(args); + fireGridChanged(column, row, this.playerTurn); this.switchPlayer(); return true; } @@ -42,4 +44,34 @@ public class GrilleModel extends Observable { this.playerTurn = (this.playerTurn + 1) % 2; } + public void addGridListener(GridChangedListener listener) { + listeners.add(GridChangedListener.class, listener); + } + + public void removeTGridListener(GridChangedListener listener) { + listeners.remove(GridChangedListener.class, listener); + } + + public GridChangedListener[] getGridListeners() { + return listeners.getListeners(GridChangedListener.class); + } + + private void fireGridChanged(int column, int row, int player) { + GridEvent event = null; + for(GridChangedListener listener : getGridListeners()) { + if(event == null) + event = new GridEvent(column, row, player); + listener.gridChanged(event); + } + } + + private void firePlayerChanged(int oldPlayer, int newPlayer) { + PlayerEvent event = null; + for(GridChangedListener listener : getGridListeners()) { + if(event == null) + event = new PlayerEvent(oldPlayer, newPlayer); + listener.playerChanged(event); + } + } + } diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/View/Grille.java b/projetAgile/src/fr/iutfbleau/projetAgile/View/Grille.java index 1471fa8..c748970 100644 --- a/projetAgile/src/fr/iutfbleau/projetAgile/View/Grille.java +++ b/projetAgile/src/fr/iutfbleau/projetAgile/View/Grille.java @@ -2,11 +2,10 @@ package fr.iutfbleau.projetAgile.View; import javax.swing.*; import java.awt.*; -import java.util.Observable; -import java.util.Observer; +import fr.iutfbleau.projetAgile.Controlers.EventListener.*; import fr.iutfbleau.projetAgile.Utils.Constants; -public class Grille extends JPanel implements Observer{ +public class Grille extends JPanel implements GridChangedListener{ private Pion grille[][]; @@ -55,13 +54,6 @@ public class Grille extends JPanel implements Observer{ this.setMinimumSize(new Dimension(minimumWidth, minimumHeight)); this.setPreferredSize(new Dimension(preferredWidth, preferredHeight)); } - - @Override - public void update(Observable o, Object arg) { - int[] param = (int[]) arg; - this.addPlayerPawn(param[0], param[1], param[2]); - System.out.println("Appuie sur la colonne : " + param[0]); - } protected void addPlayerPawn(int column, int row, int player) { @@ -70,4 +62,17 @@ public class Grille extends JPanel implements Observer{ this.grille[column][row].repaint(); } + @Override + public void gridChanged(GridEvent e) { + this.addPlayerPawn(e.getColumn(), e.getRow(), e.getPlayer()); + System.out.println("Appuie sur la colonne : " + e.getColumn()); + + } + + @Override + public void playerChanged(PlayerEvent e) { + System.out.println("Nouveau jouer : " + (e.getNewPlayer() == Constants.PLAYER_ONE ? Constants.PLAYER_ONE : Constants.PLAYER_TWO)); + + } + } \ No newline at end of file