From 3c486887ab56446a02d38d68e2f4514e0aa72f6d Mon Sep 17 00:00:00 2001
From: arnaudlabourel <arnaud.labourel@univ-amu.fr>
Date: Wed, 25 Oct 2023 16:50:00 +0200
Subject: [PATCH] refactoring grid

---
 src/main/java/view/FirefighterGrid.java | 17 +++++++++++++++++
 src/main/java/view/Grid.java            |  3 +--
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/src/main/java/view/FirefighterGrid.java b/src/main/java/view/FirefighterGrid.java
index c4df123..c1b2274 100644
--- a/src/main/java/view/FirefighterGrid.java
+++ b/src/main/java/view/FirefighterGrid.java
@@ -19,10 +19,18 @@ public class FirefighterGrid extends Canvas implements Grid<ViewElement>{
 
     @Override
     public void repaint(List<Pair<Position, ViewElement>> positionedElements) {
+        clear(positionedElements);
         paint(positionedElements);
         paintLines();
     }
 
+    private void clear(List<Pair<Position, ViewElement>> positionedElements) {
+        for (Pair<Position, ViewElement> positionElement : positionedElements) {
+            Position position = positionElement.getKey();
+            clearSquare(position.row(), position.column());
+        }
+    }
+
     private void paint(List<Pair<Position, ViewElement>> positionedElements) {
         for(Pair<Position, ViewElement> pair : positionedElements){
             paintElementAtPosition(pair.getValue(), pair.getKey());
@@ -31,10 +39,15 @@ public class FirefighterGrid extends Canvas implements Grid<ViewElement>{
 
     @Override
     public void repaint(ViewElement[][] elements) {
+        clear();
         paint(elements);
         paintLines();
     }
 
+    private void clear() {
+        getGraphicsContext2D().clearRect(0,0,getWidth(), getHeight());
+    }
+
     private void paint(ViewElement[][] elements) {
         for(int column = 0; column < columnCount; column++)
             for(int row = 0; row < rowCount; row++){
@@ -77,4 +90,8 @@ public class FirefighterGrid extends Canvas implements Grid<ViewElement>{
         getGraphicsContext2D().setFill(color);
         getGraphicsContext2D().fillRect(row*squareHeight,column*squareWidth,squareHeight,squareWidth);
     }
+
+    private void clearSquare(int row, int column){
+        getGraphicsContext2D().clearRect(row*squareHeight,column*squareWidth,squareHeight,squareWidth);
+    }
 }
\ No newline at end of file
diff --git a/src/main/java/view/Grid.java b/src/main/java/view/Grid.java
index d87897b..b95d59f 100644
--- a/src/main/java/view/Grid.java
+++ b/src/main/java/view/Grid.java
@@ -28,8 +28,7 @@ public interface Grid<E> {
   void repaint(E[][] elements);
 
   /**
-   * Set the dimensions of the grid to the specified column count, row count, square width, and square height.
-   * This method adjusts the dimensions of the grid to the given number of columns, number of rows, square width,
+   * Set the dimensions of the grid to the specified number of columns, number of rows, square width,
    * and square height.
    *
    * @param columnCount The new number of columns in the grid.
-- 
GitLab