creation of the reset method + start working on clicButtonStop (cannot

restart a simulation after pressed)
This commit is contained in:
Laure BEL 2024-05-27 16:09:15 +02:00
parent 63ea7a1b4d
commit 5c61effc36
2 changed files with 35 additions and 6 deletions

View File

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

View File

@ -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() {