From 3d3149a9134c91518cdaab5d839fcd184bdd67c9 Mon Sep 17 00:00:00 2001 From: Justine Yannis Date: Thu, 20 Oct 2022 14:51:53 +0200 Subject: [PATCH] Ajout visualisation de la case qui sera remplit --- projetAgile/Makefile | 3 +-- .../Controller/GrilleMouseListener.java | 12 ++++++++++++ .../Controller/Puissance4Controller.java | 14 +++++++++++++- .../fr/iutfbleau/projetAgile/View/Grille.java | 17 +++++++++++++++++ .../src/fr/iutfbleau/projetAgile/View/Pion.java | 13 +++++++++++++ 5 files changed, 56 insertions(+), 3 deletions(-) diff --git a/projetAgile/Makefile b/projetAgile/Makefile index 40bea69..21581db 100644 --- a/projetAgile/Makefile +++ b/projetAgile/Makefile @@ -29,8 +29,7 @@ Events = build/$(PACKAGE_PATH)/Event/GridChangedListener.class \ Model = build/$(PACKAGE_PATH)/Model/GrilleModel.class Controller = build/$(PACKAGE_PATH)/Controller/GrilleMouseListener.class \ - build/$(PACKAGE_PATH)/Controller/ModelEventListener.class \ - build/$(PACKAGE_PATH)/Controller/Puissance4Controller.class + build/$(PACKAGE_PATH)/Controller/ModelEventListener.class #------- Events ------ diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/Controller/GrilleMouseListener.java b/projetAgile/src/fr/iutfbleau/projetAgile/Controller/GrilleMouseListener.java index 36753e2..f62b319 100644 --- a/projetAgile/src/fr/iutfbleau/projetAgile/Controller/GrilleMouseListener.java +++ b/projetAgile/src/fr/iutfbleau/projetAgile/Controller/GrilleMouseListener.java @@ -10,11 +10,23 @@ public class GrilleMouseListener extends MouseInputAdapter{ public GrilleMouseListener(Puissance4Controller p) { this.controller = p; } + + @Override + public void mouseMoved(MouseEvent e) { + this.controller.hoverGrille(e.getX()); + } @Override public void mousePressed(MouseEvent e) { if(e.getButton() == MouseEvent.BUTTON1) { this.controller.verifyColumn(e.getX()); + this.controller.hoverGrille(e.getX()); } } + + @Override + public void mouseExited(MouseEvent e) { + this.controller.hoverGrille(-1); + } + } diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/Controller/Puissance4Controller.java b/projetAgile/src/fr/iutfbleau/projetAgile/Controller/Puissance4Controller.java index 22cee31..90f0e19 100644 --- a/projetAgile/src/fr/iutfbleau/projetAgile/Controller/Puissance4Controller.java +++ b/projetAgile/src/fr/iutfbleau/projetAgile/Controller/Puissance4Controller.java @@ -31,7 +31,7 @@ public class Puissance4Controller { ModelEventListener modelListener = new ModelEventListener(panel); this.modele.addGridListener(modelListener); this.grille.addMouseListener(mouseListener); - + this.grille.addMouseMotionListener(mouseListener); this.panel.init(); this.panel.changeLabel(this.modele.getPlayerTurn()); } @@ -54,7 +54,19 @@ public class Puissance4Controller { } } + public void hoverGrille(int x) { + if(this.modele.getGameStatus() == GameStatus.PLAYING) { + if(x == -1) { + this.grille.hover(-1); //Clean + return; + } + int column = (x * this.modele.getColumn() / grille.getWidth()); + this.grille.hover(column); + } + } + public JPanel getPanel() { return this.panel; } + } diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/View/Grille.java b/projetAgile/src/fr/iutfbleau/projetAgile/View/Grille.java index 3891bab..60bd4a6 100644 --- a/projetAgile/src/fr/iutfbleau/projetAgile/View/Grille.java +++ b/projetAgile/src/fr/iutfbleau/projetAgile/View/Grille.java @@ -61,4 +61,21 @@ public class Grille extends JPanel{ this.repaint(); } + public void hover(int column) { + for(int x = 0; x < this.column; x++) { + for(int y = 0; y < this.row; y++) { + this.grille[x][y].setHover(false); + } + } + if(column != -1) { + for (int row = this.row - 1; row >= 0; row--) { + if (this.grille[column][row].getPlayer() == Constants.EMPTY_PLAYER) { + this.grille[column][row].setHover(true); + break; + } + } + } + this.repaint(); + } + } \ No newline at end of file diff --git a/projetAgile/src/fr/iutfbleau/projetAgile/View/Pion.java b/projetAgile/src/fr/iutfbleau/projetAgile/View/Pion.java index 51b93d8..6b1d235 100644 --- a/projetAgile/src/fr/iutfbleau/projetAgile/View/Pion.java +++ b/projetAgile/src/fr/iutfbleau/projetAgile/View/Pion.java @@ -7,6 +7,7 @@ import fr.iutfbleau.projetAgile.Utils.Constants; public class Pion extends JComponent{ private int player; + private boolean hover = false; public static Dimension getPionMinimumSize() { return new Dimension(80,80); @@ -31,6 +32,14 @@ public class Pion extends JComponent{ this.player = player; } + public void setHover(boolean b) { + this.hover = b; + } + + public int getPlayer() { + return this.player; + } + @Override protected void paintComponent(Graphics g) { Graphics g2 = g.create(); @@ -46,6 +55,10 @@ public class Pion extends JComponent{ c = Constants.EMPTY_COLOR; break; } + if(this.hover) { + g2.setColor(Color.YELLOW); + g2.fillRect(0, 0, this.getWidth(), this.getHeight()); + } g2.setColor(c); g2.fillOval(Constants.PIECE_MARGIN, Constants.PIECE_MARGIN, this.getWidth() - 2 * Constants.PIECE_MARGIN, this.getHeight() - 2 * Constants.PIECE_MARGIN); }