Event custom

This commit is contained in:
Justine Yannis 2022-10-13 15:51:42 +02:00
parent 39b43c0454
commit 90024948f9
8 changed files with 129 additions and 19 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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