From 5c61effc369dccdbe4fce21d77a27dc59654b70c Mon Sep 17 00:00:00 2001 From: laure Date: Mon, 27 May 2024 16:09:15 +0200 Subject: [PATCH] creation of the reset method + start working on clicButtonStop (cannot restart a simulation after pressed) --- src/backend/Simulator.java | 26 +++++++++++++++++++++----- src/windowInterface/MyInterface.java | 15 ++++++++++++++- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index 042d236..60d109e 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -29,12 +29,12 @@ public class Simulator extends Thread { private int[][] world; //TODO : add missing attribute(s) - + private int stepCount; public Simulator(MyInterface mjfParam) { mjf = mjfParam; - stopFlag=false; + //stopFlag=false; //not necessary since i set the state when pressing the button start pauseFlag=false; loopingBorder=false; clickActionFlag=false; @@ -64,7 +64,7 @@ public class Simulator extends Thread { //Should probably stay as is public void run() { - int stepCount=0; + stepCount=0; while(!stopFlag) { stepCount++; makeStep(); @@ -174,8 +174,13 @@ public class Simulator extends Thread { /* * leave this as is */ - public void stopSimu() { + public void stopSimu() { stopFlag=true; + this.interrupt(); + } + + public void startSimu() { // same as stopSimu but will set the stopFlag value as false. + stopFlag=false; } /* @@ -305,7 +310,7 @@ public class Simulator extends Thread { ArrayList rule = new ArrayList<>(); for (int i = 0; i < getHeight(); i++) { StringBuilder lineState = new StringBuilder(); - for (int j = 0 ; j < getHeight() ; j++) { + for (int j = 0 ; j < getHeight() ; j++) { // je crois qu'il y a un probleme, il faut mettre getWidth je crois lineState.append(getCell(i, j)); if (j < getWidth() - 1) { lineState.append(";"); @@ -407,5 +412,16 @@ public class Simulator extends Thread { // depending on clickActionFlag return ""; } + + + public void reset() { + for (int i = 0; i < getHeight(); i++) { + for (int j = 0 ; j < getWidth() ; j++) { + setCell(i,j,0); + } + } + this.stepCount = 0; + } } + diff --git a/src/windowInterface/MyInterface.java b/src/windowInterface/MyInterface.java index d0a433f..c52fe79 100644 --- a/src/windowInterface/MyInterface.java +++ b/src/windowInterface/MyInterface.java @@ -226,6 +226,7 @@ public class MyInterface extends JFrame { public void clicButtonGo() { this.instantiateSimu(); if(!mySimu.isAlive()) { + mySimu.startSimu(); mySimu.start(); } else { mySimu.togglePause(); @@ -233,7 +234,19 @@ public class MyInterface extends JFrame { } public void clicButtonStop() { - //TODO + //mySimu.stopSimu(); + //update(0); + //instantiateSimu(); + + mySimu.stopSimu(); // Stop the simulation + mySimu.reset(); // Reset the simulation state + panelDraw.repaint(); // Refresh the display + eraseLabels(); // Reset the labels to their initial state + //mySimu.stopSimu(); + + + + } public void clicButtonToggleClickAction() {