parent
94fd880575
commit
ce36dc5e09
|
|
@ -1,8 +1,12 @@
|
||||||
import windowInterface.MyInterface;
|
import windowInterface.MyInterface;
|
||||||
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
MyInterface mjf = new MyInterface();
|
MyInterface mjf = new MyInterface();
|
||||||
mjf.setVisible(true);
|
mjf.setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -17,21 +17,24 @@ public abstract class Agent {
|
||||||
public Color getDisplayColor() {
|
public Color getDisplayColor() {
|
||||||
return color;
|
return color;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getX() {
|
public int getX() {
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getY() {
|
public int getY() {
|
||||||
return y;
|
return y;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isInArea(int x, int y, int radius) {
|
public boolean isInArea(int x, int y, int radius) {
|
||||||
int diffX = this.x - x;
|
int diffX = this.x-x;
|
||||||
int diffY = this.y - y;
|
int diffY = this.y-y;
|
||||||
int dist = (int) Math.floor(Math.sqrt(diffX * diffX + diffY * diffY));
|
int dist = (int) Math.floor(Math.sqrt(diffX*diffX+diffY*diffY));
|
||||||
return dist < radius;
|
return dist<radius;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Does whatever the agent does during a step
|
||||||
|
// then returns a boolean
|
||||||
|
// if false, agent dies at end of turn
|
||||||
|
// see step function in Simulator
|
||||||
public abstract boolean liveTurn(ArrayList<Agent> neighbors, Simulator world);
|
public abstract boolean liveTurn(ArrayList<Agent> neighbors, Simulator world);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,33 +4,56 @@ import java.awt.Color;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
|
// example of basic animal.
|
||||||
|
// do not hesitate to make it more complex
|
||||||
|
// and DO add at least another species that interact with it
|
||||||
|
// for example wolves that eat Sheep
|
||||||
public class Sheep extends Agent {
|
public class Sheep extends Agent {
|
||||||
|
|
||||||
int hunger;
|
int hunger;
|
||||||
Random rand;
|
Random rand;
|
||||||
|
|
||||||
public Sheep(int x, int y) {
|
Sheep(int x,int y){
|
||||||
super(x, y, Color.white);
|
//first we call the constructor of the superClass(Animal)
|
||||||
|
//with the values we want.
|
||||||
|
// here we decide that a Sheep is initially white using this constructor
|
||||||
|
super(x,y,Color.white);
|
||||||
|
// we give our sheep a hunger value of zero at birth
|
||||||
hunger = 0;
|
hunger = 0;
|
||||||
|
//we initialize the random number generator we will use to move randomly
|
||||||
rand = new Random();
|
rand = new Random();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* action of the animal
|
||||||
|
* it can interact with the cells or with other animals
|
||||||
|
* as you wish
|
||||||
|
*/
|
||||||
public boolean liveTurn(ArrayList<Agent> neighbors, Simulator world) {
|
public boolean liveTurn(ArrayList<Agent> neighbors, Simulator world) {
|
||||||
if (world.getCell(x, y) == 1) {
|
if(world.getCell(x, y)==1) {
|
||||||
world.setCell(x, y, 0);
|
world.setCell(x, y, 0);
|
||||||
hunger = 0;
|
|
||||||
} else {
|
} else {
|
||||||
hunger++;
|
hunger++;
|
||||||
}
|
}
|
||||||
this.moveRandom(world);
|
this.moveRandom();
|
||||||
return hunger <= 10;
|
return hunger>10;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void moveRandom(Simulator world) {
|
private void moveRandom() {
|
||||||
int newX = x + rand.nextInt(3) - 1;
|
int direction = rand.nextInt(4);
|
||||||
int newY = y + rand.nextInt(3) - 1;
|
if(direction == 0) {
|
||||||
if (newX >= 0 && newX < world.getWidth() && newY >= 0 && newY < world.getHeight()) {
|
x+=1;
|
||||||
x = newX;
|
}
|
||||||
y = newY;
|
if(direction == 1) {
|
||||||
|
y+=1;
|
||||||
|
}
|
||||||
|
if(direction == 2) {
|
||||||
|
x-=1;
|
||||||
|
}
|
||||||
|
if(direction == 3) {
|
||||||
|
y-=1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,202 +1,362 @@
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Random;
|
|
||||||
import java.util.Timer;
|
|
||||||
import java.util.TimerTask;
|
|
||||||
|
|
||||||
import windowInterface.MyInterface;
|
import windowInterface.MyInterface;
|
||||||
|
|
||||||
public class Simulator {
|
public class Simulator extends Thread {
|
||||||
private int width;
|
|
||||||
private int height;
|
private MyInterface mjf;
|
||||||
private int[][] world;
|
|
||||||
|
private final int COL_NUM = 100;
|
||||||
|
private final int LINE_NUM = 100;
|
||||||
|
private final int LIFE_TYPE_NUM = 4;
|
||||||
|
//Conway Radius : 1
|
||||||
|
private final int LIFE_AREA_RADIUS = 1;
|
||||||
|
//Animal Neighborhood Radius : 5
|
||||||
|
private final int ANIMAL_AREA_RADIUS = 2;
|
||||||
|
private ArrayList<Integer> fieldSurviveValues;
|
||||||
|
private ArrayList<Integer> fieldBirthValues;
|
||||||
|
|
||||||
private ArrayList<Agent> agents;
|
private ArrayList<Agent> agents;
|
||||||
private boolean loopingBorder;
|
|
||||||
|
private boolean stopFlag;
|
||||||
private boolean pauseFlag;
|
private boolean pauseFlag;
|
||||||
private int loopDelay;
|
private boolean loopingBorder;
|
||||||
private MyInterface interfaceGlobal;
|
private boolean clickActionFlag;
|
||||||
private Timer timer;
|
private int loopDelay = 150;
|
||||||
private int stepCount;
|
private int[][] grid;
|
||||||
|
|
||||||
|
//TODO : add missing attribute(s)
|
||||||
|
|
||||||
public Simulator(MyInterface mjfParam) {
|
public Simulator(MyInterface mjfParam) {
|
||||||
this.interfaceGlobal = mjfParam;
|
mjf = mjfParam;
|
||||||
this.width = 100; // Example value
|
stopFlag=false;
|
||||||
this.height = 100; // Example value
|
pauseFlag=false;
|
||||||
this.world = new int[width][height];
|
loopingBorder=false;
|
||||||
this.agents = new ArrayList<>();
|
clickActionFlag=false;
|
||||||
this.loopingBorder = false;
|
|
||||||
this.pauseFlag = true; // Start in paused state
|
agents = new ArrayList<Agent>();
|
||||||
this.loopDelay = 1000;
|
fieldBirthValues = new ArrayList<Integer>();
|
||||||
this.timer = new Timer();
|
fieldSurviveValues = new ArrayList<Integer>();
|
||||||
this.stepCount = 0;
|
//TODO : add missing attribute initialization
|
||||||
|
|
||||||
|
grid = gridCreation();
|
||||||
|
|
||||||
|
//Default rule : Survive always, birth never
|
||||||
|
for(int i =0; i<9; i++) {
|
||||||
|
fieldSurviveValues.add(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startSimulationLoop() {
|
}
|
||||||
timer.scheduleAtFixedRate(new TimerTask() {
|
public int[][] gridCreation() {
|
||||||
@Override
|
int[][] newGrid = new int[COL_NUM][LINE_NUM];
|
||||||
|
//create the grid
|
||||||
|
for (int x = 0; x < COL_NUM; x ++) {
|
||||||
|
for (int y = 0; y < LINE_NUM; y ++) {
|
||||||
|
if (grid[x][y] == 0)
|
||||||
|
System.out.print("0");
|
||||||
|
else
|
||||||
|
System.out.print("1");
|
||||||
|
//create cell
|
||||||
|
//cest fait jpeux pas enlever le todo
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
return newGrid;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//TODO create a new generation
|
||||||
|
public int[][] nextGen(int gridCreation[][], int COL_NUM, int LINE_NUM){
|
||||||
|
int[][] future = new int[COL_NUM][LINE_NUM];
|
||||||
|
for (int x = 0; x < COL_NUM; x++) {
|
||||||
|
for (int y = 0; y < LINE_NUM; y++) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public int getWidth() {
|
||||||
|
return COL_NUM;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getHeight() {
|
||||||
|
return LINE_NUM;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Should probably stay as is
|
||||||
public void run() {
|
public void run() {
|
||||||
if (!pauseFlag) {
|
int stepCount=0;
|
||||||
makeStep();
|
while(!stopFlag) {
|
||||||
stepCount++;
|
stepCount++;
|
||||||
interfaceGlobal.update(stepCount);
|
makeStep();
|
||||||
|
mjf.update(stepCount);
|
||||||
|
try {
|
||||||
|
Thread.sleep(loopDelay);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
while(pauseFlag && !stopFlag) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(loopDelay);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, 0, loopDelay);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* method called at each step of the simulation
|
||||||
|
* makes all the actions to go from one step to the other
|
||||||
|
*/
|
||||||
public void makeStep() {
|
public void makeStep() {
|
||||||
int[][] newWorld = new int[width][height];
|
// agent behaviors first
|
||||||
|
// only modify if sure of what you do
|
||||||
|
// to modify agent behavior, see liveTurn method
|
||||||
|
// in agent classes
|
||||||
|
for(Agent agent : agents) {
|
||||||
|
ArrayList<Agent> neighbors =
|
||||||
|
this.getNeighboringAnimals(
|
||||||
|
agent.getX(),
|
||||||
|
agent.getY(),
|
||||||
|
ANIMAL_AREA_RADIUS);
|
||||||
|
if(!agent.liveTurn(
|
||||||
|
neighbors,
|
||||||
|
this)) {
|
||||||
|
agents.remove(agent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//then evolution of the field
|
||||||
|
// TODO : apply game rule to all cells of the field
|
||||||
|
|
||||||
|
/* you should distribute this action in methods/classes
|
||||||
|
* don't write everything here !
|
||||||
|
*
|
||||||
|
* the idea is first to get the surrounding values
|
||||||
|
* then count how many are alive
|
||||||
|
* then check if that number is in the lists of rules
|
||||||
|
* if the cell is alive
|
||||||
|
* and the count is in the survive list,
|
||||||
|
* then the cell stays alive
|
||||||
|
* if the cell is not alive
|
||||||
|
* and the count is in the birth list,
|
||||||
|
* then the cell becomes alive
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Apply Game of Life rules
|
|
||||||
for (int x = 0; x < width; x++) {
|
|
||||||
for (int y = 0; y < height; y++) {
|
|
||||||
int aliveNeighbors = countAliveNeighbors(x, y);
|
|
||||||
if (world[x][y] == 1) {
|
|
||||||
newWorld[x][y] = (aliveNeighbors < 2 || aliveNeighbors > 3) ? 0 : 1;
|
|
||||||
} else {
|
|
||||||
newWorld[x][y] = (aliveNeighbors == 3) ? 1 : 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
world = newWorld;
|
/*
|
||||||
|
* leave this as is
|
||||||
// Agents take their turn
|
*/
|
||||||
ArrayList<Agent> newAgents = new ArrayList<>();
|
public void stopSimu() {
|
||||||
for (Agent agent : agents) {
|
stopFlag=true;
|
||||||
if (agent.liveTurn(getNeighbors(agent), this)) {
|
|
||||||
newAgents.add(agent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
agents = newAgents;
|
|
||||||
|
|
||||||
interfaceGlobal.update(stepCount);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* method called when clicking pause button
|
||||||
|
*/
|
||||||
public void togglePause() {
|
public void togglePause() {
|
||||||
pauseFlag = !pauseFlag;
|
pauseFlag= ! pauseFlag;
|
||||||
if (!pauseFlag) {
|
// It changes the boolean value associated to pauseFlag (pauseFlag= not pauseFlag)
|
||||||
startSimulationLoop();
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* method called when clicking on a cell in the interface
|
||||||
|
*/
|
||||||
|
public void clickCell(int x, int y) {
|
||||||
|
//TODO : complete method
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get cell value in simulated world
|
||||||
|
* @param x coordinate of cell
|
||||||
|
* @param y coordinate of cell
|
||||||
|
* @return value of cell
|
||||||
|
*/
|
||||||
|
public int getCell(int x, int y) {
|
||||||
|
//get the value (dead or alive) of my cell at x y
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return list of Animals in simulated world
|
||||||
|
*/
|
||||||
|
public ArrayList<Agent> getAnimals(){
|
||||||
|
return agents;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* selects Animals in a circular area of simulated world
|
||||||
|
* @param x center
|
||||||
|
* @param y center
|
||||||
|
* @param radius
|
||||||
|
* @return list of agents in area
|
||||||
|
*/
|
||||||
|
public ArrayList<Agent> getNeighboringAnimals(int x, int y, int radius){
|
||||||
|
ArrayList<Agent> inArea = new ArrayList<Agent>();
|
||||||
|
for(int i=0;i<agents.size();i++) {
|
||||||
|
Agent agent = agents.get(i);
|
||||||
|
if(agent.isInArea(x,y,radius)) {
|
||||||
|
inArea.add(agent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return inArea;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set value of cell
|
||||||
|
* @param x coord of cell
|
||||||
|
* @param y coord of cell
|
||||||
|
* @param val to set in cell
|
||||||
|
*/
|
||||||
|
public void setCell(int x, int y, int val) {
|
||||||
|
//TODO : complete method
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return lines of file representing
|
||||||
|
* the simulated world in its present state
|
||||||
|
*/
|
||||||
|
public ArrayList<String> getSaveState() {
|
||||||
|
//TODO : complete method with proper return
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param lines of file representing saved world state
|
||||||
|
*/
|
||||||
|
public void loadSaveState(ArrayList<String> lines) {
|
||||||
|
/*
|
||||||
|
* First some checks that the file is usable
|
||||||
|
* We call early returns in conditions like this
|
||||||
|
* "Guard clauses", as they guard the method
|
||||||
|
* against unwanted inputs
|
||||||
|
*/
|
||||||
|
if(lines.size()<=0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String firstLine = lines.get(0);
|
||||||
|
String[] firstLineElements = firstLine.split(";");
|
||||||
|
if(firstLineElements.length<=0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* now we fill in the world
|
||||||
|
* with the content of the file
|
||||||
|
*/
|
||||||
|
for(int y =0; y<lines.size();y++) {
|
||||||
|
String line = lines.get(y);
|
||||||
|
String[] lineElements = line.split(";");
|
||||||
|
for(int x=0; x<lineElements.length;x++) {
|
||||||
|
String elem = lineElements[x];
|
||||||
|
int value = Integer.parseInt(elem);
|
||||||
|
setCell(x, y, value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void toggleLoopingBorder() {
|
/**
|
||||||
loopingBorder = !loopingBorder;
|
* called by button, with slider providing the argument
|
||||||
interfaceGlobal.setBorderBanner("border : " + (loopingBorder ? "looping" : "closed"));
|
* makes a new world state with random cell states
|
||||||
|
* @param chanceOfLife the chance for each cell
|
||||||
|
* to be alive in new state
|
||||||
|
*/
|
||||||
|
public void generateRandom(float chanceOfLife) {
|
||||||
|
//TODO : complete method
|
||||||
|
/*
|
||||||
|
* Advice :
|
||||||
|
* as you should probably have a separate class
|
||||||
|
* representing the field of cells...
|
||||||
|
* maybe just make a constructor in there
|
||||||
|
* and use it here
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isLoopingBorder() {
|
public boolean isLoopingBorder() {
|
||||||
return loopingBorder;
|
return loopingBorder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void toggleLoopingBorder() {
|
||||||
|
if (loopingBorder){
|
||||||
|
loopingBorder = false;
|
||||||
|
}else {
|
||||||
|
loopingBorder = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void setLoopDelay(int delay) {
|
public void setLoopDelay(int delay) {
|
||||||
loopDelay = delay;
|
//TODO : complete method
|
||||||
timer.cancel();
|
|
||||||
timer = new Timer();
|
|
||||||
startSimulationLoop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getWidth() {
|
public void toggleClickAction() {
|
||||||
return width;
|
//TODO : complete method
|
||||||
}
|
|
||||||
|
|
||||||
public int getHeight() {
|
|
||||||
return height;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getCell(int x, int y) {
|
|
||||||
return world[x][y];
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCell(int x, int y, int val) {
|
|
||||||
world[x][y] = val;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clickCell(int x, int y) {
|
|
||||||
setCell(x, y, getCell(x, y) == 1 ? 0 : 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void generateRandom(float chanceOfLife) {
|
|
||||||
Random rand = new Random();
|
|
||||||
for (int x = 0; x < width; x++) {
|
|
||||||
for (int y = 0; y < height; y++) {
|
|
||||||
world[x][y] = rand.nextFloat() < chanceOfLife ? 1 : 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
interfaceGlobal.update(stepCount); // Ensure the display updates
|
|
||||||
}
|
|
||||||
|
|
||||||
public ArrayList<Agent> getAgents() {
|
|
||||||
return agents;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ArrayList<Agent> getNeighbors(Agent agent) {
|
|
||||||
ArrayList<Agent> neighbors = new ArrayList<>();
|
|
||||||
for (Agent a : agents) {
|
|
||||||
if (a != agent && a.isInArea(agent.getX(), agent.getY(), 1)) {
|
|
||||||
neighbors.add(a);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return neighbors;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int countAliveNeighbors(int x, int y) {
|
|
||||||
int count = 0;
|
|
||||||
int[][] directions = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
|
|
||||||
for (int[] dir : directions) {
|
|
||||||
int nx = x + dir[0];
|
|
||||||
int ny = y + dir[1];
|
|
||||||
if (nx >= 0 && nx < width && ny >= 0 && ny < height) {
|
|
||||||
count += world[nx][ny];
|
|
||||||
} else if (loopingBorder) {
|
|
||||||
nx = (nx + width) % width;
|
|
||||||
ny = (ny + height) % height;
|
|
||||||
count += world[nx][ny];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prepare the content of a file saving present ruleSet
|
||||||
|
* as you might want to save a state,
|
||||||
|
* initialy written in this class constructor
|
||||||
|
* as a file for future use
|
||||||
|
* @return File content as an ArrayList of Lines (String)
|
||||||
|
* @see loadRule for inverse process
|
||||||
|
*/
|
||||||
public ArrayList<String> getRule() {
|
public ArrayList<String> getRule() {
|
||||||
return new ArrayList<>();
|
//TODO : complete method with proper return
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadRule(ArrayList<String> lines) {
|
public void loadRule(ArrayList<String> lines) {
|
||||||
// Implement loadRule logic
|
if(lines.size()<=0) {
|
||||||
|
System.out.println("empty rule file");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
//TODO : remove previous rule (=emptying lists)
|
||||||
|
|
||||||
public ArrayList<String> getSaveState() {
|
|
||||||
ArrayList<String> saveState = new ArrayList<>();
|
|
||||||
for (int y = 0; y < height; y++) {
|
|
||||||
StringBuilder line = new StringBuilder();
|
|
||||||
for (int x = 0; x < width; x++) {
|
|
||||||
if (x > 0) line.append(";");
|
|
||||||
line.append(world[x][y]);
|
|
||||||
}
|
|
||||||
saveState.add(line.toString());
|
|
||||||
}
|
|
||||||
return saveState;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void loadSaveState(ArrayList<String> lines) {
|
String surviveLine = lines.get(0);
|
||||||
for (int y = 0; y < lines.size(); y++) {
|
String birthLine = lines.get(1);
|
||||||
String[] values = lines.get(y).split(";");
|
String[] surviveElements = surviveLine.split(";");
|
||||||
for (int x = 0; x < values.length; x++) {
|
for(int x=0; x<surviveElements.length;x++) {
|
||||||
world[x][y] = Integer.parseInt(values[x]);
|
String elem = surviveElements[x];
|
||||||
|
int value = Integer.parseInt(elem);
|
||||||
|
//TODO : add value to possible survive values
|
||||||
|
|
||||||
}
|
}
|
||||||
|
String[] birthElements = birthLine.split(";");
|
||||||
|
for(int x=0; x<birthElements.length;x++) {
|
||||||
|
String elem = birthElements[x];
|
||||||
|
int value = Integer.parseInt(elem);
|
||||||
|
//TODO : add value to possible birth values
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<String> getAgentsSave() {
|
public ArrayList<String> getAgentsSave() {
|
||||||
return new ArrayList<>();
|
//TODO : Same idea as the other save method, but for agents
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadAgents(ArrayList<String> stringArray) {
|
public void loadAgents(ArrayList<String> stringArray) {
|
||||||
// Implement loadAgents logic
|
//TODO : Same idea as other load methods, but for agent list
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* used by label in interface to show the active click action
|
||||||
|
* @return String representation of click action
|
||||||
|
*/
|
||||||
public String clickActionName() {
|
public String clickActionName() {
|
||||||
|
// TODO : initially return "sheep" or "cell"
|
||||||
|
// depending on clickActionFlag
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ import backend.Agent;
|
||||||
import backend.Simulator;
|
import backend.Simulator;
|
||||||
|
|
||||||
public class JPanelDraw extends JPanel {
|
public class JPanelDraw extends JPanel {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
private Simulator mySimu;
|
private Simulator mySimu;
|
||||||
private MyInterface interfaceGlobal;
|
private MyInterface interfaceGlobal;
|
||||||
|
|
@ -22,17 +23,19 @@ public class JPanelDraw extends JPanel {
|
||||||
interfaceGlobal = itf;
|
interfaceGlobal = itf;
|
||||||
addMouseListener(new MouseAdapter() {
|
addMouseListener(new MouseAdapter() {
|
||||||
public void mousePressed(MouseEvent me) {
|
public void mousePressed(MouseEvent me) {
|
||||||
if (mySimu == null) {
|
// System.out.println(me);
|
||||||
|
if(mySimu == null) {
|
||||||
interfaceGlobal.instantiateSimu();
|
interfaceGlobal.instantiateSimu();
|
||||||
}
|
}
|
||||||
int x = (me.getX() * mySimu.getWidth()) / getWidth();
|
int x = (me.getX()*mySimu.getWidth())/getWidth();
|
||||||
int y = (me.getY() * mySimu.getHeight()) / getHeight();
|
int y = (me.getY()*mySimu.getHeight())/getHeight();
|
||||||
mySimu.clickCell(x, y);
|
mySimu.clickCell(x,y);
|
||||||
repaint();
|
repaint();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void setSimu(Simulator simu) {
|
public void setSimu(Simulator simu) {
|
||||||
mySimu = simu;
|
mySimu = simu;
|
||||||
}
|
}
|
||||||
|
|
@ -42,37 +45,50 @@ public class JPanelDraw extends JPanel {
|
||||||
super.paintComponent(g);
|
super.paintComponent(g);
|
||||||
this.setBackground(Color.black);
|
this.setBackground(Color.black);
|
||||||
if (mySimu != null) {
|
if (mySimu != null) {
|
||||||
float cellWidth = (float) this.getWidth() / (float) mySimu.getWidth();
|
// Draw Interface from state of simulator
|
||||||
float cellHeight = (float) this.getHeight() / (float) mySimu.getHeight();
|
float cellWidth = (float)this.getWidth()/(float)mySimu.getWidth();
|
||||||
|
float cellHeight = (float)this.getHeight()/(float)mySimu.getHeight();
|
||||||
g.setColor(Color.gray);
|
g.setColor(Color.gray);
|
||||||
for (int x = 0; x < mySimu.getWidth(); x++) {
|
for(int x=0; x<mySimu.getWidth();x++) {
|
||||||
int graphX = Math.round(x * cellWidth);
|
int graphX = Math.round(x*cellWidth);
|
||||||
g.drawLine(graphX, 0, graphX, this.getHeight());
|
g.drawLine(graphX, 0, graphX, this.getHeight());
|
||||||
}
|
}
|
||||||
for (int y = 0; y < mySimu.getHeight(); y++) {
|
for (int y=0; y<mySimu.getHeight(); y++) {
|
||||||
int graphY = Math.round(y * cellHeight);
|
int graphY = Math.round(y*cellHeight);
|
||||||
g.drawLine(0, graphY, this.getWidth(), graphY);
|
g.drawLine(0, graphY, this.getWidth(), graphY);
|
||||||
}
|
}
|
||||||
for (int x = 0; x < mySimu.getWidth(); x++) {
|
for(int x=0; x<mySimu.getWidth();x++) {
|
||||||
for (int y = 0; y < mySimu.getHeight(); y++) {
|
for (int y=0; y<mySimu.getHeight(); y++) {
|
||||||
int cellContent = mySimu.getCell(x, y);
|
int cellContent = mySimu.getCell(x,y);
|
||||||
if (cellContent == 0) {
|
if(cellContent == 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (cellContent == 1) {
|
if(cellContent == 1) {
|
||||||
g.setColor(Color.white);
|
g.setColor(Color.green);
|
||||||
} else {
|
|
||||||
g.setColor(Color.getHSBColor(cellContent / 10.0f, 1.0f, 1.0f));
|
|
||||||
}
|
}
|
||||||
g.fillRect(Math.round(x * cellWidth), Math.round(y * cellHeight), Math.round(cellWidth), Math.round(cellHeight));
|
if(cellContent == 2) {
|
||||||
|
g.setColor(Color.yellow);
|
||||||
|
}
|
||||||
|
if(cellContent == 3) {
|
||||||
|
g.setColor(Color.cyan);
|
||||||
|
}
|
||||||
|
g.fillRect(
|
||||||
|
(int) Math.round(x*cellWidth),
|
||||||
|
(int) Math.round(y*cellHeight),
|
||||||
|
(int) Math.round(cellWidth),
|
||||||
|
(int) Math.round(cellHeight)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (Agent ag : mySimu.getAgents()) {
|
for (Agent animal:mySimu.getAnimals()){
|
||||||
g.setColor(ag.getDisplayColor());
|
g.setColor(animal.getDisplayColor());
|
||||||
int graphX = Math.round(ag.getX() * cellWidth);
|
g.fillOval((int)Math.round(animal.getX()*cellWidth),
|
||||||
int graphY = Math.round(ag.getY() * cellHeight);
|
(int)Math.round(animal.getY()*cellHeight),
|
||||||
g.fillOval(graphX, graphY, Math.round(cellWidth), Math.round(cellHeight));
|
(int)Math.round(cellWidth/2),
|
||||||
|
(int)Math.round(cellHeight/2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,133 +1,331 @@
|
||||||
package windowInterface;
|
package windowInterface;
|
||||||
|
|
||||||
import java.awt.BorderLayout;
|
import java.awt.BorderLayout;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.Dimension;
|
||||||
|
import java.awt.GridLayout;
|
||||||
|
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.JSlider;
|
||||||
|
import javax.swing.SwingConstants;
|
||||||
|
import javax.swing.border.EmptyBorder;
|
||||||
|
import javax.swing.event.ChangeEvent;
|
||||||
|
import javax.swing.event.ChangeListener;
|
||||||
|
|
||||||
|
import backend.Simulator;
|
||||||
|
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JFileChooser;
|
||||||
|
import javax.swing.JLabel;
|
||||||
|
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import javax.swing.JButton;
|
import java.awt.event.ActionEvent;
|
||||||
import javax.swing.JFileChooser;
|
|
||||||
import javax.swing.JFrame;
|
|
||||||
import javax.swing.JLabel;
|
|
||||||
import javax.swing.JPanel;
|
|
||||||
import javax.swing.JSlider;
|
|
||||||
|
|
||||||
import backend.Simulator;
|
|
||||||
|
|
||||||
public class MyInterface extends JFrame {
|
public class MyInterface extends JFrame {
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
private Simulator mySimu;
|
private static final long serialVersionUID = -6840815447618468846L;
|
||||||
private JPanelDraw drawPanel;
|
private JPanel contentPane;
|
||||||
private JLabel stepBanner;
|
private JLabel stepLabel;
|
||||||
private JLabel borderBanner;
|
private JLabel borderLabel;
|
||||||
private JLabel clickBanner;
|
private JLabel speedLabel;
|
||||||
|
private JPanelDraw panelDraw;
|
||||||
|
private Simulator mySimu=null;
|
||||||
|
private JSlider randSlider;
|
||||||
private JSlider speedSlider;
|
private JSlider speedSlider;
|
||||||
|
private JLabel clickLabel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the frame.
|
||||||
|
*/
|
||||||
public MyInterface() {
|
public MyInterface() {
|
||||||
super("Simulator Interface");
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setBounds(10, 10, 700, 600);
|
||||||
this.setSize(800, 600);
|
contentPane = new JPanel();
|
||||||
|
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||||
|
contentPane.setLayout(new BorderLayout(0, 0));
|
||||||
|
setContentPane(contentPane);
|
||||||
|
|
||||||
drawPanel = new JPanelDraw(this);
|
JPanel panelTop = new JPanel();
|
||||||
this.add(drawPanel, BorderLayout.CENTER);
|
contentPane.add(panelTop, BorderLayout.NORTH);
|
||||||
|
|
||||||
JPanel controlPanel = new JPanel();
|
JPanel panelRight = new JPanel();
|
||||||
|
panelRight.setLayout(new GridLayout(12,1));
|
||||||
|
contentPane.add(panelRight, BorderLayout.EAST);
|
||||||
|
|
||||||
JButton startPauseButton = new JButton("Start/Pause");
|
JButton btnGo = new JButton("Start/Pause");
|
||||||
startPauseButton.addActionListener(new ActionListener() {
|
btnGo.addActionListener(new ActionListener() {
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent arg0) {
|
||||||
if (mySimu != null) {
|
clicButtonGo();
|
||||||
mySimu.togglePause();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
controlPanel.add(startPauseButton);
|
panelTop.add(btnGo);
|
||||||
|
|
||||||
JButton stopButton = new JButton("Stop");
|
JButton btnClickAct = new JButton("Toggle Click");
|
||||||
stopButton.addActionListener(new ActionListener() {
|
btnClickAct.addActionListener(new ActionListener() {
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent arg0) {
|
||||||
if (mySimu != null) {
|
clicButtonToggleClickAction();
|
||||||
mySimu = null;
|
|
||||||
drawPanel.setSimu(null);
|
|
||||||
drawPanel.repaint();
|
|
||||||
eraseLabels();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
controlPanel.add(stopButton);
|
panelTop.add(btnClickAct);
|
||||||
|
|
||||||
JButton toggleBorderButton = new JButton("Toggle Border");
|
stepLabel = new JLabel("Step : X");
|
||||||
toggleBorderButton.addActionListener(new ActionListener() {
|
panelTop.add(stepLabel);
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
if (mySimu != null) {
|
speedLabel = new JLabel("speed slider : ");
|
||||||
mySimu.toggleLoopingBorder();
|
panelTop.add(speedLabel);
|
||||||
}
|
|
||||||
|
speedSlider = new JSlider();
|
||||||
|
speedSlider.setValue(3);
|
||||||
|
speedSlider.setMinimum(0);
|
||||||
|
speedSlider.setMaximum(10);
|
||||||
|
speedSlider.setOrientation(SwingConstants.HORIZONTAL);
|
||||||
|
speedSlider.setPreferredSize(new Dimension(100,30));
|
||||||
|
speedSlider.addChangeListener(new ChangeListener() {
|
||||||
|
public void stateChanged(ChangeEvent arg0) {
|
||||||
|
changeSpeed();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
controlPanel.add(toggleBorderButton);
|
panelTop.add(speedSlider);
|
||||||
|
|
||||||
JButton randomButton = new JButton("Random");
|
// JButton btnSpeed = new JButton("Set Speed");
|
||||||
randomButton.addActionListener(new ActionListener() {
|
// btnSpeed.addActionListener(new ActionListener() {
|
||||||
public void actionPerformed(ActionEvent e) {
|
// public void actionPerformed(ActionEvent e) {
|
||||||
if (mySimu != null) {
|
// clicButtonSpeed();
|
||||||
mySimu.generateRandom(0.2f); // Example chance of life
|
// }
|
||||||
drawPanel.repaint();
|
// });
|
||||||
}
|
// panelTop.add(btnSpeed);
|
||||||
|
|
||||||
|
JButton btnLoad = new JButton("Load World");
|
||||||
|
btnLoad.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent arg0) {
|
||||||
|
clicLoadFileButton();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
controlPanel.add(randomButton);
|
panelRight.add(btnLoad);
|
||||||
|
|
||||||
speedSlider = new JSlider(1, 10, 3);
|
JButton btnSave = new JButton("Save World");
|
||||||
speedSlider.addChangeListener(e -> {
|
btnSave.addActionListener(new ActionListener() {
|
||||||
if (mySimu != null) {
|
public void actionPerformed(ActionEvent arg0) {
|
||||||
mySimu.setLoopDelay(1000 / speedSlider.getValue());
|
clicSaveToFileButton();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
controlPanel.add(speedSlider);
|
panelRight.add(btnSave);
|
||||||
|
|
||||||
this.add(controlPanel, BorderLayout.SOUTH);
|
|
||||||
|
|
||||||
JPanel infoPanel = new JPanel();
|
JButton btnLoadRule = new JButton("Load Rule");
|
||||||
|
btnLoadRule.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent arg0) {
|
||||||
|
clicLoadRuleFileButton();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
panelRight.add(btnLoadRule);
|
||||||
|
|
||||||
stepBanner = new JLabel("Step : X");
|
JButton btnSaveRule = new JButton("Save Rule");
|
||||||
infoPanel.add(stepBanner);
|
btnSaveRule.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent arg0) {
|
||||||
|
clicSaveRuleToFileButton();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
panelRight.add(btnSaveRule);
|
||||||
|
|
||||||
borderBanner = new JLabel("border : X");
|
JButton btnLoadAgents = new JButton("Load Agents");
|
||||||
infoPanel.add(borderBanner);
|
btnLoadAgents.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent arg0) {
|
||||||
|
clicLoadAgentsFileButton();;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
panelRight.add(btnLoadAgents);
|
||||||
|
|
||||||
clickBanner = new JLabel("click : X");
|
JButton btnSaveAgents = new JButton("Save Agents");
|
||||||
infoPanel.add(clickBanner);
|
btnSaveAgents.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent arg0) {
|
||||||
|
clicSaveAgentsToFileButton();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
panelRight.add(btnSaveAgents);
|
||||||
|
|
||||||
this.add(infoPanel, BorderLayout.NORTH);
|
JButton btnRandGen = new JButton("Random Field");
|
||||||
|
btnRandGen.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent arg0) {
|
||||||
|
generateRandomBoard();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
panelRight.add(btnRandGen);
|
||||||
|
|
||||||
|
|
||||||
|
JLabel randLabel = new JLabel("random density slider :");
|
||||||
|
panelRight.add(randLabel);
|
||||||
|
|
||||||
|
randSlider = new JSlider();
|
||||||
|
randSlider.setValue(50);
|
||||||
|
randSlider.setMinimum(0);
|
||||||
|
randSlider.setMaximum(100);
|
||||||
|
randSlider.setPreferredSize(new Dimension(30,200));
|
||||||
|
panelRight.add(randSlider);
|
||||||
|
|
||||||
|
|
||||||
|
JButton btnBorder = new JButton("Toggle Border");
|
||||||
|
btnBorder.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent arg0) {
|
||||||
|
clicButtonBorder();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
panelRight.add(btnBorder);
|
||||||
|
|
||||||
|
panelDraw = new JPanelDraw(this);
|
||||||
|
contentPane.add(panelDraw, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
instantiateSimu();
|
||||||
|
|
||||||
|
borderLabel = new JLabel("border : X");
|
||||||
|
panelRight.add(borderLabel);
|
||||||
|
borderLabel.setText("border : " +
|
||||||
|
(mySimu.isLoopingBorder()?"loop":"closed"));
|
||||||
|
|
||||||
|
clickLabel = new JLabel("click : X");
|
||||||
|
panelRight.add(clickLabel);
|
||||||
|
clickLabel.setText("click : " + mySimu.clickActionName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStepBanner(String s) {
|
||||||
|
stepLabel.setText(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBorderBanner(String s) {
|
||||||
|
borderLabel.setText(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setClickBanner(String s) {
|
||||||
|
clickLabel.setText(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
public JPanelDraw getPanelDessin() {
|
||||||
|
return panelDraw;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void instantiateSimu() {
|
public void instantiateSimu() {
|
||||||
|
if(mySimu==null) {
|
||||||
mySimu = new Simulator(this);
|
mySimu = new Simulator(this);
|
||||||
drawPanel.setSimu(mySimu);
|
panelDraw.setSimu(mySimu);
|
||||||
setStepBanner("Step : 0");
|
}
|
||||||
setBorderBanner("border : closed");
|
|
||||||
setClickBanner("click : cell");
|
|
||||||
mySimu.setLoopDelay(1000 / speedSlider.getValue());
|
|
||||||
mySimu.startSimulationLoop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setStepBanner(String text) {
|
public void clicButtonGo() {
|
||||||
stepBanner.setText(text);
|
this.instantiateSimu();
|
||||||
|
if(!mySimu.isAlive()) {
|
||||||
|
mySimu.start();
|
||||||
|
} else {
|
||||||
|
mySimu.togglePause();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBorderBanner(String text) {
|
public void clicButtonToggleClickAction() {
|
||||||
borderBanner.setText(text);
|
if(mySimu != null) {
|
||||||
|
mySimu.toggleClickAction();
|
||||||
|
clickLabel.setText("click : " + mySimu.clickActionName());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setClickBanner(String text) {
|
public void clicButtonBorder() {
|
||||||
clickBanner.setText(text);
|
if(mySimu != null) {
|
||||||
|
mySimu.toggleLoopingBorder();
|
||||||
|
borderLabel.setText("border : " +
|
||||||
|
(mySimu.isLoopingBorder()?"loop":"closed"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void generateRandomBoard() {
|
||||||
|
this.instantiateSimu();
|
||||||
|
float chanceOfLife = ((float)randSlider.getValue())/((float)randSlider.getMaximum());
|
||||||
|
mySimu.generateRandom(chanceOfLife);
|
||||||
|
panelDraw.repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void changeSpeed() {
|
||||||
|
if(mySimu != null) {
|
||||||
|
int delay = (int)Math.pow(2, 10-speedSlider.getValue());
|
||||||
|
mySimu.setLoopDelay(delay);
|
||||||
|
} else {
|
||||||
|
speedSlider.setValue(3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clicLoadFileButton() {
|
||||||
|
Simulator loadedSim = new Simulator(this);
|
||||||
|
String fileName=SelectFile();
|
||||||
|
ArrayList<String> stringArray = new ArrayList<String>();
|
||||||
|
if (fileName.length()>0) {
|
||||||
|
try {
|
||||||
|
BufferedReader fileContent = new BufferedReader(new FileReader(fileName));
|
||||||
|
String line = fileContent.readLine();
|
||||||
|
while (line != null) {
|
||||||
|
stringArray.add(line);
|
||||||
|
line = fileContent.readLine();
|
||||||
|
}
|
||||||
|
fileContent.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
if(mySimu != null) {
|
||||||
|
mySimu.stopSimu();
|
||||||
|
this.eraseLabels();
|
||||||
|
}
|
||||||
|
loadedSim.loadSaveState(stringArray);
|
||||||
|
mySimu = loadedSim;
|
||||||
|
panelDraw.setSimu(mySimu);
|
||||||
|
this.repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clicLoadRuleFileButton() {
|
||||||
|
String fileName=SelectFile();
|
||||||
|
ArrayList<String> stringArray = new ArrayList<String>();
|
||||||
|
if (fileName.length()>0) {
|
||||||
|
try {
|
||||||
|
BufferedReader fileContent = new BufferedReader(new FileReader(fileName));
|
||||||
|
String line = fileContent.readLine();
|
||||||
|
while (line != null) {
|
||||||
|
stringArray.add(line);
|
||||||
|
line = fileContent.readLine();
|
||||||
|
}
|
||||||
|
fileContent.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
mySimu.loadRule(stringArray);
|
||||||
|
this.repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clicLoadAgentsFileButton() {
|
||||||
|
String fileName=SelectFile();
|
||||||
|
ArrayList<String> stringArray = new ArrayList<String>();
|
||||||
|
if (fileName.length()>0) {
|
||||||
|
try {
|
||||||
|
BufferedReader fileContent = new BufferedReader(new FileReader(fileName));
|
||||||
|
String line = fileContent.readLine();
|
||||||
|
while (line != null) {
|
||||||
|
stringArray.add(line);
|
||||||
|
line = fileContent.readLine();
|
||||||
|
}
|
||||||
|
fileContent.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
mySimu.loadAgents(stringArray);
|
||||||
|
this.repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void clicSaveToFileButton() {
|
public void clicSaveToFileButton() {
|
||||||
String fileName = SelectFile();
|
String fileName=SelectFile();
|
||||||
if (fileName.length() > 0) {
|
if (fileName.length()>0) {
|
||||||
ArrayList<String> content = mySimu.getSaveState();
|
ArrayList<String> content = mySimu.getSaveState();
|
||||||
String[] strArr = Arrays.copyOf(content.toArray(), content.toArray().length, String[].class);
|
String[] strArr = Arrays.copyOf(content.toArray(), content.toArray().length, String[].class);
|
||||||
writeFile(fileName, strArr);
|
writeFile(fileName, strArr);
|
||||||
|
|
@ -135,8 +333,8 @@ public class MyInterface extends JFrame {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clicSaveRuleToFileButton() {
|
public void clicSaveRuleToFileButton() {
|
||||||
String fileName = SelectFile();
|
String fileName=SelectFile();
|
||||||
if (fileName.length() > 0) {
|
if (fileName.length()>0) {
|
||||||
ArrayList<String> content = mySimu.getRule();
|
ArrayList<String> content = mySimu.getRule();
|
||||||
String[] strArr = Arrays.copyOf(content.toArray(), content.toArray().length, String[].class);
|
String[] strArr = Arrays.copyOf(content.toArray(), content.toArray().length, String[].class);
|
||||||
writeFile(fileName, strArr);
|
writeFile(fileName, strArr);
|
||||||
|
|
@ -144,14 +342,15 @@ public class MyInterface extends JFrame {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clicSaveAgentsToFileButton() {
|
public void clicSaveAgentsToFileButton() {
|
||||||
String fileName = SelectFile();
|
String fileName=SelectFile();
|
||||||
if (fileName.length() > 0) {
|
if (fileName.length()>0) {
|
||||||
ArrayList<String> content = mySimu.getAgentsSave();
|
ArrayList<String> content = mySimu.getAgentsSave();
|
||||||
String[] strArr = Arrays.copyOf(content.toArray(), content.toArray().length, String[].class);
|
String[] strArr = Arrays.copyOf(content.toArray(), content.toArray().length, String[].class);
|
||||||
writeFile(fileName, strArr);
|
writeFile(fileName, strArr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public String SelectFile() {
|
public String SelectFile() {
|
||||||
String s;
|
String s;
|
||||||
JFileChooser chooser = new JFileChooser();
|
JFileChooser chooser = new JFileChooser();
|
||||||
|
|
@ -160,10 +359,10 @@ public class MyInterface extends JFrame {
|
||||||
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
|
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
|
||||||
chooser.setAcceptAllFileFilterUsed(true);
|
chooser.setAcceptAllFileFilterUsed(true);
|
||||||
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
|
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
|
||||||
s = chooser.getSelectedFile().toString();
|
s=chooser.getSelectedFile().toString();
|
||||||
} else {
|
} else {
|
||||||
System.out.println("No Selection ");
|
System.out.println("No Selection ");
|
||||||
s = "";
|
s="";
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
@ -183,8 +382,8 @@ public class MyInterface extends JFrame {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(int stepCount) {
|
public void update (int stepCount) {
|
||||||
this.setStepBanner("Step : " + stepCount);
|
this.setStepBanner("Step : "+ stepCount);
|
||||||
this.repaint();
|
this.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -194,4 +393,5 @@ public class MyInterface extends JFrame {
|
||||||
this.setClickBanner("click : X");
|
this.setClickBanner("click : X");
|
||||||
speedSlider.setValue(3);
|
speedSlider.setValue(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue