Si ci avevo pensato e infatti funziona!
Solo che ora di fatto me lo blocca, ma se premo nuovamente su avvia non mi riprende la simulazione!
Ora sto provando a lavorare sulle condizioni!
Vi posto il codice (semplificato) della classe :
codice:
import java.util.Random;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.awt.Color;
import java.util.Collections;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class Simulator
{
// VARIE DICHIARIAZIONI
private Thread t = null;
private SimpleRunner r = new SimpleRunner();
// VARI METODI
public void runLongSimulation()
{
simulate(500);
}
public void simulate(int numSteps)
{
for(int step = 1; step <= numSteps && view.isViable(field) && r.isRunning()==true; step++) {
simulateOneStep();
}
}
public void simulateOneStep()
{
//aumenta variabile che conta gli step
step++;
//invoca il metodo act su tutti gli oggetti contenuti nella lista delle location
for(Location loc : locations) {
field.getObjectAt(loc).act();
}
//resetta gli attributi su ogni animale della mappa
for(Location loc : locations) {
LocationType locType = field.getObjectAt(loc);
if(locType instanceof Grass) {
Grass grass = (Grass) locType;
grass.resetAnimalAttrib();
}
}
//randomizza la lista
Collections.shuffle(locations,rand);
//mostra la nuova composizione
view.showStatus(step, field);
}
/**
* Reset the simulation to a starting position.
*/
public void reset()
{
step = 0;
locations.clear();
populate();
//crea la lista delle location
for(int row = 0; row < field.getDepth(); row++) {
for(int col = 0; col < field.getWidth(); col++) {
locations.add(new Location(row,col));
}
}
// Show the starting state in the view.
view.showStatus(step, field);
}
public void makeMenuBar(JFrame frame)
{
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
Button avvio=new Button("Avvio");
Button stop=new Button("Stop");
Button onestep=new Button("OneStep");
Button nstep=new Button("NStep");
Button reset=new Button("Reset");
menubar.add(avvio);
menubar.add(stop);
menubar.add(onestep);
menubar.add(nstep);
menubar.add(reset);
avvio.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(t==null)
{
t = new Thread(r);
t.start();
}
else
{
r.setRunning(true);
}
}
});
stop.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
r.setRunning(false);
}
});
onestep.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
simulateOneStep();
}
});
nstep.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
t = new Thread (new NRunner());
t.start();
}
});
reset.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
reset();
}
});
}
private class SimpleRunner implements Runnable
{
private boolean running = true;
public void setRunning(boolean value)
{
running = value;
}
public boolean isRunning()
{
return running;
}
public void run()
{
simulate(500);
}
}
private class NRunner implements Runnable
{
public void run()
{
String s = JOptionPane.showInputDialog(null, "Quanti step vuoi eseguire?");
int n = Integer.parseInt(s);
simulate(n);
}
}
}