Début du modèle et du listener

This commit is contained in:
Justine Yannis 2022-10-13 13:40:53 +02:00
parent c1e4def0eb
commit 521be6a04a
11 changed files with 160 additions and 61 deletions

View File

@ -8,25 +8,41 @@ JAR_OPTION = cvfe projetAgile.jar $(PACKAGE).main.Main -C build fr -C res
View : build/$(PACKAGE_PATH)/View/Pion.class \
build/$(PACKAGE_PATH)/View/Grille.class
Utils : build/$(PACKAGE_PATH)/Utils/Utils.class
Utils : build/$(PACKAGE_PATH)/Utils/Constants.class
build/$(PACKAGE_PATH)/Utils/Utils.class : src/$(PACKAGE_PATH)/Utils/Utils.java
javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/Utils/Utils.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
javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/Model/GrilleModel.java
build/$(PACKAGE_PATH)/Utils/Constants.class : src/$(PACKAGE_PATH)/Utils/Constants.java
javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/Utils/Constants.java
build/$(PACKAGE_PATH)/View/Pion.class : src/$(PACKAGE_PATH)/View/Pion.java \
build/$(PACKAGE_PATH)/Utils/Utils.class
build/$(PACKAGE_PATH)/Utils/Constants.class
javac $(JAVAC_OPT) 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/Utils.class
build/$(PACKAGE_PATH)/Utils/Constants.class
javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/View/Grille.java
build/$(PACKAGE_PATH)/View/TestGrille.class : src/$(PACKAGE_PATH)/View/TestGrille.java \
build/$(PACKAGE_PATH)/View/Puissance4Panel.class : src/$(PACKAGE_PATH)/View/Puissance4Panel.java \
build/$(PACKAGE_PATH)/View/Pion.class \
build/$(PACKAGE_PATH)/View/Grille.class
javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/View/Puissance4Panel.java
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
javac $(JAVAC_OPT) src/$(PACKAGE_PATH)/View/TestGrille.java
testGrille : build/$(PACKAGE_PATH)/View/TestGrille.class
java -cp build $(PACKAGE).View.TestGrille

View File

@ -0,0 +1,25 @@
package fr.iutfbleau.projetAgile.Controlers;
import java.awt.event.MouseEvent;
import javax.swing.event.MouseInputAdapter;
import fr.iutfbleau.projetAgile.Model.GrilleModel;
import fr.iutfbleau.projetAgile.View.Grille;
import fr.iutfbleau.projetAgile.Utils.Constants;
public class GrilleMouseListener extends MouseInputAdapter{
private Grille grille;
private GrilleModel modele;
public GrilleMouseListener(Grille grille, GrilleModel modele) {
this.grille = grille;
this.modele = modele;
this.modele.addObserver(grille);
}
@Override
public void mouseClicked(MouseEvent e) {
int column = (e.getX() * Constants.COLUMN_COUNT / grille.getWidth());
this.modele.addPawn(column);
}
}

View File

@ -3,10 +3,10 @@ package fr.iutfbleau.projetAgile.Controlers;
import java.awt.event.MouseListener;
import java.awt.event.*;
import javax.swing.JFrame;
public class obeservateur_menu_souris implements MouseListener{
private boutons_menu bout;
public class ObservateurMenuSouris implements MouseListener{
private BoutonsMenu bout;
private JFrame fenetre;
public obeservateur_menu_souris(boutons_menu boutons, JFrame fenetre0){
public ObservateurMenuSouris(BoutonsMenu boutons, JFrame fenetre0){
this.bout=boutons;
this.fenetre=fenetre0;
}

View File

@ -0,0 +1,45 @@
package fr.iutfbleau.projetAgile.Model;
import java.util.Observable;
import fr.iutfbleau.projetAgile.Utils.Constants;
public class GrilleModel extends Observable {
private int[][] grille;
private boolean partyEnd;
private int playerTurn;
private int column, row;
public GrilleModel() {
this.init();
}
public void init() {
this.column = Constants.COLUMN_COUNT;
this.row = Constants.ROW_COUNT;
this.grille = new int[this.column][this.row];
for(int i = 0; i < this.column; i++) {
for(int j = 0; j < this.row; j++) {
this.grille[i][j] = Constants.EMPTY_PLAYER;
}
}
}
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);
this.switchPlayer();
return true;
}
}
return false;
}
private void switchPlayer() {
this.playerTurn = (this.playerTurn + 1) % 2;
}
}

View File

@ -2,10 +2,11 @@ package fr.iutfbleau.projetAgile.Utils;
import java.awt.Color;
public class Utils {
public class Constants {
public final static Color PLAYER_ONE_COLOR = Color.RED;
public final static Color PLAYER_TWO_COLOR = Color.YELLOW;
public final static Color EMPTY_COLOR = new Color(42,42,42);
public final static int EMPTY_PLAYER = -1;
public final static int PLAYER_ONE = 0;
public final static int PLAYER_TWO = 1;
public static int PAWN_MARGIN = 10;

View File

@ -1,14 +1,13 @@
package fr.iutfbleau.projetAgile.View;
import java.lang.*;
import javax.swing.JComponent;
import java.awt.*;
public class boutons_menu extends JComponent{
public class BoutonsMenu extends JComponent{
private String path;
private boolean survol;
private Image img;
public boutons_menu(String path0){
public BoutonsMenu(String path0){
super();
this.path=path0;
ClassLoader cl = Thread.currentThread().getContextClassLoader();

View File

@ -1,11 +1,10 @@
package fr.iutfbleau.projetAgile.View;
import javax.rmi.CORBA.Util;
import javax.swing.*;
import java.awt.*;
import java.util.Observable;
import java.util.Observer;
import fr.iutfbleau.projetAgile.Utils.Utils;
import fr.iutfbleau.projetAgile.Utils.Constants;
public class Grille extends JPanel implements Observer{
@ -28,9 +27,9 @@ public class Grille extends JPanel implements Observer{
GridBagLayout gbl = new GridBagLayout();
this.setLayout(gbl);
GridBagConstraints gbc = new GridBagConstraints();
this.grille = new Pion[Utils.COLUMN_COUNT][Utils.ROW_COUNT];
for (int x = 0; x < Utils.COLUMN_COUNT; x++) {
for (int y = 0; y < Utils.ROW_COUNT; y++) {
this.grille = new Pion[Constants.COLUMN_COUNT][Constants.ROW_COUNT];
for (int x = 0; x < Constants.COLUMN_COUNT; x++) {
for (int y = 0; y < Constants.ROW_COUNT; y++) {
Pion pion = new Pion(-1);
gbc.gridx = x;
gbc.gridy = y;
@ -48,10 +47,10 @@ public class Grille extends JPanel implements Observer{
}
}
int minimumWidth = Utils.COLUMN_COUNT * Pion.getPionMinimumSize().width;
int minimumHeight = Utils.ROW_COUNT * Pion.getPionMinimumSize().height;
int preferredWidth = Utils.COLUMN_COUNT * Pion.getPionPreferredSize().width;
int preferredHeight = Utils.ROW_COUNT * Pion.getPionPreferredSize().height;
int minimumWidth = Constants.COLUMN_COUNT * Pion.getPionMinimumSize().width;
int minimumHeight = Constants.ROW_COUNT * Pion.getPionMinimumSize().height;
int preferredWidth = Constants.COLUMN_COUNT * Pion.getPionPreferredSize().width;
int preferredHeight = Constants.ROW_COUNT * Pion.getPionPreferredSize().height;
this.setMinimumSize(new Dimension(minimumWidth, minimumHeight));
this.setPreferredSize(new Dimension(preferredWidth, preferredHeight));
@ -61,11 +60,12 @@ public class Grille extends JPanel implements Observer{
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) {
Color c = player == Utils.PLAYER_ONE ? Utils.PLAYER_ONE_COLOR : Utils.PLAYER_TWO_COLOR;
Color c = player == Constants.PLAYER_ONE ? Constants.PLAYER_ONE_COLOR : Constants.PLAYER_TWO_COLOR;
this.grille[column][row] = new Pion(player);
}

View File

@ -5,9 +5,9 @@ import javax.swing.JFrame;
import javax.swing.JLabel;
import fr.iutfbleau.projetAgile.Controlers.*;
public class menu extends JFrame{
public class Menu extends JFrame{
public menu(){
public Menu(){
super();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(0,0);
@ -34,41 +34,41 @@ public class menu extends JFrame{
titre.setPreferredSize(new Dimension(400,200));
this.add(titre, gbc);
boutons_menu puissance_4=new boutons_menu("puissance4.png");
boutons_menu echecs=new boutons_menu("echecs.png");
boutons_menu dames=new boutons_menu("dames.png");
BoutonsMenu puissance_4=new BoutonsMenu("puissance4.png");
BoutonsMenu echecs=new BoutonsMenu("echecs.png");
BoutonsMenu dames=new BoutonsMenu("dames.png");
boutons_menu demineur=new boutons_menu("demineur.png");
boutons_menu taquin=new boutons_menu("taquin.png");
boutons_menu morpion=new boutons_menu("morpion.png");
BoutonsMenu demineur=new BoutonsMenu("demineur.png");
BoutonsMenu taquin=new BoutonsMenu("taquin.png");
BoutonsMenu morpion=new BoutonsMenu("morpion.png");
boutons_menu CoD=new boutons_menu("CoD.png");
boutons_menu minecraft=new boutons_menu("minecraft.png");
boutons_menu puzzle=new boutons_menu("puzzle.png");
BoutonsMenu CoD=new BoutonsMenu("CoD.png");
BoutonsMenu minecraft=new BoutonsMenu("minecraft.png");
BoutonsMenu puzzle=new BoutonsMenu("puzzle.png");
puissance_4.addMouseListener(new obeservateur_menu_souris(puissance_4, this));
echecs.addMouseListener(new obeservateur_menu_souris(echecs, this));
dames.addMouseListener(new obeservateur_menu_souris(dames, this));
puissance_4.addMouseListener(new ObservateurMenuSouris(puissance_4, this));
echecs.addMouseListener(new ObservateurMenuSouris(echecs, this));
dames.addMouseListener(new ObservateurMenuSouris(dames, this));
demineur.addMouseListener(new obeservateur_menu_souris(demineur, this));
taquin.addMouseListener(new obeservateur_menu_souris(taquin, this));
morpion.addMouseListener(new obeservateur_menu_souris(morpion, this));
demineur.addMouseListener(new ObservateurMenuSouris(demineur, this));
taquin.addMouseListener(new ObservateurMenuSouris(taquin, this));
morpion.addMouseListener(new ObservateurMenuSouris(morpion, this));
CoD.addMouseListener(new obeservateur_menu_souris(CoD, this));
minecraft.addMouseListener(new obeservateur_menu_souris(minecraft, this));
puzzle.addMouseListener(new obeservateur_menu_souris(puzzle, this));
CoD.addMouseListener(new ObservateurMenuSouris(CoD, this));
minecraft.addMouseListener(new ObservateurMenuSouris(minecraft, this));
puzzle.addMouseListener(new ObservateurMenuSouris(puzzle, this));
this.add(puissance_4, menu.GridBagConstraintsRetour(0, 1));
this.add(echecs, menu.GridBagConstraintsRetour(1, 1));
this.add(dames, menu.GridBagConstraintsRetour(2, 1));
this.add(puissance_4, Menu.GridBagConstraintsRetour(0, 1));
this.add(echecs, Menu.GridBagConstraintsRetour(1, 1));
this.add(dames, Menu.GridBagConstraintsRetour(2, 1));
this.add(demineur, menu.GridBagConstraintsRetour(0, 2));
this.add(taquin, menu.GridBagConstraintsRetour(1, 2));
this.add(morpion, menu.GridBagConstraintsRetour(2, 2));
this.add(demineur, Menu.GridBagConstraintsRetour(0, 2));
this.add(taquin, Menu.GridBagConstraintsRetour(1, 2));
this.add(morpion, Menu.GridBagConstraintsRetour(2, 2));
this.add(CoD, menu.GridBagConstraintsRetour(0, 3));
this.add(minecraft, menu.GridBagConstraintsRetour(1, 3));
this.add(puzzle, menu.GridBagConstraintsRetour(2, 3));
this.add(CoD, Menu.GridBagConstraintsRetour(0, 3));
this.add(minecraft, Menu.GridBagConstraintsRetour(1, 3));
this.add(puzzle, Menu.GridBagConstraintsRetour(2, 3));
this.setVisible(true);
}
public static GridBagConstraints GridBagConstraintsRetour(int collonne, int ligne){

View File

@ -2,7 +2,7 @@ package fr.iutfbleau.projetAgile.View;
import java.awt.*;
import javax.swing.*;
import fr.iutfbleau.projetAgile.Utils.Utils;
import fr.iutfbleau.projetAgile.Utils.Constants;
public class Pion extends JComponent{
@ -32,17 +32,17 @@ public class Pion extends JComponent{
Graphics g2 = g.create();
Color c;
switch(this.player) {
case Utils.PLAYER_ONE :
c = Utils.PLAYER_ONE_COLOR;
case Constants.PLAYER_ONE :
c = Constants.PLAYER_ONE_COLOR;
break;
case Utils.PLAYER_TWO :
c = Utils.PLAYER_TWO_COLOR;
case Constants.PLAYER_TWO :
c = Constants.PLAYER_TWO_COLOR;
break;
default :
c = Utils.EMPTY_COLOR;
c = Constants.EMPTY_COLOR;
break;
}
g2.setColor(c);
g2.fillOval(Utils.PAWN_MARGIN, Utils.PAWN_MARGIN, this.getWidth() - 2 * Utils.PAWN_MARGIN, this.getHeight() - 2 * Utils.PAWN_MARGIN);
g2.fillOval(Constants.PAWN_MARGIN, Constants.PAWN_MARGIN, this.getWidth() - 2 * Constants.PAWN_MARGIN, this.getHeight() - 2 * Constants.PAWN_MARGIN);
}
}

View File

@ -1,6 +1,5 @@
package fr.iutfbleau.projetAgile.View;
import java.util.Observable;
import javax.swing.*;
import java.awt.*;
@ -78,4 +77,7 @@ public class Puissance4Panel extends JPanel{
this.add(panneauBas, gbc);
}
public Grille getGrille() {
return grille;
}
}

View File

@ -2,13 +2,24 @@ package fr.iutfbleau.projetAgile.View;
import javax.swing.*;
import java.awt.*;
import fr.iutfbleau.projetAgile.Controlers.GrilleMouseListener;
import fr.iutfbleau.projetAgile.Model.GrilleModel;
public class TestGrille extends JFrame{
public TestGrille() {
super("Puissance 4");
Puissance4Panel p = new Puissance4Panel();
Grille g = p.getGrille();
GrilleModel gm = new GrilleModel();
GrilleMouseListener listener = new GrilleMouseListener(g, gm);
g.addMouseListener(listener);
this.add(p);
this.setLocation(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new Puissance4Panel());
this.setSize(new Dimension(1280,720));
this.setVisible(true);
}
public static void main(String[] args) {